# Generating Cycles solution codechef

## Generating Cycles solution codechef

A vertex-induced subgraph of a simple graph GG is one that can be obtained from GG by removing some (possibly, none) vertices. When a vertex is removed, all edges incident to it must also be removed.

A vertex-induced cycle is a vertex-induced subgraph that is also a cycle.

Equivalently, a cycle CC is vertex induced, if for every edge ee in the graph GG that does not belong to CCat most one of its ends lies on CC.

You are given a simple undirected graph GG with NN vertices and MM edges. Each edge has a label that is either 00 or 11.

In one move, you can select any vertex-induced cycle in GG and flip the values of the label in all edges of the cycle, (i.e. change every 00 to a 11 and every 11 to a 00). The cost of one such move is equal to the size (i.e. number of edges) of the cycle. The total cost for a sequence of moves is the sum of the costs of each individual move.

Determine if there exists a sequence of moves after which all edges have label 00. If such a sequence exists, then find a sequence of moves whose total cost does not exceed 3k3⋅k, where kk is the number of edges in GG that have label 11 before any moves are done; otherwise, print 1−1.

It’s guaranteed that if an answer exists, there exists a sequence of moves with a total cost not exceeding 3k3⋅k.

Note: Test data contains large input files. It is recommended that you use fast input/output methods.

### Input Format

• The first line of input contains an integer TT, the number of test cases. The description of the TT test cases follows.
• The first line of each test case contains two space-separated integers N,MN,M — the number of vertices and edges of the graph respectively.
• The next MM lines of each test case contain three space-separated integers u,vu,v, and ww, denoting an edge between vertices u,vu,v with label ww.

### Output Format

• For each test case, print the answer starting from a new line in the following format:
• If it is impossible to convert all edge labels to 00 as per the statement, then print 1−1.
• Otherwise, in the first line print the number of operations that you require to perform, (say xx).
• In the ii-th of the next xx lines, print the number of vertices of the cycle you choose in the ii-th move followed by the vertices of the cycle. The vertices must be printed in either clockwise or anticlockwise order. That is, print s x1 x2  xss x1 x2 … xs to denote a cycle of size ss whose edges are (x1,x2),(x2,x3),,(xs1,xs),(xs,x1)(x1,x2),(x2,x3),…,(xs−1,xs),(xs,x1).
• If there are multiple correct outputs, you may print any of them.

### Constraints

• 1T1031≤T≤103
• 1N1031≤N≤103
• 1MN(N1)21≤M≤N(N−1)2
• The sum of NN over all test cases does not exceed 20002000
• The sum of kk over all test cases does not exceed 50005000
• 1u,vn1≤u,v≤n
• The given graph is a simple graph, i.e. there are no self loops or multiple edges

Subtask 1 (100 pts): Original constraints

### Sample Input 1

4
6 9
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 1 1
1 3 1
3 5 1
5 1 1
6 9
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 1 1
1 3 0
3 5 0
5 1 0
2 1
1 2 0
2 1
1 2 1


### Sample Output 1

5
3 3 4 5
3 1 2 3
3 1 3 5
3 1 5 6
3 1 3 5
4
3 3 4 5
3 1 2 3
3 1 3 5
3 1 5 6
0
-1


### Explanation

Test Case 1: In this example N=6,M=9N=6,M=9 and k=9k=9. The total cost of the sequence of moves given is 1515. The graph and the sequence of moves are shown below.

Note that any solution for which the total cost is no more than 3k=273⋅k=27 is also considered correct. For example another solution is shown below with total cost equal to 99.

Test Case 2: The graph is same as the previous testcase, except that the edge labels are different (k=6k=6). After the first move, the resulting graph is identical to the first case. The total cost is 1212 which is less than 3k=183⋅k=18. The solution for this case is shown below.

Note that the cycle formed by vertices 1,2,3,4,5,61,2,3,4,5,6 is not a vertex induced cycle.

Test Case 3: The graph does not have any edge with label 11, so no operations are required.

Test Case 4: The graph does not contain any cycles, and therefore it is impossible to flip the label of any edge (i.e. no valid move exists).