8000 Merge pull request #319 from hehehwang/0x19-2250 · dongju97/basic-algo-lecture@6cea5d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cea5d5

Browse files
Merge pull request encrypted-def#319 from hehehwang/0x19-2250
0x19 트리 Update 2250
2 parents 257a8f7 + d624964 commit 6cea5d5

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

0x19/solutions/2250.cpp

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
1-
// Authored by : BaaaaaaaaaaarkingDog
1+
// Authored by : heheHwang
22
// Co-authored by : -
3-
// http://boj.kr/****************
3+
// http://boj.kr/250280ac422d4be8a7bbc04ad75f695f
44
#include <bits/stdc++.h>
55
using namespace std;
66

7-
int main(void){
7+
const int MXN = 10010;
8+
int lc[MXN], rc[MXN], N, colno, root;
9+
pair<int, int> colLR[MXN];
10+
void inorder(int curr, int d) {
11+
if (curr == -1) return;
12+
inorder(lc[curr], d + 1);
13+
colno++;
14+
auto &[lcol, rcol] = colLR[d];
15+
if (!lcol || colno < lcol) lcol = colno;
16+
if (!rcol || rcol < colno) rcol = colno;
17+
inorder(rc[curr], d + 1);
18+
}
19+
int main(void) {
820
ios::sync_with_stdio(0);
921
cin.tie(0);
10-
11-
}
22+
23+
cin >> N;
24+
vector<bool> isRoot(N + 1, true);
25+
for (int i = 0; i < N; i++) {
26+
int p, l, r;
27+
cin >> p >> l >> r;
28+
lc[p] = l;
29+
rc[p] = r;
30+
31+
if (l != -1) isRoot[l] = false;
32+
if (r != -1) isRoot[r] = false;
33+
}
34+
for (int i = 1; i <= N; i++)
35+
if (isRoot[i]) {
36+
root = i;
37+
break;
38+
}
39+
int mxWidth = 0, mxDepth = 0;
40+
inorder(root, 0);
41+
for (int d = 0; d < N; d++) {
42+
auto [lcol, rcol] = colLR[d];
43+
if (lcol + rcol == 0) break;
44+
int width = rcol - lcol + 1;
45+
if (mxWidth < width) {
46+
mxWidth = width;
47+
mxDepth = d;
48+
}
49+
}
50+
cout << mxDepth + 1 << ' ' << mxWidth;
51+
}
52+
/*
53+
규칙을 살펴보면, 노드의 왼쪽, 노드 자신, 노드의 오른쪽 순으로
54+
열을 채워나가는데, 이는 중위순회(inorder)에 해당합니다.
55+
56+
따라서 먼저 루트 노드를 찾은 뒤, 루트에서부터 중위순회를 돌며
57+
왼쪽부터 열번호를 부여하고, 해당 깊이의 열번호의
58+
최소(left)와 최대(right)를 갱신합니다.
59+
마지막으로 깊이마다 너비를 측정해서 최대 너비를 반환하면 됩니다.
60+
*/

0 commit comments

Comments
 (0)
0