Link Cut Tomato
Time limit3.5sMemory limit1024 MB
Given a graph whose edges form or break on given days, find the earliest day each tomato ripens when ripeness spreads across connected edges.
- Level
Hard8 of 10
- Topics
- Graph, Union-find, Segment tree, Shortest path
- Solved
- No attempts yet
Problem
It is well known that tomatoes form a network by connecting and disconnecting with each other, using their stems like antennas. A connection between two tomatoes can form or break only at the moment the date changes, and the connection state of any two tomatoes does not change more than once a day.
John, a farmer who majored in tomato networks, found a link between the connection states of tomatoes and how ripe they are. According to John's finding, if a ripe tomato and an unripe tomato are connected for a day, the unripe tomato ripens under the influence of the ripe one.
After long research, John built a device that observes the connection states between tomatoes. With this device, you can see which tomatoes are ripe on day and which tomatoes are connected on day . You can also track when a connection between any two tomatoes forms or breaks.
Chanwoo spent all the money he had for setting problems for the Cheonha Jeil Coding Contest on John's device and tomatoes numbered through , but he could not figure out when each tomato ripens. Write a program that computes the ripening day of each tomato from the information obtained from John's device, so that Chanwoo can eat the ripe tomatoes.
Input
The first line contains four integers , , , and , separated by spaces, which are the number of tomatoes, the number of connected pairs on day , the number of tomatoes that are ripe on day , and the number of changes in connection states.
The next lines contain the initial connection states on day , with no duplicates. Each line contains two tomato numbers and , separated by a space, that are connected on day .
The next line contains the numbers of the distinct tomatoes that are ripe on day , separated by spaces.
The next lines describe the changes in connection states in the order they occur. Each line contains a day and two tomato numbers and , separated by spaces. If the two tomatoes were connected on day , they become disconnected from midnight of day . If they were not connected on day , they become newly connected from midnight of day . Every pair in the input is distinct.
All numbers in the input are integers.
Output
On the first line, print integers separated by spaces. For the -th tomato, print if it never ripens, or the day it ripens otherwise.
Hint
The input and output are large, so it is recommended to use the fast I/O described in the language guide on pages 2-4 of the problem set.