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 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 :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,根 ...
随机推荐
- Types of Security Vulnerabilities
1)内存空间安全.2)参量级别数据安全:3)通信级别数据安全:4)数据访问控制:5)通信对象身份确认. https://developer.apple.com/library/content/docu ...
- RGB颜色空间、色调、饱和度、亮度,HSV颜色空间详解
本文章会详细的介绍RGB颜色空间与RGB三色中色调.饱和度.亮度之间的关系,最后会介绍HSV颜色空间! RGB颜色空间 概述 RGB颜色空间以R(Red:红).G(Green:绿).B(Blue:蓝) ...
- ionic小白的学习路之安装运行篇
1.什麽是ionic? Ionic 是一款基于Angular.Cordova 的强大的HTML5 移动应用开发框架, 可以快速创建一个跨平台的移动应用.可以快速开发移动App.移动端WEB 页面.微信 ...
- IDEA无法编译源码,IDEA查看源码出现/* compiled code */
打开Settings -> Plugins 搜索dec,选中,确定,重启,解决
- java登录拦截Filter
此例子为一个简单的登录拦截. 首先在web.xml中配置拦截类. <filter-mapping> <filter-name>SessionFilter</filter- ...
- swift中的as?和as!
as操作符用来把某个实例转型为另外的类型,由于实例转型可能失败,因此Swift为as操作符提供了两种形式:选项形式as?和强制形式as 选项形式(as?)的操作执行转换并返回期望类型的一个选项值,如果 ...
- 前段开发 jq ajax数据处理详细讲解。
定义和用法 ajax() 方法通过 HTTP 请求加载远程数据. 常用的ajax结构模板: function indes(){ $.ajax({ url: '', type: "GET&qu ...
- mysql查询表中最小可用id值
今天在看实验室的项目时,碰到的一个问题,.先把sql语句扔出来 // 这条语句在id没有1时,不能得到正确的查询结果. select min(id+1) from oslist c where not ...
- JS提前声明和定义方式
来源:JS的函数定义方式以及对声明的提前 以下代码,声明语句会被提前到当前作用域(全局作用域和函数作用域)的顶部.但赋值语句不会提前,依然留在原地 var x = function(){}; var ...
- Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 必看!
Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 觉得好的麻烦点下推荐!谢谢!