hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

CRB and Tree

                                                            Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                           Total Submission(s): 79    Accepted Submission(s): 16


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?
 

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 ab 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 ≤ ab ≤ N
0 ≤ cs ≤ 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 0
Hint
For 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
 


   
   
  解题思路:

       首先对于从节点u到节点v的异或值等于u到根节点的异或值再异或v到根节点的异或值,这是由于a^b=a^c^c^b,
   
   于是能够dfs求出全部节点到根节点的异或值,接着就是求全部异或值为s的情况,我们枚举一个u到根节点的值x,
   
   则v到根节点值为s^x,依据dfs的结果能够直接找到。由于u,v是无序的,会出现x==s^x的情况。特殊考虑就可。


  代码:
#include <iostream>
#include <cstring>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=131072;
struct EDGE
{
    int to,v,next;
}edge[200010];

int ne=0;
int head[100010];
int sum[200010];
int n;
void addedge(int s,int e,int v)
{
    edge[ne].to=e;
    edge[ne].next=head[s];
    edge[ne].v=v;
    head[s]=ne++;
}

void dfs(int now,int pre,int nows)
{
    sum[nows]++;
    for(int i=head[now];i!=-1;i=edge[i].next)
    {
        if(edge[i].to==pre) continue;
        dfs(edge[i].to,now,nows^edge[i].v);
    }
}

int main()
{
    int T,i;
    cin>>T;
    while(T--)
    {
        ne=0;
        memset(head,-1,sizeof(head));
        cin>>n;
        for(i=0;i<n-1;i++)
        {
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            addedge(a,b,c);
            addedge(b,a,c);
        }
        memset(sum,0,sizeof(sum));
        dfs(1,0,0);
        int q,s;
        cin>>q;
        while(q--)
        {
            long long ans1=0,ans2=0;
            cin>>s;
            for(i=0;i<131072;i++)
            {
                int x=i,y=s^i;
                if(x!=y)
                    ans1+=(1ll*sum[x]*sum[y]);
                else
                {
                    ans1+=(1ll*sum[x]*(sum[x]-1));
                    ans2+=1ll*sum[x];
                }
            }
            cout<<ans1/2+ans2<<endl;
        }
    }
}


posted @ 2017-07-05 16:37  wzzkaifa  阅读(128)  评论(0编辑  收藏  举报