HDU 5416——CRB and Tree——————【DFS搜树】
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
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?
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.
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.
#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搜树】的更多相关文章
- Hdu 5416 CRB and Tree (bfs)
题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...
- HDU 5416 CRB and Tree(前缀思想+DFS)
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- HDOJ 5416 CRB and Tree DFS
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
CRB and Tree Time Limit: 8000/4000 MS (J ...
- HDU 5416 CRB and Tree (2015多校第10场)
欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5416 CRB and Tree
题目大意: T, T组测试数据 给你一个n有n个点,下标是从 1 开始的.这个是一棵树,然后下面是n-1条边, 每条边的信息是 s,e,w 代表 s-e的权值是w 然后是一个Q代表Q次询问. 每次询问 ...
- HDU 5416 CRB and Tree (技巧)
题意:给一棵n个节点的树(无向边),有q个询问,每个询问有一个值s,问有多少点对(u,v)的xor和为s? 注意:(u,v)和(v,u)只算一次.而且u=v也是合法的. 思路:任意点对之间的路径肯定经 ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
随机推荐
- Vue v-if ToolList
可根据v-if="IsOk",动态判断标签是否展示 <template> <div id="app"> <input type=& ...
- dubbo异步调用原理 (1)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 一.使用方式 服务提供方不变,调用方代码如下: 1 <dubbo:reference id=& ...
- Locust压力测试使用总结
https://blog.csdn.net/jojoy_tester/article/details/77926470 参考网址 上次做接口压力测试前一直研究使用jmeter,本以为可以拿来使用了, ...
- Linux配置国内的Yum源
因为Linux默认的yum源是国外的源,所以会有卡顿,缓慢的情况.而国内的Yum源相对速度较快,现在也比较成熟,所以给Linux更换国内Yum源是一个很好的选择. 1. 备份(备份之前需要yum i ...
- P4094 [HEOI2016/TJOI2016]字符串 后缀数组+主席树+二分答案
$ \color{#0066ff}{ 题目描述 }$ 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须 ...
- 写一个Spring Boot的Hello World
尽管这个demo也就hello world水平,但我还是要记录一下(总算能动了QAQ),毕竟老是看文章不动手不行啊 上次写Servlet的CRUD项目还是2月份,虽然代码忘的差不多了,但我就记得JDB ...
- excel测试数据导入
需求背景:测试数据的导入一般在dataprovider中写入对应的测试数据,当参数较多,组合测试或者接口参数测试的测试数据都需要逐一写数据驱动类,数据准备消耗了大量时间.前一篇博客中介绍了对偶测试的小 ...
- 20. js继承的6种方式
想要继承,就必须要提供个父类(继承谁,提供继承的属性) 一.原型链继承 重点:让新实例的原型等于父类的实例. 特点: 1.实例可继承的属性有:实例的构造函数的属性,父类构造函数属性,父类原型的属性.( ...
- vscode 注册表
Windows Registry Editor Version 5.00 ; Open files [HKEY_CLASSES_ROOT\*\shell\Open with VS Code] @=&q ...
- Django之ORM其他骚操作 执行原生SQl
Django ORM执行原生SQL # extra # 在QuerySet的基础上继续执行子语句 # extra(self, select=None, where=None, params=Non ...