Link-Cut Tree
Time limit2sMemory limit1024 MB
Given an undirected graph where edge i has length 2^i, find the simple cycle with the smallest total length and print its edge indices, or -1 if no cycle exists.
- Level
Hard8 of 10
- Topics
- Graph, Union-find, Greedy
- Solved
- No attempts yet
Problem
BaoBao just learned how to find cycles in a graph with a data structure called link-cut tree, and decided to try it. You are given an undirected graph with vertices and edges. The length of the -th edge is . BaoBao needs to find a simple cycle with the smallest length.
A simple cycle is a subgraph that contains () vertices and edges, such that for every there is an edge connecting and in the subgraph. The length of a simple cycle is the sum of the lengths of its edges.
Input
The first line contains an integer , the number of test cases. Each test case is given as follows.
The first line contains two integers and (, ), the number of vertices and edges in the original graph.
The next lines each contain two integers and (), the endpoints of the edge with length . There are no self loops and no multiple edges. The graph is not necessarily connected.
The sum of over all test cases and the sum of over all test cases do not exceed .
Output
For each test case, print one line. If the graph has no simple cycle, print -1. Otherwise, print the indices of the edges in the simple cycle with the smallest length, in increasing order, separated by spaces. It can be shown that there is at most one such answer.
Hint
The first test case is shown below. The number beside each edge is its index (outside the parentheses) and its length (inside the parentheses). The simple cycle with the smallest length consists of edges , , , and , with length .
