1
- // Authored by : BaaaaaaaaaaarkingDog
1
+ // Authored by : heheHwang
2
2
// Co-authored by : -
3
- // http://boj.kr/****************
3
+ // http://boj.kr/d6c7ee9ea1a64b95885c8858ddbf1404
4
4
#include < bits/stdc++.h>
5
5
using namespace std ;
6
6
7
- int main (void ){
7
+ const int MXN = 10010 ;
8
+ int lc[MXN], rc[MXN], N, colno, root;
9
+ pair<int , int > depth[MXN];
10
+ void inorder (int curr, int d) {
11
+ if (curr == -1 ) return ;
12
+ inorder (lc[curr], d + 1 );
13
+ colno++;
14
+ if (!depth[d].first || colno < depth[d].first )
15
+ depth[d].first = colno;
16
+ if (!depth[d].second || depth[d].second < colno)
17
+ depth[d].second = colno;
18
+ inorder (rc[curr], d + 1 );
19
+ }
20
+ int main (void ) {
8
21
ios::sync_with_stdio (0 );
9
22
cin.tie (0 );
10
-
23
+
24
+ cin >> N;
25
+ vector<bool > isRoot (N + 1 , true );
26
+ for (int i = 0 ; i < N; i++) {
27
+ int p, l, r;
28
+ cin >> p >> l >> r;
29
+ lc[p] = l;
30
+ rc[p] = r;
31
+
32
+ if (l != -1 ) isRoot[l] = false ;
33
+ if (r != -1 ) isRoot[r] = false ;
34
+ }
35
+ for (int i = 1 ; i <= N; i++)
36
+ if (isRoot[i]) {
37
+ root = i;
38
+ break ;
39
+ }
40
+ int mxWidth = 0 , mxDepth = 0 ;
41
+ inorder (root, 0 );
42
+ for (int d = 0 ; d < N; d++) {
43
+ auto [l, r] = depth[d];
44
+ if (l + r == 0 ) break ;
45
+ int width = r - l + 1 ;
46
+ if (mxWidth < width) {
47
+ mxWidth = width;
48
+ mxDepth = d;
49
+ }
50
+ }
51
+ cout << mxDepth + 1 << ' ' << mxWidth;
11
52
}
0 commit comments