-
Notifications
You must be signed in to change notification settings - Fork 2
/
prob2250.cpp
85 lines (71 loc) · 1.16 KB
/
prob2250.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <algorithm>
#define num 100001
using namespace std;
class Node {
public:
int left;
int right;
int order;
int depth;
};
Node a[num];
int lef[num];
int rig[num];
int cnt[num];
int order = 0;
void inorder(int node, int depth)
{
if (node == -1) return;
inorder(a[node].left, depth + 1);
a[node].order = ++order;
a[node].depth = depth;
inorder(a[node].right, depth + 1);
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x, y, z;
cin >> x >> y >> z;
a[x].left = y;
a[x].right = z;
if (y != -1)
cnt[y]++;
if (z != -1)
cnt[z]++;
}
int root = 0;
for (int j = 1; j <= n; j++)
{
if (cnt[j] == 0)
root = j;
}
inorder(root, 1);
int dep = 0;
for (int i = 1; i <= n; i++)
{
int depth = a[i].depth;
int order = a[i].order;
if (lef[depth] == 0)
lef[depth] = order;
else
lef[depth] = min(lef[depth], order);
rig[depth] = max(rig[depth], order);
dep = max(dep, depth);
}
int ans = 0;
int ans_level = 0;
for (int i = 1; i <= dep; i++)
{
if(ans < rig[i] - lef[i] + 1)
{
ans = rig[i] - lef[i] + 1;
ans_level = i;
}
}
cout << ans_level << ' ' << ans << '\n';
return 0;
}