New Island
Time limit1sMemory limit128 MB
Given a graph whose edge i costs 2^i, delete the cheapest set of edges so the graph stays connected and every pairwise distance at most doubles.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Greedy, Implementation
- Solved
- No attempts yet
Problem
A new island has been discovered. A team of architects designed a road plan that connects the island's important places, but the budget is too small to build all of it, so the plan must be trimmed into a cheaper one.
In the proposed plan, every road has a distinct id between and (where is the number of roads), and the road with id costs exactly . Because the ids are all different, the road costs are distinct powers of two.
We want to remove some roads so that the total cost of the remaining roads is as small as possible, while every place stays connected. However, roads may not be removed freely: in the reduced plan, the distance between any two places must not become more than twice their distance in the original plan. The distance between two places is the minimum number of roads on a route connecting them.
You are given the original road plan as a graph. Find the removal that minimizes the total remaining cost while respecting the distance constraint. Because the costs are distinct powers of two, this optimal removal is unique.
Input
The input contains several test cases. Each test case begins with a line containing two integers () and — the number of places (vertices) and the number of roads (edges). Each of the next lines contains two integers and , meaning the road with id runs between places and (roads are numbered through in the order they are given).
The input ends with a line containing two zeros.
Output
For each test case, print a single line containing the number of removed roads, followed by the ids of the removed roads in increasing order. If no road is removed, print a single .