-
Notifications
You must be signed in to change notification settings - Fork 0
/
1761.cpp
68 lines (54 loc) · 1.45 KB
/
1761.cpp
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
#include<iostream>
#include<vector>
#include<algorithm>
#define N 40005
using namespace std;
typedef pair<int, int> pii;
int n, parent[N][30], level[N], root[N];
vector<pii> dist[N];
void make_tree(int index, int lev, int par, int d)
{
root[index]=d;
level[index]=lev;
parent[index][0]=par;
int k=0;
while(parent[parent[index][k]][k]>0) parent[index][k+1] = parent[parent[index][k]][k], k++;
for(auto iter : dist[index]){
if(level[iter.first]) continue;
make_tree(iter.first, lev+1, index, d+iter.second);
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int i, j, m, a, b, c;
cin >> n;
for(i=1; i<n; i++){
cin >> a >> b >> c;
dist[a].push_back({b, c});
dist[b].push_back({a, c});
}
make_tree(1, 1, 0, 0);
cin >> m;
for(i=1; i<=m; i++){
cin >> a >> b;
int l = a, r = b;
if(level[a]<level[b]) swap(a, b);
for(j=20; j>=0; j--){
if(level[a]==level[b]) break;
if(level[parent[a][j]] >= level[b])
a = parent[a][j];
}
for(j=20; j>=0; j--){
if(a==b) break;
if(parent[a][j]!=parent[b][j]){
a = parent[a][j];
b = parent[b][j];
}
}
while(a!=b) a = parent[a][0], b = parent[b][0];
cout << root[l] + root[r] - 2*root[a] << endl;
}
return 0;
}