CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 690 Accepted Submission(s): 221
Problem Description
CRB has a tree, whose vertices are labeled by 1, 2, …,
N
. They are connected by
N
– 1 edges. Each edge has a weight.
For any two vertices u and v (possibly equal), f(u,v) is xor(exclusive-or) sum of weights of all edges on the path from u to v .
CRB’s task is for given s , to calculate the number of unordered pairs (u,v) such that f(u,v) = s . Can you help him?
For any two vertices u and v (possibly equal), f(u,v) is xor(exclusive-or) sum of weights of all edges on the path from u to v .
CRB’s task is for given s , to calculate the number of unordered pairs (u,v) such that f(u,v) = s . Can you help him?
Input
There are multiple test cases. The first line of input contains an integer
T
, indicating the number of test cases. For each test case:
The first line contains an integer N denoting the number of vertices.
Each of the next N - 1 lines contains three space separated integers a , b and c denoting an edge between a and b , whose weight is c .
The next line contains an integer Q denoting the number of queries.
Each of the next Q lines contains a single integer s .
1 ≤ T ≤ 25
1 ≤ N ≤ 105
1 ≤ Q ≤ 10
1 ≤ a , b ≤ N
0 ≤ c , s ≤ 105
It is guaranteed that given edges form a tree.
The first line contains an integer N denoting the number of vertices.
Each of the next N - 1 lines contains three space separated integers a , b and c denoting an edge between a and b , whose weight is c .
The next line contains an integer Q denoting the number of queries.
Each of the next Q lines contains a single integer s .
1 ≤ T ≤ 25
1 ≤ N ≤ 105
1 ≤ Q ≤ 10
1 ≤ a , b ≤ N
0 ≤ c , s ≤ 105
It is guaranteed that given edges form a tree.
Output
For each query, output one line containing the answer.
Sample Input
1 3 1 2 1 2 3 2 3 2 3 4
Sample Output
1 1 0HintFor the first query, (2, 3) is the only pair that f(u, v) = 2. For the second query, (1, 3) is the only one. For the third query, there are no pair (u, v) such that f(u, v) = 4.
Author
KUT(DPRK)
Source
/* ***********************************************
Author :CKboss
Created Time :2015年08月21日 星期五 14时10分39秒
File Name :HDOJ5416.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int LL;
const int maxn=100100;
const int MX=1e6+10;
int n,Q;
struct Edge
{
int to,next,val;
}edge[maxn*2];
int Adj[maxn],Size;
void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
}
void Add_Edge(int u,int v,int c)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
edge[Size].val=c;
Adj[u]=Size++;
}
int num[maxn];
LL cnt[MX];
void DFS(int u,int fa,int val)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int c=edge[i].val;
if(v==fa) continue;
num[v]=num[u]^c;
DFS(v,u,num[v]);
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&n);
init();
for(int i=0,a,b,c;i<n-1;i++)
{
scanf("%d%d%d",&a,&b,&c);
Add_Edge(a,b,c); Add_Edge(b,a,c);
}
memset(cnt,0,sizeof(cnt));
DFS(1,1,0);
for(int i=1;i<=n;i++) cnt[num[i]]++;
scanf("%d",&Q);
while(Q--)
{
int x;
scanf("%d",&x);
LL ans=0;
for(int i=1;i<=n;i++)
{
int u=num[i];
int v=x^u;
ans=ans+cnt[v];
}
if(x==0) ans+=n;
printf("%lld\n",ans/2);
}
}
return 0;
}