版权声明:~~~感谢支持! https://blog.csdn.net/qq_39897867/article/details/88593112
题目
https://www.luogu.org/problemnew/show/P1351
解题思路
需要知道一个关于
的知识:
因为
答案易得
简化一下就变成了
就是
代码
#include<cstdio>
#include<algorithm>
#define rep(i,x,y) for(register int i=x;i<=y;i++)
using namespace std;
struct node{int y,z,next;}a[200005];
int g,n,head[100001],dis[100001],tot;
void add(int x,int y,int z){a[++tot]=(node){y,z,head[x]};head[x]=tot;}
void dfs(int x,int fa){
for(register int i=head[x];i;i=a[i].next){
int y=a[i].y;
if (y!=fa){
dis[y]=dis[x]^a[i].z;
dfs(y,x);
}
}
}
int main(){
scanf("%d",&n);
int x,y,z;
rep(i,1,n-1)
scanf("%d%d%d",&x,&y,&z),add(x,y,z),add(y,x,z);
dfs(1,0);
scanf("%d",&g);
rep(i,1,g) scanf("%d%d",&x,&y),printf("%d\n",dis[x]^dis[y]);
}