AMR12B  Gandalf vs the Balrog
'We fought far under the living earth, where time is not counted. Ever he clutched me, and ever I hewed him, till at last he fled into dark tunnels. Ever up now we went, until we came to the Endless Stair. Out he sprang, and even as I came behind, he burst into new flame. Those that looked up from afar thought that the mountain was crowned with storm. Thunder they heard, and lightning, they said, smote upon Celebdil, and leaped back broken into tongues of fire.'  Gandalf, describing his fight against the Balrog.
Although Gandalf would not go into the details of his battle, they can be summarized into the following simplified form: both Gandalf and the Balrog have a set of N attacks they can use (spells, swords, bruteforce strength etc.). These attacks are numbered from 1 to N in increasing order of Power. When each has chosen an attack, in general, the one with the higher power wins. However, there are a few ("M") anomalous pairs of attacks, in which the lesserpowered attack wins.
Initially, Gandalf picks an attack. Then the Balrog counters it with one of the remaining attacks. If the Balrog's counter does not defeat Gandalf's, then we say Gandalf receives a score of 2. If however it does, then Gandalf has exactly one more opportunity to pick an attack that will defeat the Balrog's. If he manages to defeat him now, his score will be 1, whereas if he is still unable to defeat him, his score will be 0.
Your task is to determine, given N and the M anomalous pairs of attacks, what will be Gandalf's score, given that both play optimally. Further, in case Gandalf gets a score of 2, you must also determine which attack he could have chosen as his first choice.
Note 1: The Balrog can choose only one attack, whereas Gandalf can choose upto two.
Note 2: The relation A defeats B is not transitive within the attacks. For example, attack A can defeat attack B, attack B can defeat attack C, and attack C can defeat attack A.
Note 3: Between any two attacks A and B, either attack A defeats attack B or attack B defeats attack A.
Input (STDIN):
The first line will consist of the integer T, the number of testcases.
Each test case begins with a single line containing two integers N and M.
This is followed by M lines consisting of 2 integers each x and y, denoting that x and y are an anomalous pair.
Output (STDOUT):
For each testcase, output a single line either
2 A, if Gandalf can defeat any attack the Balrog chooses if he picks attack A,
1, if Gandalf can choose an attack such that even if the Balrog chooses an attack to defeat him, he can choose an attack to defeat the Balrog's chosen card,
0, if none of the above two options are possible for all possible choices of Gandalf's attack(s).
Between successive test cases, there should not be any blank lines in the output.
Constraints:
1 <= T <= 15
3 <= N <= 1,000,000
0 <= M <= min(N(N1)/2, 300,000)
1 <= x < y <= N for all the anomalous pairs (x,y)
The sum of M over all testcases will not exceed 300,000.
Sample Input:
2
3 0
3 1
1 3
Sample Output:
2 3
1
Notes/Explanation of Sample Input:
In the first case, attack 3 can beat both attacks 1 and 2. So Gandalf just chooses attack 3.
In the second case, attack 1 beats 3 which beats 2 which beats 1. No matter which attack Gandalf chooses, the Balrog can pick the one which defeats his, but then he can pick the remaining attack and defeat the Balrog's.
hide comments
Fanghua Ye:
20130718 12:41:44
Well,0 is impossible! 

tuhin:
20130530 11:12:35
i guess its never zero . because if it cant win in first turn ,it should always win in second turn . wasted too much time contemplating on it 

Kevin Sebastian:
20130305 13:01:33
please give a testcase where answer is 0 

Archit Mittal:
20130126 15:40:50
nice one... 

AC Srinivas:
20130105 09:25:35
@Flawless : yes 
Added by:  Varun Jalan 
Date:  20121222 
Time limit:  2s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: ASM64 
Resource:  Pradeep George Mathias  ICPC Asia regionals, Amritapuri 2012 