Skip to content

Commit e829eb4

Browse files
authored
Merge pull request #1082 from AlgorithmWithGod/khj20006
[20251010] BOJ / P1 / Pictionary / 권혁준
2 parents 5aee126 + fadc767 commit e829eb4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int N, M, Q, r[100001]{}, dep[100001]{}, par[100001][17]{}, mx[100001][17]{};
6+
vector<vector<pair<int, int>>> v(100001);
7+
8+
int f(int x) {return x==r[x] ? x : r[x]=f(r[x]);}
9+
10+
void dfs(int n, int p, int d) {
11+
par[n][0] = p, dep[n] = d;
12+
for(auto [i,c]:v[n]) if(i != p) {
13+
mx[i][0] = c;
14+
dfs(i,n,d+1);
15+
}
16+
}
17+
18+
int main(){
19+
cin.tie(0)->sync_with_stdio(0);
20+
21+
cin>>N>>M>>Q;
22+
iota(r, r+N+1, 0);
23+
for(int g=M,i=1;g>0;g--,i++) {
24+
for(int j=g;j+g<=N;j+=g) {
25+
int x = f(j), y = f(j+g);
26+
if(x == y) continue;
27+
r[x] = y;
28+
v[j].emplace_back(j+g,i);
29+
v[j+g].emplace_back(j,i);
30+
}
31+
}
32+
33+
dfs(1,0,0);
34+
for(int k=1;k<17;k++) for(int i=1;i<=N;i++) {
35+
par[i][k] = par[par[i][k-1]][k-1];
36+
mx[i][k] = max(mx[i][k-1], mx[par[i][k-1]][k-1]);
37+
}
38+
39+
for(int a,b;Q--;) {
40+
cin>>a>>b;
41+
int ans = 1, diff = abs(dep[a] - dep[b]);
42+
for(int k=0;k<17;k++) if(diff & (1<<k)) {
43+
if(dep[a] > dep[b]) ans = max(ans, mx[a][k]), a = par[a][k];
44+
else ans = max(ans, mx[b][k]), b = par[b][k];
45+
}
46+
for(int k=16;k>=0;k--) if(par[a][k] != par[b][k]) {
47+
ans = max({ans, mx[a][k], mx[b][k]});
48+
a = par[a][k], b = par[b][k];
49+
}
50+
if(a != b) ans = max({ans, mx[a][0], mx[b][0]});
51+
cout<<ans<<'\n';
52+
}
53+
54+
}
55+
```

0 commit comments

Comments
 (0)