Single Point of Failure
All submissions for this problem are available.### Read problem statements in [Hindi](http://www.codechef.com/download/translated/NOV19/hindi/FAILURE.pdf), [Bengali](http://www.codechef.com/download/translated/NOV19/bengali/FAILURE.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/NOV19/mandarin/FAILURE.pdf), [Russian](http://www.codechef.com/download/translated/NOV19/russian/FAILURE.pdf), and [Vietnamese](http://www.codechef.com/download/translated/NOV19/vietnamese/FAILURE.pdf) as well. Chef has always dreamed of becoming an employee of the "Hack and Manipulate Group". Their work is simple ― attacking networks. They gave Chef the following task to assess his programming skills, but Chef is too busy cooking "Vegetable Gourmet". Can you solve the task for him? A network consists of $N$ nodes (numbered $1$ through $N$) and $M$ bidirectional connections between pairs of these nodes. A *robust network* is a network that contains a cycle. Shutting down a node means removing that node (and all direct connections it is part of) from the network. You may shut down at most one node. If the network was robust before it was shut down and is not robust afterwards, this node is called the *point of failure*. Is the initial network robust? If it is robust, does it have a point of failure? If it does, find the point of failure with the smallest number. ### Input - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains two space-separated integers $N$ and $M$. - Each of the following $M$ lines contains two space-separated integers $u$ and $v$ denoting that nodes $u$ and $v$ are directly connected. ### Output For each test case, print a single line containing one integer ― the smallest number of a point of failure, or $-1$ if the network does not have a point of failure. ### Constraints - $1 \le T \le 10$ - $1 \le N \le 10^5$ - $0 \le M \le 10^5$ - $1 \le u, v \le N$ ### Subtasks **Subtask #1 (10 points):** - $N \le 100$ - $N - 1 \le M \le N + 1$ - the given graph is connected **Subtask #2 (10 points):** $1 \le N \le 100$ **Subtask #3 (30 points):** $1 \le N \le 1,000$ **Subtask #4 (50 points):** original constraints ### Example Input ``` 5 5 5 5 1 5 2 1 2 2 3 2 4 5 6 4 5 4 1 4 2 4 3 5 1 5 2 5 5 3 4 3 5 3 1 3 2 4 2 4 1 3 4 6 6 1 2 2 3 3 1 4 5 5 6 6 4 ``` ### Example Output ``` 1 4 2 -1 -1 ```
|Tags||challenge, dfs, erfaniaa, graphs, nov19, watcher|
|Time Limit:||2 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, 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, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.