Road Development
Time limit2sMemory limit256 MB
Given N cities and Q plans, some carried out, find for each abandoned plan how many unpaved roads on shortest paths in the current graph it would have paved, or -1 if it builds a new road.
- Level
Hard9 of 10
- Topics
- Graph, Union-find, Tree, Dynamic programming
- Solved
- No attempts yet
Problem
IOI Country is a nation made up of cities. The cities are numbered . Professor JOI became interested in how the road network of IOI Country was developed.
Professor JOI examined materials on the history of IOI Country and learned the following.
- The cities of IOI Country have been the same from right after its founding to the present. Right after the founding of IOI Country, there was not a single road connecting cities.
- In year after the founding of IOI Country (), a plan to improve the traffic conditions between city and city was made.
- Some of the planned improvements were carried out as planned, while other plans were abandoned without being carried out.
- The materials make clear which improvement plans were carried out.
- Every improvement plan that was carried out was completed within one year.
From another document, Professor JOI learned that a plan to improve the traffic conditions between city and city works as follows.
- If city cannot be reached from city using roads already built at the time the improvement plan is made, a new road connecting city and city in both directions is built. The newly built road is unpaved.
- If city can be reached from city using roads already built at the time the improvement plan is made, then among all such routes, take one that uses the minimum number of roads and pave every unpaved road on it. If there are multiple routes using the minimum number of roads, pave the unpaved roads on all of them in the same way. A road that has already been paved is never paved again.
For further investigation, Professor JOI decided to compute, for each improvement plan that was abandoned without being carried out, how many roads would have been paved by that plan if only that plan had additionally been carried out.
Given the traffic improvement plans of IOI Country and whether each was carried out, write a program that computes, for each improvement plan that was abandoned without being carried out, how many roads would have been paved by that plan if it had been carried out.
Input
Read the following data from standard input.
- The first line contains two integers and separated by a space. This means that IOI Country has cities, and Professor JOI is looking at the traffic improvement plans over years from the founding.
- Of the following lines, the -th line () contains three integers , , separated by spaces. The integer represents the status of the improvement plan made in year after the founding: means the plan was carried out, and means the plan was abandoned without being carried out. The integers and mean that in year after the founding, a plan to improve the traffic conditions between city and city was made.
Output
For each improvement plan that was abandoned without being carried out, output on a single line the number of roads that would have been paved by that plan if it had been carried out. If carrying out that plan would build a new road, output -1.
Constraints
- .
- .
- ().
- ().
- ().
- ().