萌新乱搞错误求助
查看原帖
萌新乱搞错误求助
311921
_晓风残月_楼主2021/1/25 20:05
#include<bits/stdc++.h>
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
struct node{
	int u,add;
};
vector<node> v[100001];
int n,q,dis[100001];
inline void addsum(int x,int y,int add)
{
	node now;
	now.u=y;
	now.add=add;
	v[x].push_back(now);
	node now1;
	now1.u=x;
	now1.add=add;
	v[y].push_back(now1);
}
bool vis[100001];
inline void dfs(int id,int val)
{
	vis[id]=true;
	dis[id]=val;
	for(int i=0;i<v[id].size();i++)
	{
		if(vis[v[id][i].u])continue;
		dfs(id+1,val^v[id][i].add);
	}
}
int main()
{
	n=read();
	for(int i=1;i<=n-1;++i)
	{
		addsum(read(),read(),read());
	}
	dfs(1,0);
	q=read();
	while(q--)
	{
		printf("%d\n",dis[read()]^dis[read()]);
	}
}
2021/1/25 20:05
加载中...