-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1471.cpp
113 lines (97 loc) · 1.74 KB
/
1471.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
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
#include"iomanip"
#include"iostream"
#include"limits"
#include"cmath"
#include"vector"
#include"algorithm"
#include"list"
#include"queue"
#include"stack"
#include"set"
#include"unordered_set"
#include"map"
#include"unordered_map"
#include"string"
#include"cstring"
#include"assert.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define mset(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn=50000+5;
list<pii>adj[maxn];
int tou[maxn],froo[maxn],st[maxn],fi[maxn],ti=0;
int anc[maxn][17];
int n;
void df(int u,int len,int p=-1)
{
st[u]=ti++;
froo[u]=len;
tou[u]=1;
for(pii xx:adj[u])
{
if(xx.ff==p)continue;
anc[xx.ff][0]=u;
df(xx.ff,len+xx.ss,u);
}
fi[u]=ti++;
}
int che(int u,int v)//u is ancestor of v
{
if(st[u]<=st[v]&&fi[v]<=fi[u])return 1;
return 0;
}
void build()
{
for(int i=1;i<=16;++i)
{
for(int u=0;u<n;++u)
{
anc[u][i]=anc[ anc[u][i-1] ] [i-1];
}
}
}
int lca(int u,int v)
{
if(che(u,v))return u;
if(che(v,u))return v;
for(int i=16;i>=0;--i)
{
if(!che(anc[u][i],v))
{
u=anc[u][i];
}
}
return anc[u][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(int i=0;i<n-1;++i)
{
int u,v,w;
cin>>u>>v>>w;
adj[u].pb(mp(v,w));
adj[v].pb(mp(u,w));
}
df(0,0);
anc[0][0]=0;
build();
int m;
cin>>m;
while(m--)
{
int u,v;
cin>>u>>v;
cout<<froo[u]+froo[v]-2*froo[lca(u,v)]<<'\n';
}
}