查询才10个
X^Y=S && X^S=Y X^Y^X=Y
DFS处理出每个点到 节点1 的异或值 并统计每个异或值的数量
对于查询的值 S 枚举每个点的到根节点的异或值 可以知道另一个需要的异或值 先前处理了异或值的个数
还要注意S=0的时候
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod= 1e9+7;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define N_node 100005
#define N_edge 200010
typedef struct
{
int to ,next,cost;
}STAR;
STAR E[N_edge];
int head[N_node] ,tot;
int XOR[N_node],num[500000];
int mark[N_node];
int nowT;
void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = head[a];
head[a] = tot;
}
void DFS(int x ,int nowxor)
{
mark[x] = 1;
XOR[x] = nowxor;
if(x!=1)
num[XOR[x]]++;
for(int k = head[x] ;~k ;k = E[k].next)
{
if(!mark[E[k].to])
DFS(E[k].to ,nowxor^E[k].cost);
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,a,b,c;
scanf("%d",&n);
memset(head ,-1 ,sizeof(head));
tot = 0;
for(int i = 1 ;i < n ;i ++)
{
scanf("%d %d %d" ,&a ,&b ,&c);
add(a,b,c);
add(b,a,c);
}
memset(mark ,0 ,sizeof(mark));
memset(num ,0 ,sizeof(num));
DFS(1,0);
int q;
scanf("%d",&q);
while(q--)
{
int c;
scanf("%d",&c);
LL tmp=0;
LL aaaa=0;//
for(int i=2;i<=n;i++)
{
if(c==XOR[i])//1-i
aaaa++;
if((XOR[i]^c)==XOR[i])
tmp+=num[XOR[i]]-1;
else
tmp+=num[XOR[i]^c];//other-i
}
LL x=0;
if(c==0)
x=n;
printf("%I64d\n",tmp/2+aaaa+x);
}
}
return 0;
}
/*
1
3
1 2 0
2 3 0
1
0
*/