Submission #731776

#TimeUsernameProblemLanguageResultExecution timeMemory
731776TrunktyForensic (NOI12_forensic)C++14
10 / 25
8 ms2900 KiB
#include <bits/extc++.h> using namespace std; typedef long long ll; #define int ll int n; int arr[20005]; bool vis[20005]; map<int,vector<int>> roads; int dfs(int x){ if(vis[x]){ return -1; } int ret=0; for(int i:roads[x]){ ret = max(ret,dfs(i)); } return ret+1; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i=0;i<n;i++){ cin >> arr[i]; roads[arr[i]].push_back(i); } int cnt=0,curr=0; while(1){ if(curr==-1 or vis[curr]){ break; } cnt++; vis[curr] = true; curr = arr[curr]; } if(curr==-1){ curr = 0; int maxi=0; while(1){ for(int i:roads[curr]){ maxi = max(maxi,dfs(i)); } if(curr==-1){ break; } curr = arr[curr]; } cout << cnt+maxi << "\n"; } else{ cout << cnt+dfs(-1)-1LL << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...