-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboj.11438.cc
99 lines (86 loc) · 1.9 KB
/
boj.11438.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, sparse_table[17][100001] = {{0}}, depth[100001] = {0};
void fill0(vector<vector<int>> &edges, int node, int parent, int stage)
{
depth[node] = stage;
for(auto c: edges[node])
{
if(c == parent)
{
continue;
}
sparse_table[0][c] = node;
fill0(edges, c, node, stage + 1);
}
}
int query(int idx, int n)
{
for(int i = 16; i>=0; --i)
{
if(n&(1<<i))
{
idx = sparse_table[i][idx];
}
}
return idx;
}
int find(int a, int b)
{
int depthA = depth[a], depthB = depth[b],
start = 0, end = min(depthA, depthB), mid;
for(mid = (start+end)/2; start<end; mid = (start+end)/2)
{
int _a = query(a, depthA-mid), _b = query(b, depthB-mid);
if(_a == _b)
{
start = mid+1;
}
else
{
end = mid;
}
}
// cout<<start<<" "<<end<<"\n";
int _a = query(a, depthA-mid), _b = query(b, depthB-mid),
result = depthB - start;
if(_a != _b)
{
++result;
}
return query(b, result);
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// 한쪽을 구한 다음 이분 탐색으로 찾기;
cin>>n;
vector<vector<int>> edges(n+1);
for(int i = 0, a, b; i<n-1; ++i)
{
cin>>a>>b;
edges[a].push_back(b);
edges[b].push_back(a);
}
fill0(edges, 1, 0, 0);
for(int step = 1; (1<<step)<=n; ++step)
{
for(int i = 1; i<=n; ++i)
{
sparse_table[step][i] = sparse_table[step-1][
sparse_table[step-1][i]
];
}
}
cin>>m;
while(m--)
{
int a, b;
cin>>a>>b;
cout<<find(a, b)<<"\n";
}
return 0;
}