50分求助
查看原帖
50分求助
1404345
zzx20110203楼主2024/8/23 17:30

P2420

#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >ve[100005];
int sum[100005];
void dfs(int fa,int x){
	if(ve[x].size()==1){
		return ;
	}
	for(int i=0;i<ve[x].size();i++){
		if(ve[x][i].first!=fa){
			sum[ve[x][i].first]=sum[x]^ve[x][i].second;
			dfs(x,ve[x][i].first);
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	int n,m,x,y,z;
	cin>>n;
	for(int i=1;i<=n-1;i++){
		cin>>x>>y>>z;
		ve[x].push_back({y,z});
		ve[y].push_back({x,z});
	}
	sum[1]=0;
	dfs(0,1);
	cin>>m; 
	while(m--){
		cin>>x>>y;
		cout<<(sum[x]^sum[y])<<endl;
	} 
	return 0;
}

#1 6 7 8 9 WA

2024/8/23 17:30
加载中...