-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path115A.cpp
More file actions
56 lines (49 loc) · 1.21 KB
/
115A.cpp
File metadata and controls
56 lines (49 loc) · 1.21 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fo(i,n) for(int i = 0; i < n; i++)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
int niveis = 0;
int bfs(vector<vector<int>>&grafo,vector<bool>&vis,int v){
int max_prof=0;
queue<pair<int,int>>fila;
fila.push(mp(v,1));
vis[v]=true;
while(!fila.empty()){
auto [atual,nivel] = fila.front();
fila.pop();
max_prof = max(max_prof,nivel);
for(int u:grafo[atual]){
if(!vis[u]){
vis[u]=true;
fila.push(mp(u,nivel+1));
}
}
}
return max_prof;
}
int main() {_
int n; cin >> n;
vector<vector<int>> grafo(n + 1);
vector<bool> vis(n + 1, false);
vector<int> chefes;
fo(i, n) {
int x; cin >> x;
if (x != -1) {
grafo[i + 1].push_back(x);
grafo[x].push_back(i + 1);
} else {
chefes.push_back(i + 1);
}
}
for(int chefe : chefes){
if(!vis[chefe]){
niveis = max(niveis, bfs(grafo, vis,chefe));
}
}
cout << niveis << endl;
return 0;
}