CRB and Tree

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

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.

 
 
题目大意:给你一棵n个顶点n-1条边的树,每条边有一个权重,定义f(u,v)为结点u到结点v的边权异或值的和,让你求在该树上有多少f(u,v)=s的无序对。
 
解题思路:由于异或的性质。a^a=0。f(u,v)=f(1,u)^f(1,v)。f(u,v)=s  =>  f(1,u)^f(1,v)=s  =>   f(1,v)=f(1,u)^s。那么我们可以枚举u,然后得出f(1,v)为 f(1,u)^s的有多少个。对于s为0的情况,我们发现对于f(1,u)^0的结果还是f(1,u)我们在计算的时候会加一次f(1,u)本身的情况。那么我们最后只需要加上n就可以满足f(u,v)=f(u,u)的情况了。
如样例:
555 
3
1 2 1
1 3 1
1
0
如果不加上n且没除以2之前的结果会是5。如果加上n那么结果就是8。最后除以2以后就是4。
 
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+200;
int n,tot;
struct Edge{
int to,w,next;
Edge(){}
Edge(int too,int ww,int nextt){
to=too;w=ww;next=nextt;
}
}edges[maxn*3];
typedef long long INT;
int head[maxn*2],val[maxn*2];
int times[maxn*2];
void init(){
tot=0;
memset(head,-1,sizeof(head));
memset(times,0,sizeof(times));
memset(val,0,sizeof(val));
}
void addedge(int fro , int to,int wei){
edges[tot]=Edge(to,wei,head[fro]);
head[fro]=tot++;
edges[tot]=Edge(fro,wei,head[to]);
head[to]=tot++;
}
void dfs(int u,int fa){
times[val[u]]++;
for(int i=head[u];i!=-1;i=edges[i].next){
Edge &e=edges[i];
if(e.to==fa)
continue;
val[e.to]=val[u]^e.w;
dfs(e.to,u);
}
return ;
}
int main(){
// freopen("1011.in","r",stdin);
// freopen("why.txt","w",stdout);
int t , m, a,b,c ,s;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
dfs(1,-1);
scanf("%d",&m);
while(m--){
INT ans=0;
scanf("%d",&s);
for(int i=1;i<=n;++i){
if(times[val[i]^s]){
ans+=times[val[i]^s];
}
}
if(s==0)
ans+=n;
printf("%lld\n",ans/2);
}
}
return 0;
}

  

 
 
 

HDU 5416——CRB and Tree——————【DFS搜树】的更多相关文章

  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(前缀思想+DFS)

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

  3. HDOJ 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(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

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

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

  6. HDU 5416 CRB and Tree

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

  7. HDU 5416 CRB and Tree (技巧)

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

  8. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  9. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

随机推荐

  1. Python-OpenCV中VideoCapture类的使用

    目录   主要记录Python-OpenCV中的VideoCapture类的使用:官方文档:   VideoCapture()是用于从视频文件.图片序列.摄像头捕获视频的类: #!/usr/bin/e ...

  2. Go语言学习教程:管理员登录功能开发

    学习完了数据库操作的知识以后.本节内容,我们将实现管理员登陆功能,涉及到多个模块的代码实现和逻辑处理,以及数据库表的操作,都将在本节内容中进行实现. 管理员结构体定义 首先我们要定义管理员这个实体的结 ...

  3. 利用Angular2的Observables实现交互控制

    在Angular1.x中,我们使用Promise来处理各种异步.但是在angular2中,使用的是Reactive Extensions (Rx)的Observable.对于Promise和Obser ...

  4. Websocket,ProtoBuffer,Hightlight,JSON 等,最近遇到的一些知识点小结

    websocket websocket 支持但不仅限于web场景,是一种封装好的socket通信协议,可以用来做C-S通信. 可以通过设置 binaryType 来指定通信的数据流格式,从而达到简洁高 ...

  5. C#之重载

    前言 今天去看了看论坛,发现有些人对于重载还是很有疑问的,像大多数人一样,貌似知道重载方法这回事儿, 但是具体怎么应用,或者用重载方法的好处,好像还是一知半解,模模糊糊.我们都知道,重载方法的定义,甚 ...

  6. 通过div实现arcgis自定义infowindow

    通过给地图绑定缩放,单击和平移命令,实现在地图附加div标签,实现infowindow效果: /* *作者 扰扰 *自定义esri弹窗 *paramter Map地图对象 *paramter x *p ...

  7. 基础线程机制--Executor线程池框架

    基础线程机制 Executor线程池框架 1.引入Executor的原因 (1)new Thread()的缺点 ​  每次new Thread()耗费性能 ​  调用new Thread()创建的线程 ...

  8. pytorch 0.4.1安装问题

    环境 ubuntu 16.04, anaconda3, python 2.7 将pytorch升级为0.4.1后,import torch报错 undefined symbol: _ZN4thpp10 ...

  9. Josephus环的四种解法(约瑟夫环)

    约瑟夫环 约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个 ...

  10. C#工具类之字符串扩展类

    /// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...