-
Notifications
You must be signed in to change notification settings - Fork 0
/
2250.cpp
54 lines (44 loc) · 997 Bytes
/
2250.cpp
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
#include<iostream>
#include<algorithm>
#include<vector>
#define N 10005
using namespace std;
int l[N], r[N], num, max_lev, ll[N], rr[N], check[N];
void dfs(int index, int level)
{
max_lev=max(max_lev, level);
if(l[index]) dfs(l[index], level+1);
if(ll[level]==0) ll[level]=++num;
else rr[level]=++num;
if(r[index]) dfs(r[index], level+1);
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n, i, root;
cin >> n;
for(i=0; i<n; i++){
int a, b, c;
cin >> a >> b >> c;
if(b!=-1) l[a]=b, check[b]=1;
if(c!=-1) r[a]=c, check[c]=1;
}
for(i=1; i<=n; i++){
if(!check[i]){
root=i;
break;
}
}
dfs(root, 1);
int ans=0, ansi;
for(i=1; i<=max_lev; i++){
if(!rr[i]) rr[i]=ll[i];
if(ans<rr[i]-ll[i]+1){
ans=rr[i]-ll[i]+1;
ansi=i;
}
}
cout << ansi << " " << ans;
return 0;
}