Find the Hacker
All submissions for this problem are available.
In today's digital era everyone is being hacked by someone, but there are a few who are untouchables/unhackable and are considered Master Hackers.
Your task is to find these Master Hackers in the hacker community. A Master Hacker is the one who is hacking everyone else either directly or indirectly,
1. A --> B, A hacks B 2. B --> C, B hacks C ==> A --> C, A is indirectly hacking C.
A hacker can hack the same person in more than 1 way.
- The first Line contains T, the no. of test cases.
- Each Test case is as follow, first line contains two space seperated integers N and M, N specifying the no. of people in the community numbered 1...N, and M is the number of people who are being hacked by someone.
- M lines follow each line consists of two space seperated integers H Q, where H-Hacker, Q-Target which is being hacked by H.
- You need to print the index of the master hacker, if more than one exists you can output any one of them, if no such master hacker is present output -1.
Should contain all the constraints on the input data that you may have. Format it like:
- 1 ≤ T ≤ 3
- 1 ≤ N ≤ 105
- 1 ≤ M ≤ min(2 * 105, N * (N - 1) / 2)
Input: 2 3 2 1 2 2 3 5 7 2 1 1 3 2 3 2 4 4 5 5 1 5 3 Output: 1 2
|Time Limit:||2 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.