-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF.cpp
More file actions
101 lines (91 loc) · 2.01 KB
/
Copy pathF.cpp
File metadata and controls
101 lines (91 loc) · 2.01 KB
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
100
101
#include<bits/stdc++.h>
using namespace std;
#define MAXNODES (int)1e5+1
struct edge{
int to,w;
edge(){}
edge(int T,int W): to(T),w(W){}
};
vector<edge> g[MAXNODES];
int T[MAXNODES];
int P[MAXNODES];
int L[MAXNODES];
long long dist_to_root[MAXNODES];
int n,nr;
void fill_l(){
L[0]=0;
dist_to_root[0]=0LL;
vector<int> visited(n,0);
queue<int> Q;
Q.push(0);
while(!Q.empty()){
int act=Q.front();Q.pop();
visited[act]=true;
for(int i=0;i< g[act].size();++i){
if(visited[g[act][i].to])continue;
T[g[act][i].to]=act;
L[g[act][i].to]=L[act] + 1;
dist_to_root[g[act][i].to]=dist_to_root[act] + g[act][i].w;
Q.push(g[act][i].to);
}
}
}
void bfs(){
vector<int> visited(n,0);
queue<int> Q;
Q.push(0);
while(!Q.empty()){
int node= Q.front();Q.pop();
if(visited[node])continue;
visited[node]=true;
if (L[node] < nr)
P[node]=1;
else
if(!(L[node] % nr))
P[node]=T[node];
else
P[node]=P[T[node]];
for(int i=0;i< g[node].size();++i)
Q.push(g[node][i].to);
}
}
int LCA(int x,int y){
while (P[x] != P[y])
if (L[x] > L[y])
x=P[x];
else
y=P[y];
while (x != y)
if (L[x] > L[y])
x=T[x];
else
y=T[y];
return x;
}
int main() {
freopen("fun.in","r",stdin);
freopen("out.txt","w",stdout);
int t;cin>>t;
while(t--){
int m;
cin>>n>>m;
memset(T,-1,sizeof T);
nr=sqrt(n);
for(int i = 0;i< n;++i)
g[i].clear();
for(int i=1 ;i < n ;++ i) {
int x,y,z;
cin>>x>>y>>z;
g[x-1].push_back(edge(y-1,z));
g[y-1].push_back(edge(x-1,z));
}
fill_l();
bfs();
for(int i=0;i< m;++i){
int u,v;
cin>>u>>v;u--;v--;
cout<<(dist_to_root[u] + dist_to_root[v] - 2LL*dist_to_root[LCA(u,v)])<<endl;
}
}
return 0;
}