HDOJ 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): 690 Accepted Submission(s): 221
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?
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.
1
3
1 2 1
2 3 2
3
2
3
4
1
1
0HintFor 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 :CKboss
Created Time :2015年08月21日 星期五 14时10分39秒
File Name :HDOJ5416.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL;
const int maxn=100100;
const int MX=1e6+10; int n,Q; struct Edge
{
int to,next,val;
}edge[maxn*2]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void Add_Edge(int u,int v,int c)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
edge[Size].val=c;
Adj[u]=Size++;
} int num[maxn];
LL cnt[MX]; void DFS(int u,int fa,int val)
{
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int c=edge[i].val;
if(v==fa) continue;
num[v]=num[u]^c;
DFS(v,u,num[v]);
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d",&n);
init();
for(int i=0,a,b,c;i<n-1;i++)
{
scanf("%d%d%d",&a,&b,&c);
Add_Edge(a,b,c); Add_Edge(b,a,c);
}
memset(cnt,0,sizeof(cnt));
DFS(1,1,0);
for(int i=1;i<=n;i++) cnt[num[i]]++;
scanf("%d",&Q);
while(Q--)
{
int x;
scanf("%d",&x);
LL ans=0; for(int i=1;i<=n;i++)
{
int u=num[i];
int v=x^u;
ans=ans+cnt[v];
}
if(x==0) ans+=n;
printf("%lld\n",ans/2);
}
} return 0;
}
HDOJ 5416 CRB and Tree DFS的更多相关文章
- 异或+构造 HDOJ 5416 CRB and Tree
题目传送门 题意:给一棵树,问f (u, v) 意思是u到v的所有路径的边权值的异或和,问f (u, v) == s 的u,v有几对 异或+构造:首先计算f (1, u) 的值,那么f (u, v) ...
- 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 ...
- HDU 5416——CRB and Tree——————【DFS搜树】
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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也是合法的. 思路:任意点对之间的路径肯定经 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
随机推荐
- Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
1. 接口 接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法 2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下: (1)这里MainActivity.j ...
- jxcel - 好用的Excel与Java对象转换工具
更多精彩博文,欢迎访问我的个人博客 Jxcel简介 Jxcel是一个支持Java对象与Excel(目前仅xlsx.xls)互相转换的工具包. 项目地址:https://github.com/jptan ...
- HTML中获取input中单选按钮radio数据(性别例子)
个人学习整理 1.编写HTML <!doctype html> <html> <head> <meta charset="utf-8"&g ...
- Openjudge-4151-电影节
这个题是一道贪心的题目,我们要想看的电影数目最多,我们肯定每次都要选最早结束的电影,这样我们才能去看下一部电影. 它本身最早结束,如果同时开始,那肯定是它的放映时间比较短,如果它后开始,先结束,那它的 ...
- 【MySQL】索引和锁
前言 本文摘自数据库两大神器[索引和锁] 声明:如果没有说明具体的数据库和存储引擎,默认指的是MySQL中的InnoDB存储引擎 索引 在之前,我对索引有以下的认知: 索引可以加快数据库的检索速度 表 ...
- ES5的数组方法
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array Array.prot ...
- Django之学员管理三
Django之学员管理三 web框架的本质: 本质是客户端和服务端的交互.用socket实现. socket客户端(浏览器) 2.发送ip和端口,http://www.baidu.com:80/ind ...
- uwsgs loading shared libraries: libicui18n.so.58 异常处理
背景 想使用 ningx + uwsgi + flask 搭建 python 应用环境 Python使用的是anaconda3(pyhton 3.6) 依赖包安装完毕,但是执行 uwsgi 的时候出现 ...
- 03002_Http请求协议分析
1.编写一个form.html的表单页面 (1)使用EclipseEE新建一个动态的web项目: (2)Dynamic web module version选择2,5版本: (3)新建一个form.h ...
- [Go]GOPATH相关知识点
在成功安装好Go之后,执行命令 go env 就可以看到有关go的一些环境变量,其中比较关键的是GOROOT.GOPATH和 GOBIN 1.设置GOPATH环境变量有什么意义? GOPATH是指:指 ...