Under construction

Still being built. Things may change

Internet problem (Hard)

Time limit5sMemory limit1024 MB

Summary
Given a directed graph, list the servers that every route from server 1 to server n passes through exactly once.
Level

Medium7 of 10

Topics
Graph, DFS
Solved
No attempts yet

Problem

Lisa and Sarah have exposed a massive conspiracy and are now on the run from a corrupt government. Being together is too risky because both could be captured, so they communicate over the Internet. The normal Internet is not safe enough, so they send each other secret messages through the dark web.

On the dark web, a message can take a long and convoluted path through many servers before it reaches its destination. The path may even pass through one server multiple times, which makes messages harder to trace.

Lisa is still worried. What if the government has already hacked one of the dark web servers? If the hacked server is in a good central location, it could intercept all of her messages to Sarah, regardless of the path they take.

Help Lisa solve her Internet problem!

The dark web consists of nn servers, numbered from 1 to nn. The servers are connected by mm directed network links. If one server can transmit a message to another, the opposite does not have to be true. Lisa is connected to server 1 and Sarah is connected to server nn. Whenever Lisa sends a message to Sarah, she chooses a route: a sequence of consecutive network links from server 1 to server nn. The route may go through each server multiple times.

The government wants to intercept Lisa's messages to Sarah. They can hack one server so it records all messages that go through it. They want to see every message from Lisa to Sarah exactly once. At least once is needed so they learn all of the plans, and at most once is needed so their hard drives do not fill up with duplicates.

Find all the servers that satisfy this condition.

Input

The first line of the input contains an integer tt, the number of test cases. Each test case is preceded by a blank line.

The first line of each test case contains the integers nn and mm. Each of the next mm lines contains two integers aa and bb (1≤a,b≤n1 \le a, b \le n), meaning that server aa can transmit messages directly to server bb. It may be the case that a=ba = b. Each distinct ordered pair a,ba, b is given at most once.

Output

For each test case, output two lines. On the first line, print the number of servers that could be hacked by the government. On the second line, print the numbers of those servers, separated by spaces, in the order in which Lisa's messages pass through them.

If there is no path from Lisa to Sarah, output an empty set of servers.

Constraints

2≤n≤5000002 \le n \le 500000

0≤m≤10000000 \le m \le 1000000

Hint

First test case: All messages must take the same path, so the government could hack any server on that path.

Second test case: The government does not want any duplicates, so they cannot hack either server. Lisa and Sarah are safe.

Third test case: Lisa cannot send any messages at all, so there is nothing to hack.

Fourth test case: The government cannot hack both 2 and 3 at once. If they hack only 2 or only 3, they will not see all messages.

Examples1

  1. Example 1

    Input
    4
    
    4 3
    2 4
    1 3
    3 2
    
    2 2
    1 2
    2 1
    
    3 1
    2 3
    
    4 4
    1 2
    2 4
    3 4
    1 3
    
    Expected output
    4
    1 3 2 4
    0
    
    0
    
    2
    1 4