#P5870. [SEERC 2018] Modern Djinn

[SEERC 2018] Modern Djinn

Description

You are not a leader; you are a president. Luckily, you have a djinn that can grant your wishes. One of your wishes is to pretend that your society has democracy.

The society is simple. There are NN people in the society, numbered from 11 to NN. Some people are “happy”, while others are ordinary (“unhappy”). Humans are very strange: people feel happy only when others are unhappy. There are MM wishes in total, numbered from 11 to MM. XYX \rightarrow Y means that XX wants YY to be unhappy. A person XX is happy if and only if at least one of their wishes is fulfilled.

Democracy is not that complicated either. Some say that to achieve democracy, you need at least half of the people to be happy (or half of the wishes to be fulfilled), but that is not entirely true. As I said, you are a good president, not a good leader. You can define democracy through the media. Therefore, among all MM wishes, you decide to fulfill at least M/4+1\lfloor M/4 \rfloor + 1 wishes.

The remaining task is to choose which wishes you want to fulfill, and then the djinn will take care of everything.

Input Format

The input contains multiple test cases. The first line contains an integer TT, the number of test cases. The following lines give the test cases in order.

For each test case, the first line contains two positive integers NN and MM, representing the number of people in the society and the number of wishes. The next MM lines each contain two integers X,YX, Y, describing a wish: XX wants YY to be unhappy.

Output Format

For each test case, output an integer KK on the first line, the number of wishes to be fulfilled. Output KK integers on the second line, the indices of the fulfilled wishes. The order does not matter.

2
3 3
1 2
2 3
3 1
4 4
1 2
2 3
3 4
1 4
1
2
2
1 4

Hint

Constraints

  • 1T10,0001 \leq T \leq 10,000
  • 2N100,0002 \leq N \leq 100,000
  • 1M200,0001 \leq M \leq 200,000
  • There is no wish where XX wants XX to be unhappy.
  • There may be multiple identical wishes where XX wants YY to be unhappy.
  • For each test case, a solution is guaranteed to exist.
  • Any correct solution is accepted.

Sample Explanation

In the first test case, we can fulfill at most 11 wish; outputting any wish is valid.

In the second test case, another valid solution is to fulfill wishes 11, 33, and 44. At least 22 wishes must be fulfilled.

Translated by ChatGPT 5