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


   
   
  解题思路:


       首先对于从节点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;
}
}
}

hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)的更多相关文章

  1. Hdu 5416 CRB and Tree (bfs)

    题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...

  2. HDU 5416 CRB and Tree (2015多校第10场)

    欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536 ...

  3. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. HDU 5416——CRB and Tree——————【DFS搜树】

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. HDU 5416 CRB and Tree

    题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...

  6. HDU 5416 CRB and Tree (技巧)

    题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...

  7. HDOJ 5416 CRB and Tree DFS

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. vue2.0框架认识

    虚拟dom和声明式渲染: Vue的编译器在编译模板之后,会把这些模板编译成一个渲染函数 .而函数被调用的时候就会渲染并且返回一个 虚拟DOM的树 .这个树非常轻量,它的职责就是描述当前界面所应处的状态 ...

  2. Java 基础入门随笔(3) JavaSE版——逻辑运算符、位运算符

    上一节写了一些运算符的注意事项,这节开头依然是对运算符的一些注意点的阐述! 比较运算符除了>.>=.<.<=.==.!=之外需要注意instanceof:检查是否是类的对象,例 ...

  3. margin与padding如何进行区分

    margin与padding如何进行区分,这是很多学html人的困扰,其实说白了padding 就是内容与边框的空隙.而margin则是模块与模块的空隙.[3]

  4. CAD利用Select2得到所有实体(网页版)

    主要用到函数说明: IMxDrawSelectionSet::Select2 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARI ...

  5. 11Java Server Pages 动作

    Java Server Pages 动作 JSP标准动作 分类 JSP标准动作 存取JavaBean相关 <jsp:useBean> <jsp:setProperty> < ...

  6. 09js、MySQL相关

    09js.MySQL相关-2018/07/19 1.js的dom 理解一下文档对象模型:html文件加载到内存之后会形成一颗dom树,根据这些节点对象可以进行脚本代码的动态修改;在dom树当中 一切皆 ...

  7. Gym - 101670E Forest Picture (CTU Open Contest 2017 模拟)

    题目: https://cn.vjudge.net/problem/1451310/origin 题意&思路: 纯粹模拟. 大体题意是这样的: 1.有人要在一个10-9<=x<=1 ...

  8. python3 的range

    1. range(3):从0到2: 2. range(3,10,2):从3到10,隔一个数显示一个: 3.list(range(3,10,-2):结果为空: 4. S[::2] : 同隔一个数显示一个 ...

  9. LINUX-光盘

    cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force 清空一个可复写的光盘内容 mkisofs /dev/cdrom > ...

  10. 处理回车提交、ctrl+enter和shift+enter都不提交->textarea正常换行

    <input type="textarea" @on-keypress="handlerMultiEnter"> handlerMultiEnter ...