Traffic
Time limit5sMemory limit128 MB
Given a planar graph of junctions on an island with one-way and two-way streets, count for each western-side junction how many eastern-side junctions it can reach, exploiting planarity and geometric structure since streets cannot cross.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Union-find
- Solved
- No attempts yet
Problem
The center of Gdynia lies on an island in the middle of the Kacza river. Every morning thousands of cars cross the island from the residential districts on the western bank (entering through junctions on the western side of the island) to the industrial areas on the eastern bank (leaving through junctions on the eastern side).
The island is a rectangle whose sides are parallel to the axes. We model it as an rectangle in the Cartesian plane whose opposite corners are and .
There are junctions on the island, numbered from to . Junction has coordinates . A junction with coordinates lies on the western side of the island; a junction with coordinates lies on the eastern side. Junctions are connected by streets, each a straight segment joining two junctions. A street is either one-way or two-way. No two streets share any point other than a common endpoint at a junction (there are no bridges or tunnels). Nothing else about the network is assumed: streets may run along a bank, and a junction may have no incoming or outgoing streets.
For every junction on the western side, determine how many junctions on the eastern side are reachable from it.
Input
The first line contains four integers , , , (, , ): the number of junctions, the number of streets, and the dimensions of the island.
Each of the next lines contains two integers , (, ), the coordinates of junction . No two junctions share the same coordinates.
Each of the next lines contains three integers , , (, , ) describing a street between junctions and . If the street is one-way from to ; if it can be driven in both directions. Each unordered pair appears at most once.
At least one junction on the western side can reach some junction on the eastern side.
Output
Print one line for each junction on the western side. Each line contains the number of junctions on the eastern side reachable from that junction. Output the western junctions in order of decreasing -coordinate.
Hint
