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 102
| #include <bits/stdc++.h> using namespace std;
const int SIZE=500010; int ver[2*SIZE],Next[2*SIZE],edge[2*SIZE],head[SIZE]; int fa[SIZE],d[SIZE],v[SIZE],lca[SIZE],ans[SIZE]; vector<int> query[SIZE],query_id[SIZE]; int T,n,m,tot,t;
void add(int x,int y,int z){ ver[++tot]=y; edge[tot]=z; Next[tot]=head[x]; head[x]=tot; }
void add_query(int x,int y,int id){ query[x].push_back(y); query_id[x].push_back(id); query[y].push_back(x); query_id[y].push_back(id); }
int get(int x){ if(x==fa[x]) return x; return fa[x]=get(fa[x]); }
void tarjan(int x){ v[x]=1; for(int i=head[x];i;i=Next[i]){ int y=ver[i]; if(v[y]) continue; d[y]=d[x]+edge[i]; tarjan(y); fa[y]=x; } for(int i=0;i<query[x].size();i++){ int y=query[x][i],id=query_id[x][i]; if(v[y]==2){ int lca=get(y); ans[id]=min(ans[id],d[x]+d[y]-2*d[lca]); } } v[x]=2; }
int main(){ cin>>T; while(T--){ cin>>n>>m; for(int i=1;i<=n;i++){ head[i]=0; fa[i]=i; v[i]=0; query[i].clear(); query_id[i].clear(); } tot=0; for(int i=1;i<n;i++){ int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,z); } for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); if(x==y) ans[i]=0; else{ add_query(x,y,i); ans[i]=1<<30; } } tarjan(1); for(int i=1;i<=m;i++) printf("%d\n",ans[i]); } return 0; }
|