#K36962. Largest Friendship Cycle
Largest Friendship Cycle
Largest Friendship Cycle
You are given a network of n users and a list of friend assignments. The users are labelled from 1 to n. For each user i, you are given an integer friends[i] which represents the friend of user i. This forms a directed edge from user i to user friends[i].
A friendship cycle is a sequence of distinct users \( u_1, u_2, \ldots, u_k \) such that \( friends[u_1] = u_2, friends[u_2] = u_3, \ldots, friends[u_{k-1}] = u_k, \) and \( friends[u_k] = u_1 \). Your task is to find the length of the largest friendship cycle in the network.
Note: It is guaranteed that each user has exactly one friend. You can assume \(1 \leq n \leq 10^5\) and for all valid i, \(1 \leq friends[i] \leq n\).
inputFormat
The input is given from standard input (stdin) and consists of two lines:
- The first line contains an integer n, the number of users.
- The second line contains n space-separated integers. The i-th integer represents friends[i], the friend of user i.
outputFormat
Output a single integer to standard output (stdout), which is the length of the largest friendship cycle in the network.
## sample5
2 3 4 5 1
5
</p>