CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1789 Accepted Submission(s): 573
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.
思路:首先我们要明确s^i^i=s 即一个数两次异或同一个数会得到原数。 所以我们假设任意一点为根节点,然后用DFS求出任意一点到根节点的异或之和(相当于遍历一遍树,O(n)即可),同时记录下每个异或之和的个数。比如有两个点到根节点的异或之和是3,那么f[3]=2;
此时我们可以知道,u点到根节点的异或之和为dp[u],v点到根节点的异或之和为dp[v],那么(u,v)之间的异或之和即是dp[u]^dp[v]
为什么? 我们假设u点到根节点与v点到根节点没有公共路径,即u点和v点在根节点的不同分支上,那么此时uv之间的路径即是u点到根节点,根节点到v,结果显然是对的
我们再假设u点到根节点与v点到根节点之间有公共路径,那么正如我刚刚写出的结论s^i^i=s 。dp[u]与dp[v]在进行异或的时候这段公共路径会相互抵消,最后的结果就是u到v路径的异或值。
注意s为0的情况,s为0的时候,值有两种可能,第一种是(i,i)这种可能,一共有n种
第二种是当路径异或和为m的点有k个时,他们两两之间异或结果必为0(相同的数异或结果为0),那么此时有(k-1)+(k-2)+...+1种可能性,即k*(k-1)/2
加起来就是最后结果。
并且对于一个已知的长度i,与i异或结果为s的数是s^i,所以我们只需要枚举i即可
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 200005
#define LL long long
struct Edge
{
int u,v,w,next;
} edge[N*2];
int cnt,head[N],n;
int f[N],vis[N];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
}
void addedge(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int root,int cost)
{
f[cost]++;
vis[root]=1;
for(int i=head[root]; i!=-1; i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(vis[v]) continue;
dfs(v,cost^w);
}
}
int main()
{
int T,q,t;
int u,v,w;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i=1; i<n; i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dfs(1,0);
scanf("%d",&q);
while(q--)
{
LL ans=0;
scanf("%d",&t);
if(t==0)
{
for (int i=0;i<N;i++)
ans=ans+(LL)f[i]*(f[i]-1)/2;
ans+=n;
}
else
{
for(int i=0; i<N; i++)
{
int tt=i^t;
if(tt>=N) continue;
ans+=(LL)f[i]*f[tt];
}
ans/=2;
}
printf("%lld\n",ans);
}
}
return 0;
}