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 ...
随机推荐
- EasyUi取消选中表格的所有行
- L - Large Division (大数, 同余)
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- linux 远程连接ssh提示 IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY解决
Linux ssh 远程登录到其他机器上时,有时会出现登不进去,并弹出如下类似提示的情况: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- Flink学习笔记-支持的数据类型
说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...
- ubuntu14 上无法使用vim命令的解决方法
在ubuntu14.10终端输入vim: The program 'vim' can be found in the following packages: * vim * vim-gnome * v ...
- 【STL基础】deque
deque (double-ended queue) 构造函数: //default: deque<T> d; //空的vector //fill: deque<T> d(n) ...
- AngularJs ng-repeat解决循环对象出现重复项报错的问题
问题:ng-repeat 的循环对象是不能出现重复项的,所以如果有重复的就会报错,应该是 key value的问题吧,不是很了解内部运行机制:经过查询发现 在 循环后面加上 track by $i ...
- python 中文路径问题
Python直接读取中文路径的文件时失败,可做如下处理: inpath = 'D:/work/yuanxx/在线导航/驾车导航/walk_log/20130619_172355.txt' uipath ...
- java程序员的从0到1:@Resource与@Autowired的比较
目录: 1.@Resource与@Autowired的源码分析 2.@Resource与@Autowired的相同点 3.@Resource与@Autowired的不同点 正文: 1.@Resourc ...
- redis初识及基本操作
一.redis介绍: 1.Redis是什么: REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(Key-Value) ...