hiho week 271 register

Ended

Participants:68

Verdict:Accepted
Score:100 / 100
Submitted:2019-09-08 20:25:43

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int vis[100005];
int main()
{
    int n;
    cin >> n;
    memset(vis,0,sizeof(vis));
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    queue<int>q;
    int ans = 0,cnt=0;
    for(int i = 1; i <= n; i++)
    {
        if(vis[i])  continue;
        while(!q.empty()) q.pop();
        cnt = 0;
        q.push(a[i]);
        while(!q.empty())
        {
            int s = q.front(); q.pop();
            if(vis[s])
                break;
            vis[s]=1;
            cnt++;
            q.push(a[s]);
        }
        ans = max(cnt,ans);
    }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX