标签:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<queue> #include<cmath> using namespace std; #define INF 0x3fffffff typedef long long LL; const LL maxn = 300005; const LL mod = 1e9+7; bool vis[maxn]; int k, Head[maxn], n, Max = 0; LL num[maxn]; struct node { int e, next, w; }Edge[maxn*2]; void AddEdge(int s,int e,int w) { Edge[k].w = w; Edge[k].e = e; Edge[k].next = Head[s]; Head[s] = k; k ++; } void DFS(int root,int a) { vis[root] = true; num[a] ++; for(int i=Head[root]; i!=-1; i=Edge[i].next) { int e = Edge[i].e; if(vis[e] == true) continue; DFS(e, a^Edge[i].w); } } int main() { int Q, T; scanf("%d", &T); while(T--) { scanf("%d", &n); k = 0; memset(Head, -1, sizeof(Head)); memset(vis, false, sizeof(vis)); memset(num, 0, sizeof(num)); for(int i=0; i<n-1; i++) { int s, e, w; scanf("%d %d %d",&s, &e, &w); AddEdge(s, e, w); AddEdge(e, s, w); } DFS(1, 0); scanf("%d", &Q); while( Q --) { int a; LL ans = 0; scanf("%d", &a); if(a == 0) ans += n; for(int i=0; i<=270000; i++) { if(a == 0) ans += num[i]*(num[i]-1)/2; else if(i < (a^i) && num[i] && num[a^i]) ans += num[i]*num[a^i]; } printf("%lld\n", ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4746918.html