hdu-5416(多校2015)

本文介绍了一种解决树形结构中特定路径异或值查询问题的算法。通过预处理从根节点到每个节点的异或值,并使用哈希表记录每种异或值出现的次数,快速响应查询,寻找满足条件的节点对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CRB and Tree

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


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.

题意:给一棵树,树上的边对应一个值,q个询问s,即f(u,v),从u到v的路径上的边值异或操作,结果等于s的路径数。

思路:f(u,v)=f(1,u)^f(1,v);

           s^f(1,v)=f(1,u);


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <string>
#include<string.h>
#include<map>
#include<queue>
#include<set>
#include<utility>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-8
#define pii pair<int,int>
#define inf 0x3f3f3f3f
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define mod 1000000007
#define maxn 100005
#define maxm 1000005
vector<pii> f[maxn];
int ff[maxn];
ll num[maxm];
int t,n,q,x,y,z;
void dfs(int x,int pre,int k){//求f(1,x)
    ff[x]=k;
    num[k]++;
    for(int i=0;i<f[x].size();i++)
    {
        int v=f[x][i].first;
        int vv=f[x][i].second;
        if(v==pre) continue;
        dfs(v,x,k^vv);
    }
}
int main()
{
    rd(t);
    while(t--){
        rd(n);
        for(int i=1;i<=n;i++) f[i].clear();
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            f[x].push_back(make_pair(y,z));
            f[y].push_back(make_pair(x,z));
        }
        memset(num,0,sizeof(num));
        dfs(1,-1,0);
        rd(q);
        for(int i=1;i<=q;i++){
            rd(x);
            ll res=0;
            for(int j=1;j<=n;j++)
            {
                res+=num[x^ff[j]];
                if((x^ff[j])==ff[j]) res++;
            }
            printf("%I64d\n",res/2);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值