EN - Entrapment
The separatist leader General Grievous, the second in command of Count Dooku, comes to know that Chancellor Palpatine’s convoy, escorted by Obi-wan and Anakin, is scheduled to depart from Kashyyyk in the Middle Rim of the Universe to Alderaan. General Grievous is aware that there are multiple paths going via different sets of planets from Kashyyyk to Alderaan. To make his abduction attempt successful, he decides to send his robots to the planet closest to Kashyyyk, other than itself, which lies on all the possible paths from Kashyyyk to Alderaan. Since you have pledged your allegiance to Count Dooku, you need to help him identify this planet.
The planetary map which is given to you for this purpose consists of a set of one-way connections between planets. You also know that a pair of planets can have at most one connection between them in each direction and there is always a path from Kashyyyk to Alderaan.
Note: In the given example, the planet with index 5 is the required planet.
The first line of the input is a positive integer t ≤ 20, which is the number of test cases. The descriptions of the test cases follow one after the other. The first line of each test case is a pair of positive integers n, m (separated by a single space). 2 ≤ n ≤ 30011 is the number of planets and m ≤ 100011 is the number of connections between planets. The planets are indexed with integers from 1 to n and the indices of Kashyyyk and Alderaan are 1, n respectively. Each of the next m lines contains two integers p,q, meaning that there is a one-way connection from planet p to planet q.
The output should contain t lines. The ith line corresponds to the ith test case. The output for the ith test case should be the index of the planet with the required property.
This problem should be in needed 15 problems (SPOJ). Will recommend to read about algorithms on flow network, if stuck. O(m+n) is way to go..
Nice Problem (:Last edit: 2018-08-02 17:08:11
m ≤ 119407 not m ≤ 100011
test cases are very weak!!! my solution is O(n(n+m)) and still got AC, pls update the cases
José Carlos Gutiérrez:
add c++14 please!!!!!
Hussain Kara Fallah:
done in o(n+m) :D
Weak test cases, my program won't even pass the example given in picture, still got AC
@amaroq is right. I got RE (SIGSEV) with the constraints given in the text.
WARNING: the input contains cases where m>100011!
|Added by:||Kashyap KBR|
|Cluster:||Cube (Intel G860)|
|Languages:||ADA95 ASM32 BASH BF C CSHARP CPP C99 CLPS LISP sbcl LISP clisp D FORTRAN HASK ICON ICK JAVA LUA NEM NICE OCAML PAS-GPC PAS-FPC PERL PHP PIKE PRLG-swi PYTHON RUBY SCM qobi SCM guile ST WHITESPACE|
|Resource:||Fair Isaac Programming Challenge - prelims 2006|