hdu5416 CRB and Tree(树形DP)

题目:

CRB and Tree

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


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 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 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
 

Recommend
wange2014
 

题意:给一棵树,树的形态和每条边的权值告诉你,有Q个询问,每个询问有一个整数s,问你这棵树中有多少对节点,使得这两个节点间路径的异或和等于s。

思路:由于异或的结合律和交换律,u和v路径的异或和f(u,v)=f(root,u)^f(root,v),所以我们只需用树形DP求出根节点与每个节点的异或和,然后将这些值都存起来,对于每个询问,我们只需遍历所有节点,假设第i个节点与根节点的异或和为dp[i],如果dp[i]^dp[j]=s,那么dp[j]=dp[i]^s,所以只需看一下有多少个节点的值为dp[i]^s,每次查询复杂度为O(n),

需要注意当dp[i]^s=dp[i]的时候要特判一下。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/



struct node
{
    int id;
    int w;
    node(int _id,int _w):id(_id),w(_w){}
};
vector<node>g[100000+5];
int head[100000+5];
struct Edge
{
    int u;
    int next;
    int w;
}edge[100000+5];
int elen;

long long int dp[100000+5];
void dfs(int cur,int f)
{
    for(int i=0;i<g[cur].size();i++)
        if(g[cur][i].id!=f)
    {
        dp[g[cur][i].id]=dp[cur]^g[cur][i].w;
        dfs(g[cur][i].id,cur);
    }
}
map<long long int ,int >mm;
int sum[1000000];
int main()
{int T;
RI(T);
while(T--)
{

    int n;
    RI(n);
    for(int i=1;i<=n;i++)
        g[i].clear();
    MS0(sum);
    int a,b,c;
    //mm.clear();
    for(int i=0;i<n-1;i++)
    {
        RIII(a,b,c);
        g[a].push_back(node(b,c));
        g[b].push_back(node(a,c));
    }
    dp[1]=0;
    dfs(1,1);
    for(int i=1;i<=n;i++)
        sum[dp[i]]++;
    int q;
    RI(q);
    while(q--)
    {
        int s;
        RI(s);
        long long int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(dp[i]==(dp[i]^s))
                ans++;
            ans+=sum[dp[i]^s];
        }
        printf("%I64d\n",ans/2);

    }
}



        return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值