标签:void ring cstring 链接 can algo lang turn scanf
链接Miku
dfs就行了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int head[100001];
int p;
int n;
int m;
int x,y,z;
int dis[100001];
struct e{
int to;
int ne;
int v;
}ed[200001];
void add(int f,int t,int v){
p++;
ed[p].ne=head[f];
ed[p].to=t;
ed[p].v=v;
head[f]=p;
return ;
}
void dfs(int now,int f){
for(int i=head[now];i;i=ed[i].ne){
if(ed[i].to==f) continue;
dis[ed[i].to]=dis[now]^ed[i].v;
dfs(ed[i].to,now);
}
return ;
}
int main(){
scanf("%d",&n);
for(int i=1;i<n;++i){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
scanf("%d",&m);
dfs(1,1);
for(int i=1;i<=m;++i){
scanf("%d%d",&x,&y);
x=dis[x]^dis[y];
cout<< x <<endl;
}
return 0;
}
标签:void ring cstring 链接 can algo lang turn scanf
原文地址:https://www.cnblogs.com/For-Miku/p/13619668.html