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

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. 1
  2. 3
  3. 1 2 1
  4. 2 3 2
  5. 3
  6. 2
  7. 3
  8. 4
 
Sample Output
  1. 1
  2. 1
  3. 0
  4. Hint
  5. For the first query, (2, 3) is the only pair that f(u, v) = 2.
  6. For the second query, (1, 3) is the only one.
  7. For the third query, there are no pair (u, v) such that f(u, v) = 4.
  8.  
 
Author
KUT(DPRK)
 
Source
 

  1. /* ***********************************************
  2. Author :CKboss
  3. Created Time :2015年08月21日 星期五 14时10分39秒
  4. File Name :HDOJ5416.cpp
  5. ************************************************ */
  6.  
  7. #include <iostream>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <string>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <vector>
  15. #include <queue>
  16. #include <set>
  17. #include <map>
  18.  
  19. using namespace std;
  20.  
  21. typedef long long int LL;
  22. const int maxn=100100;
  23. const int MX=1e6+10;
  24.  
  25. int n,Q;
  26.  
  27. struct Edge
  28. {
  29. int to,next,val;
  30. }edge[maxn*2];
  31.  
  32. int Adj[maxn],Size;
  33.  
  34. void init()
  35. {
  36. memset(Adj,-1,sizeof(Adj)); Size=0;
  37. }
  38.  
  39. void Add_Edge(int u,int v,int c)
  40. {
  41. edge[Size].to=v;
  42. edge[Size].next=Adj[u];
  43. edge[Size].val=c;
  44. Adj[u]=Size++;
  45. }
  46.  
  47. int num[maxn];
  48. LL cnt[MX];
  49.  
  50. void DFS(int u,int fa,int val)
  51. {
  52. for(int i=Adj[u];~i;i=edge[i].next)
  53. {
  54. int v=edge[i].to;
  55. int c=edge[i].val;
  56. if(v==fa) continue;
  57. num[v]=num[u]^c;
  58. DFS(v,u,num[v]);
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. //freopen("in.txt","r",stdin);
  65. //freopen("out.txt","w",stdout);
  66.  
  67. int T_T;
  68. scanf("%d",&T_T);
  69. while(T_T--)
  70. {
  71. scanf("%d",&n);
  72. init();
  73. for(int i=0,a,b,c;i<n-1;i++)
  74. {
  75. scanf("%d%d%d",&a,&b,&c);
  76. Add_Edge(a,b,c); Add_Edge(b,a,c);
  77. }
  78. memset(cnt,0,sizeof(cnt));
  79. DFS(1,1,0);
  80. for(int i=1;i<=n;i++) cnt[num[i]]++;
  81. scanf("%d",&Q);
  82. while(Q--)
  83. {
  84. int x;
  85. scanf("%d",&x);
  86. LL ans=0;
  87.  
  88. for(int i=1;i<=n;i++)
  89. {
  90. int u=num[i];
  91. int v=x^u;
  92. ans=ans+cnt[v];
  93. }
  94. if(x==0) ans+=n;
  95. printf("%lld\n",ans/2);
  96. }
  97. }
  98.  
  99. return 0;
  100. }

HDOJ 5416 CRB and Tree DFS的更多相关文章

  1. 异或+构造 HDOJ 5416 CRB and Tree

    题目传送门 题意:给一棵树,问f (u, v) 意思是u到v的所有路径的边权值的异或和,问f (u, v) == s 的u,v有几对 异或+构造:首先计算f (1, u) 的值,那么f (u, v) ...

  2. Hdu 5416 CRB and Tree (bfs)

    题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...

  3. HDU 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——————【DFS搜树】

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

  5. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

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

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

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

  7. HDU 5416 CRB and Tree

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

  8. HDU 5416 CRB and Tree (技巧)

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

  9. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

随机推荐

  1. Types of Security Vulnerabilities

    1)内存空间安全.2)参量级别数据安全:3)通信级别数据安全:4)数据访问控制:5)通信对象身份确认. https://developer.apple.com/library/content/docu ...

  2. RGB颜色空间、色调、饱和度、亮度,HSV颜色空间详解

    本文章会详细的介绍RGB颜色空间与RGB三色中色调.饱和度.亮度之间的关系,最后会介绍HSV颜色空间! RGB颜色空间 概述 RGB颜色空间以R(Red:红).G(Green:绿).B(Blue:蓝) ...

  3. ionic小白的学习路之安装运行篇

    1.什麽是ionic? Ionic 是一款基于Angular.Cordova 的强大的HTML5 移动应用开发框架, 可以快速创建一个跨平台的移动应用.可以快速开发移动App.移动端WEB 页面.微信 ...

  4. IDEA无法编译源码,IDEA查看源码出现/* compiled code */

    打开Settings -> Plugins    搜索dec,选中,确定,重启,解决

  5. java登录拦截Filter

    此例子为一个简单的登录拦截. 首先在web.xml中配置拦截类. <filter-mapping> <filter-name>SessionFilter</filter- ...

  6. swift中的as?和as!

    as操作符用来把某个实例转型为另外的类型,由于实例转型可能失败,因此Swift为as操作符提供了两种形式:选项形式as?和强制形式as 选项形式(as?)的操作执行转换并返回期望类型的一个选项值,如果 ...

  7. 前段开发 jq ajax数据处理详细讲解。

    定义和用法 ajax() 方法通过 HTTP 请求加载远程数据. 常用的ajax结构模板: function indes(){ $.ajax({ url: '', type: "GET&qu ...

  8. mysql查询表中最小可用id值

    今天在看实验室的项目时,碰到的一个问题,.先把sql语句扔出来 // 这条语句在id没有1时,不能得到正确的查询结果. select min(id+1) from oslist c where not ...

  9. JS提前声明和定义方式

    来源:JS的函数定义方式以及对声明的提前 以下代码,声明语句会被提前到当前作用域(全局作用域和函数作用域)的顶部.但赋值语句不会提前,依然留在原地 var x = function(){}; var ...

  10. Python 3.52官方文档翻译 http://usyiyi.cn/translate/python_352/library/index.html 必看!

    Python 3.52官方文档翻译   http://usyiyi.cn/translate/python_352/library/index.html 觉得好的麻烦点下推荐!谢谢!