Export Estimate
Time limit4sMemory limit512 MB
For each threshold query, count the vertices and edges left after deleting low-priority streets and contracting degree-two vertices in index order.
- Level
Hard8 of 10
- Topics
- Graph, Union-find, Sorting
- Solved
- No attempts yet
Problem
Luka runs a geographic data company. The company maintains a detailed city map and exports the data to interested parties. Most clients do not want the complete map. They want a simplified map that keeps only the major streets.
The city map is an undirected graph with intersections, numbered from to , and two-way streets. Every street carries a priority, which is a non-negative integer. A client who requests a map picks a threshold priority . The original map is copied, and the exported map is produced by the following procedure.
-
Delete every street whose priority is lower than .
-
Process the intersections in the order .
-
If intersection has no street attached to it, delete intersection .
-
If intersection has exactly two different streets and attached to it, where leads to intersection and leads to intersection , and both and differ from , contract intersection as follows.
- Delete streets and .
- Delete intersection .
- Add a new street connecting intersections and .
-

The picture shows threshold priority applied to the map of the second example.
The initial map has no loop (a street joining an intersection to itself) and no parallel streets (more than one street between the same pair of intersections), but contraction can create both. In the second rule of step 2 neither nor can be a loop, because and both differ from , yet the new street can be a loop, because and may be equal.
You are given the map and a sequence of export requests. For each request, report how many intersections and how many streets the exported map has.
Input
The first line contains the number of intersections and the number of streets . (, )
Each of the next lines contains three integers , and . (, ) They describe a street of priority connecting intersections and . No street joins an intersection to itself, and there is at most one street between any two intersections.
The next line contains the number of export requests . ()
The last line contains integers. The -th integer is the threshold priority of the -th request. ()
Output
Print lines. On the -th line print the number of intersections and the number of streets of the map exported for the -th request, separated by a space.