File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Authored by : haneulkimdev
2
+ // Co-authored by : -
3
+ // http://boj.kr/fc8f300d54f94c62a70da57ff910fb71
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+ #define X first
7
+ #define Y second
8
+
9
+ int ans = 0 ;
10
+ int v;
11
+ vector<pair<int , int >> adj[100001 ];
12
+ int p[100001 ];
13
+
14
+ int dfs (int cur) {
15
+ int first = 0 ;
16
+ int second = 0 ;
17
+ for (auto nxt : adj[cur]) {
18
+ if (p[cur] == nxt.X ) continue ;
19
+ p[nxt.X ] = cur;
20
+ int dist = dfs (nxt.X ) + nxt.Y ;
21
+ if (dist > first) {
22
+ second = first;
23
+ first = dist;
24
+ } else if (dist > second) {
25
+ second = dist;
26
+ }
27
+ }
28
+ ans = max (ans, first + second);
29
+ return first;
30
+ }
31
+
32
+ int main (void ) {
33
+ ios::sync_with_stdio (0 );
34
+ cin.tie (0 );
35
+ cin >> v;
36
+ for (int i = 1 ; i <= v; i++) {
37
+ int a, b, c;
38
+ cin >> a;
39
+ while (true ) {
40
+ cin >> b;
41
+ if (b == -1 ) break ;
42
+ cin >> c;
43
+ adj[a].push_back ({b, c});
44
+ }
45
+ }
46
+ dfs (1 );
47
+ cout << ans;
48
+ }
49
+ /*
50
+ 지름은 결국 한 점으로부터 가장 먼 두 자식까지의 거리의 합이 된다.
51
+ dfs(x)는 x를 root로 하는 서브트리에서 가장 먼 자식까지의 거리를 반환한다.
52
+ */
You can’t perform that action at this time.
0 commit comments