Skip to content

Commit c6f83bc

Browse files
authored
[20250815] BOJ / D5 / 정기 모임 / 권혁준
1 parent 544aefc commit c6f83bc

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
# C++
2+
```cpp
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
int N, Q;
7+
vector<vector<pair<int, int>>> v(300001);
8+
int par[300001][19]{}, mx[300001][19]{}, dep[300001]{};
9+
int lca[1048576]{}, dist[1048576]{};
10+
11+
void dfs(int n, int p, int d) {
12+
par[n][0] = p, dep[n] = d;
13+
for(auto [i,c]:v[n]) if(i != p) {
14+
mx[i][0] = c;
15+
dfs(i,n,d+1);
16+
}
17+
}
18+
19+
pair<int,int> mrg(pair<int,int> A, pair<int,int> B) {
20+
if(A.first == -1) return B;
21+
if(B.first == -1) return A;
22+
int a = A.first, b = B.first, res = max(A.second, B.second);
23+
int diff = abs(dep[a]-dep[b]);
24+
for(int k=0;k<19;k++) if(diff & (1<<k)) {
25+
if(dep[a] > dep[b]) res = max(res, mx[a][k]), a = par[a][k];
26+
else res = max(res, mx[b][k]), b = par[b][k];
27+
}
28+
for(int k=18;k>=0;k--) if(par[a][k] != par[b][k]) {
29+
res = max({res, mx[a][k], mx[b][k]});
30+
a = par[a][k], b = par[b][k];
31+
}
32+
if(a != b) res = max({res, mx[a][0], mx[b][0]}), a = par[a][0];
33+
return {a, res};
34+
}
35+
36+
void init(int s, int e, int n) {
37+
if(s == e) {
38+
lca[n] = s;
39+
return;
40+
}
41+
int m = (s+e)>>1;
42+
init(s,m,n*2); init(m+1,e,n*2+1);
43+
auto [a, b] = mrg({lca[n*2], dist[n*2]}, {lca[n*2+1], dist[n*2+1]});
44+
lca[n] = a, dist[n] = b;
45+
}
46+
47+
pair<int,int> find(int s, int e, int l, int r, int n) {
48+
if(l>r || l>e || r<s) return {-1,0};
49+
if(l<=s && e<=r) return {lca[n], dist[n]};
50+
int m = (s+e)>>1;
51+
auto a = find(s,m,l,r,n*2);
52+
auto b = find(m+1,e,l,r,n*2+1);
53+
return mrg(a,b);
54+
}
55+
56+
int main(){
57+
cin.tie(0)->sync_with_stdio(0);
58+
59+
cin>>N>>Q;
60+
for(int i=1,a,b,c;i<N;i++) {
61+
cin>>a>>b>>c;
62+
v[a].emplace_back(b,c);
63+
v[b].emplace_back(a,c);
64+
}
65+
66+
dfs(1,0,0);
67+
for(int k=1;k<19;k++) for(int i=1;i<=N;i++) {
68+
par[i][k] = par[par[i][k-1]][k-1];
69+
mx[i][k] = max(mx[i][k-1], mx[par[i][k-1]][k-1]);
70+
}
71+
init(1,N,1);
72+
for(int a,b;Q--;cout<<find(1,N,a,b,1).second<<'\n') cin>>a>>b;
73+
74+
}
75+
```
76+
77+
# Java
78+
```java
79+
import java.util.*;
80+
import java.io.*;
81+
82+
class IOController {
83+
BufferedReader br;
84+
BufferedWriter bw;
85+
StringTokenizer st;
86+
87+
public IOController() {
88+
br = new BufferedReader(new InputStreamReader(System.in));
89+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
90+
st = new StringTokenizer("");
91+
}
92+
93+
String nextLine() throws Exception {
94+
String line = br.readLine();
95+
st = new StringTokenizer(line);
96+
return line;
97+
}
98+
99+
String nextToken() throws Exception {
100+
while (!st.hasMoreTokens()) nextLine();
101+
return st.nextToken();
102+
}
103+
104+
int nextInt() throws Exception {
105+
return Integer.parseInt(nextToken());
106+
}
107+
108+
long nextLong() throws Exception {
109+
return Long.parseLong(nextToken());
110+
}
111+
112+
double nextDouble() throws Exception {
113+
return Double.parseDouble(nextToken());
114+
}
115+
116+
void close() throws Exception {
117+
bw.flush();
118+
bw.close();
119+
}
120+
121+
void write(String content) throws Exception {
122+
bw.write(content);
123+
}
124+
125+
}
126+
127+
public class Main {
128+
129+
static IOController io;
130+
131+
//
132+
133+
static int N, Q;
134+
static List<int[]>[] graph;
135+
static int[][] par, max;
136+
static int[] dep;
137+
138+
static int[][] seg;
139+
140+
public static void main(String[] args) throws Exception {
141+
142+
io = new IOController();
143+
144+
init();
145+
solve();
146+
147+
io.close();
148+
149+
}
150+
151+
static void init() throws Exception {
152+
153+
N = io.nextInt();
154+
Q = io.nextInt();
155+
graph = new List[N+1];
156+
for(int i=1;i<=N;i++) graph[i] = new ArrayList<>();
157+
for(int i=1;i<N;i++) {
158+
int a = io.nextInt();
159+
int b = io.nextInt();
160+
int c = io.nextInt();
161+
graph[a].add(new int[]{b,c});
162+
graph[b].add(new int[]{a,c});
163+
}
164+
par = new int[N+1][19];
165+
max = new int[N+1][19];
166+
dep = new int[N+1];
167+
seg = new int[4*N][2];
168+
169+
}
170+
171+
static void solve() throws Exception {
172+
173+
dfs(1,0,0);
174+
for(int k=1;k<19;k++) for(int i=1;i<=N;i++) {
175+
par[i][k] = par[par[i][k-1]][k-1];
176+
max[i][k] = Math.max(max[i][k-1], max[par[i][k-1]][k-1]);
177+
}
178+
179+
initSeg(1,N,1);
180+
181+
for(int a, b;Q-->0;) {
182+
a = io.nextInt();
183+
b = io.nextInt();
184+
io.write(find(1,N,a,b,1)[1] + "\n");
185+
}
186+
187+
}
188+
189+
static void dfs(int n, int p, int d) {
190+
par[n][0] = p;
191+
dep[n] = d;
192+
for(int[] e:graph[n]) if(e[0] != p) {
193+
max[e[0]][0] = e[1];
194+
dfs(e[0],n,d+1);
195+
}
196+
}
197+
198+
static int[] lca(int[] A, int[] B) {
199+
if(A[0] == -1) return B;
200+
if(B[0] == -1) return A;
201+
202+
int a = A[0], b = B[0];
203+
int res = Math.max(A[1], B[1]);
204+
205+
int diff = Math.abs(dep[a]-dep[b]);
206+
for(int k=0;k<19;k++) if((diff & (1<<k)) != 0) {
207+
if(dep[a] > dep[b]) {
208+
res = Math.max(res, max[a][k]);
209+
a = par[a][k];
210+
}
211+
else {
212+
res = Math.max(res, max[b][k]);
213+
b = par[b][k];
214+
}
215+
}
216+
for(int k=18;k>=0;k--) if(par[a][k] != par[b][k]) {
217+
res = Math.max(res, Math.max(max[a][k], max[b][k]));
218+
a = par[a][k];
219+
b = par[b][k];
220+
}
221+
if(a != b) {
222+
res = Math.max(res, Math.max(max[a][0], max[b][0]));
223+
a = par[a][0];
224+
}
225+
return new int[]{a, res};
226+
}
227+
228+
static void initSeg(int s, int e, int n) {
229+
if(s == e) {
230+
seg[n][0] = s;
231+
return;
232+
}
233+
int m = (s+e)>>1;
234+
initSeg(s,m,n*2);
235+
initSeg(m+1,e,n*2+1);
236+
seg[n] = lca(seg[n*2], seg[n*2+1]);
237+
}
238+
239+
static int[] find(int s, int e, int l, int r, int n) {
240+
if(l>r || l>e || r<s) return new int[]{-1,0};
241+
if(l<=s && e<=r) return seg[n];
242+
int m = (s+e)>>1;
243+
return lca(find(s,m,l,r,n*2), find(m+1,e,l,r,n*2+1));
244+
}
245+
246+
}
247+
```

0 commit comments

Comments
 (0)