Skip to content

Commit a397502

Browse files
authored
Merge pull request #557 from AlgorithmWithGod/khj20006
[20250727] BOJ / P5 / 내 왼손에는 흑염룡이 잠들어 있다 / 권혁준
2 parents 6cdb96d + bba4869 commit a397502

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N;
57+
static List<int[]>[] graph;
58+
static int[] down, up;
59+
60+
public static void main(String[] args) throws Exception {
61+
62+
io = new IOController();
63+
64+
init();
65+
solve();
66+
67+
io.close();
68+
69+
}
70+
71+
static void init() throws Exception {
72+
73+
N = io.nextInt();
74+
graph = new ArrayList[N+1];
75+
for(int i=1;i<=N;i++) graph[i] = new ArrayList<>();
76+
down = new int[N+1];
77+
up = new int[N+1];
78+
for(int i=1;i<N;i++) {
79+
int a = io.nextInt();
80+
int b = io.nextInt();
81+
int c = io.nextInt();
82+
graph[a].add(new int[]{b,c});
83+
graph[b].add(new int[]{a,c});
84+
}
85+
86+
}
87+
88+
static void solve() throws Exception {
89+
90+
dfs1(1,0);
91+
dfs2(1,0);
92+
for(int i=1;i<=N;i++) {
93+
io.write(Math.max(up[i], down[i]) + "\n");
94+
}
95+
96+
}
97+
98+
static void dfs1(int n, int p) {
99+
for(int[] e:graph[n]) if(e[0] != p) {
100+
dfs1(e[0],n);
101+
down[n] = Math.max(down[n], down[e[0]] + e[1]);
102+
}
103+
}
104+
105+
static void dfs2(int n, int p) {
106+
int max1 = 0, max2 = 0;
107+
int num1 = 0;
108+
for(int[] e:graph[n]) if(e[0] != p) {
109+
if(down[e[0]] + e[1] > max1) {
110+
max2 = max1;
111+
max1 = down[e[0]] + e[1];
112+
num1 = e[0];
113+
}
114+
else if(down[e[0]] + e[1] > max2) {
115+
max2 = down[e[0]] + e[1];
116+
}
117+
}
118+
for(int[] e:graph[n]) if(e[0] != p) {
119+
if(e[0] == num1) {
120+
up[e[0]] = Math.max(up[n], max2) + e[1];
121+
}
122+
else {
123+
up[e[0]] = Math.max(up[n], max1) + e[1];
124+
}
125+
dfs2(e[0], n);
126+
}
127+
}
128+
129+
}
130+
```

0 commit comments

Comments
 (0)