Visiting Islands
Practice
3.5 (8 votes)
Algorithms
Depth first search
Dynamic programming
Medium
Problem
35% Success 2140 Attempts 30 Points 3s Time Limit 256MB Memory 1024 KB Max Code

Alice likes visiting new places. Now he wants to visit a place called Strange Islands. There are N islands numbered from 0 to \(N - 1\). From each island you can only go to one island. Alice can start on any island and visit others form that. Alice wants to visit as many islands as possible. Help him find the island, from which he starts, can visit maximum islands. If there are multiple such islands find the lowest numbered one.

Input
First line of the input is the number of test cases T. It is followed by T lines. Each test case contains N numbers. \(i\;th\) number denotes the island number to which island i leads to.

Output
For each test case print a single number the required answer.

Constraints
\(1 \le T \le 10 \)
\(1 \le N \le 10^6\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
12 votes
Tags:
AlgorithmsApprovedGraphsMediumOpenShortest Path Algorithm
Points:30
10 votes
Tags:
AlgorithmsApprovedGraphsMedium
Points:30
6 votes
Tags:
AlgorithmsDepth First SearchGraphsMedium