ICPC Kingdom
Time limit2sMemory limit1024 MB
Pick exactly k roads that form no cycle and can each be assigned to a different worker, maximizing the sum of floor(sqrt(a_u + a_v)) for each k.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Union-find, Greedy
- Solved
- No attempts yet
Problem
The ICPC kingdom has cities numbered from to , and roads numbered from to that connect these cities. Each road connects two cities, and people can travel along the roads in both directions. City has residents. Road connects city and city . The economic benefit of road is .
One day, an enemy invaded the ICPC kingdom and destroyed all roads. Fortunately, the ICPC troops defeated the enemy and stopped the invasion. To recover from the war, the king of ICPC hired workers to fix the roads. Each worker can fix at most one road, and the -th worker can only fix one of the roads whose indices are among .
The king does not want the repair plan to contain any useless road. A plan contains a useless road if there exist cities and with more than one simple path between them. A simple path is a sequence of distinct roads such that a trip along visits exactly distinct cities.
The king asks you to calculate the maximum economic benefit when exactly roads are fixed without useless roads. Calculate it for each from to .
Input
The first line contains and separated by a space. is the number of cities, and is the number of roads. The second line contains , the number of residents in cities . The next lines each contain and , meaning road connects cities and . The next line contains , the number of workers. The following lines describe the roads each worker can fix. The -th line starts with , the number of roads the -th worker can fix, followed by distinct integers . The -th worker can fix only one of these roads.
Output
Print numbers. The -th number is the maximum economic benefit when the repair plan fixes exactly roads without useless roads. If no such plan exists, print -1 for that .