![[Luogu] 让我们异或吧 [Luogu] 让我们异或吧](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
https://www.luogu.org/problemnew/show/P2420
异或满足
A ^ B = B ^ A
A ^ A = 0
0 ^ A = A
#include <cstdio>
#include <iostream> using namespace std;
const int N = 1e5 + ; #define yxy getchar() #define RR freopen("gg.in", "r", stdin) int n, Ty, now = , head[N], A[N];
struct Node{int u, v, w, nxt;} G[N << ]; inline int read() {
int x = , f = ; char c = yxy;
while(c < '' || c > '') {if(c == '-') f = -; c = yxy;}
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x * f;
} void Add(int u, int v, int w) {
G[now].v = v; G[now].w = w; G[now].nxt = head[u]; head[u] = now ++;
} void Dfs(int u, int fa) {
for(int i = head[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(v != fa) {
A[v] = G[i].w ^ A[u];
Dfs(v, u);
}
}
} int main() {
n = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++) {
int u = read(), v = read(), w = read();
Add(u, v, w); Add(v, u, w);
}
Dfs(, );
Ty = read();
while(Ty --) {
int u = read(), v = read();
cout << (A[u] ^ A[v]) << endl;
} return ;
}