【瞎搞】 HDU 5416 CRB and Tree

点击打开链接

查询才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
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值