-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImperialism.cpp
More file actions
64 lines (46 loc) · 1.13 KB
/
Copy pathImperialism.cpp
File metadata and controls
64 lines (46 loc) · 1.13 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
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > matriz;
vector<int> visitado;
int nododmax, dmax;
void dfs(int n){
if(visitado[n] > dmax){
dmax = visitado[n];
nododmax = n;
}
for(int i = 0; i < matriz[n].size(); i ++){
int ady = matriz[n][i];
if(visitado[ady] == -1){
visitado[ady] = visitado[n] + 1;
dfs(ady);
}
}
return;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int nodos, a;
while(cin >> nodos && nodos != -1){
matriz.clear();
visitado.clear();
matriz.resize(nodos + 1);
visitado.resize(nodos + 1, -1);
for(int i = 2; i <= nodos; i ++){
cin >> a;
matriz[i].push_back(a);
matriz[a].push_back(i);
}
visitado[1] = 0;
nododmax = 1;
dmax = 0;
dfs(1);
fill(visitado.begin(), visitado.end(), -1);
visitado[nododmax] = 0;
dfs(nododmax);
if(dmax % 2 == 1) cout << dmax / 2 + 1 << "\n";
else cout << dmax / 2 << "\n";
}
return 0;
}