Postcards
Time limit8sMemory limit256 MB
For each shutdown plan, count the cities that can reach every other city when some roads are closed in one direction, with each plan decoded from earlier answers.
- Level
Hard9 of 10
- Topics
- Graph, Divide and conquer, Union-find, Topological sort
- Solved
- No attempts yet
Problem
Bithuania has cities connected with bidirectional roads. Currently, there exists a path between every pair of cities. Each city has one child living in it. Every child would like to send a Christmas postcard to every other child. Unfortunately, the ongoing Christmas period means some roads are being renovated. Because of this, postcards can be sent in only one direction on some roads, and cannot be sent along others at all.
The government of Bithuania has drawn up several plans for shutting down roads. For each plan, in order, find the number of children who can receive a postcard from every other child.
Input
The first line contains three integers , , (, , ). The next lines describe the road network: the -th line contains two integers , (, ), meaning a road between cities and . Roads are numbered through in input order. A pair of cities may be connected by more than one road.
Then come shutdown plans, numbered through . The -th plan starts with a line containing an integer (), the number of renovated roads. Then lines follow, each describing one shut down road with three numbers , , (, , ).
Let be the sum of the answers to plans through . The -th plan refers to road . If , road cannot be used to travel from city to city . If , road cannot be used to travel from city to city .
For each plan, the values of are distinct. The sum of all does not exceed . Because each plan depends on earlier answers, the plans must be processed online.
Output
For every shutdown plan, print one line with the number of children who can receive a postcard from every other child.
Hint
In the first plan, road 4 (connecting cities 1 and 3) is closed completely and road 2 is partially closed (travel from 3 to 4 is not possible). Children in cities 1, 2, and 3 can receive postcards from all other children.
In the second plan, . Roads 1, 4, and 3 are partially blocked: travel from 1 to 2, from 1 to 3, and from 2 to 3 is disallowed, respectively. Only the child in city 1 can receive a postcard from all other children.
In the last plan, . Road 2 is blocked. No child can receive a postcard from all other children.