CRB and Tree

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 481    Accepted Submission(s): 151

Problem Description
CRB has a tree, whose vertices are labeled by 1, 2, …, .
They are connected by  –
1 edges. Each edge has a weight.

For any two vertices 

rev=2.4-beta-2" alt="" style=""> and (possibly
equal), 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style=""> is
xor(exclusive-or) sum of weights of all edges on the path from 

rev=2.4-beta-2" alt="" style=""> to .

CRB’s task is for given ,
to calculate the number of unordered pairs  such
that 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">.
Can you help him?

 
Input
There are multiple test cases. The first line of input contains an integer 

rev=2.4-beta-2" alt="" style="">,
indicating the number of test cases. For each test case:

The first line contains an integer  denoting
the number of vertices.

Each of the next  -
1 lines contains three space separated integers 

rev=2.4-beta-2" alt="" style=""> and  denoting
an edge between  and ,
whose weight is 

rev=2.4-beta-2" alt="" style="">.

The next line contains an integer  denoting
the number of queries.

Each of the next 

rev=2.4-beta-2" alt="" style=""> lines
contains a single integer .

1 ≤ 

rev=2.4-beta-2" alt="" style=""> ≤
25

1 ≤  ≤ 

rev=2.4-beta-2" alt="" style="">

1 ≤  ≤
10

1 ≤  ≤ 

rev=2.4-beta-2" alt="" style="">

0 ≤ 

rev=2.4-beta-2" alt="" style="">,  ≤ 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

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)

解题思路:
由于异或是可逆的,因此从前到后记录前缀异或和,用hash表记录每一个值出现的次数,每次仅仅须要加上x ^ sum[v]出现的次数就可以。由于此时,u到v的异或和就是x。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <algorithm>
  11. #define LL long long
  12. using namespace std;
  13. const int MAXN = 100000 + 10;
  14. struct Edge
  15. {
  16. int to, next, w;
  17. }edge[MAXN<<1];
  18. int tot, head[MAXN];
  19. int read()
  20. {
  21. int res = 0, f = 1; char ch = getchar();
  22. while(ch < '0' || ch > '9'){if(ch == '-') f *= -1; ch = getchar();}
  23. while(ch >= '0' && ch <= '9'){res = res * 10 + ch - '0'; ch = getchar();}
  24. return res;
  25. }
  26. void init()
  27. {
  28. tot = 0;
  29. memset(head, -1, sizeof(head));
  30. }
  31. void addedge(int u, int v, int w)
  32. {
  33. edge[tot].to = v;
  34. edge[tot].w = w;
  35. edge[tot].next = head[u];
  36. head[u] = tot++;
  37. }
  38. int N, Q;
  39. int vis[MAXN], st[MAXN<<2], op;
  40. LL ans;
  41. void dfs(int u, int x)
  42. {
  43. vis[u] = 1; st[x]++;
  44. ans += st[op ^ x];
  45. for(int i=head[u];i!=-1;i=edge[i].next)
  46. {
  47. int v = edge[i].to, w = edge[i].w;
  48. if(!vis[v])
  49. {
  50. dfs(v, x ^ w);
  51. }
  52. }
  53. }
  54. int main()
  55. {
  56. int T;
  57. scanf("%d", &T);
  58. while(T--)
  59. {
  60. N = read();
  61. int u, v, w;
  62. init();
  63. for(int i=1;i<N;i++)
  64. {
  65. u = read(); v = read(); w = read();
  66. addedge(u, v, w);
  67. addedge(v, u, w);
  68. }
  69. scanf("%d", &Q);
  70. while(Q--)
  71. {
  72. op = read();
  73. memset(vis, 0, sizeof(vis));
  74. memset(st, 0, sizeof(st));
  75. ans = 0;
  76. dfs(1, 0);
  77. printf("%I64d\n", ans);
  78. }
  79. }
  80. return 0;
  81. }

 

HDU 5416 CRB and Tree (2015多校第10场)的更多相关文章

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

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

  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

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

  6. HDU 5416 CRB and Tree (技巧)

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

  7. HDU 5305 Friends (搜索+剪枝) 2015多校联合第二场

    開始对点搜索,直接写乱了.想了想对边搜索,尽管复杂度高.剪枝一下水过去了. 代码: #include<cstdio> #include<iostream> #include&l ...

  8. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  9. HDOJ 5416 CRB and Tree DFS

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

随机推荐

  1. JAVA实现将GeoHash转化为相应的经纬度坐标

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/50568428 http://www.llwjy.com/blogdetail/f ...

  2. TeamTalk Android代码分析(业务流程篇)---消息发送和接收的整体逻辑说明

    第一次纪录东西,也没有特别的顺序,想到哪里就随手画了一下,后续会继续整理- 6.2消息页面动作流程 6.2.1 消息页面初始化的总体思路 1.页面数据的填充更新直接由页面主线程从本地数据库请求 2.数 ...

  3. Java-MyBatis-杂项:MyBatis根据数组、集合查询

    ylbtech-Java-MyBatis-杂项:MyBatis根据数组.集合查询 1.返回顶部 1. foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的 ...

  4. ROS-SLAM仿真-cartographer

    前言:cartographer是谷歌2016年发布的一个开源slam算法,采用基于图网络的优化方法,主要基于激光雷达来实现. 使用源码编译方式. 一.新建工作空间 1.1 使用roboware新建名为 ...

  5. BZOJ 2427 /HAOI 2010 软件安装 tarjan缩点+树形DP

    终于是道中文题了.... 当时考试的时候就考的这道题.... 果断GG. 思路: 因为有可能存在依赖环,所以呢 先要tarjan一遍 来缩点. 随后就进行一遍树形DP就好了.. x表示当前的节点.j表 ...

  6. js从数组中取出n个不重复的数据

    /** * 首先,针对这个数组做一个去重处理,避免你在后面取数据的时候,因为取到相同的元素而又要多去取一次随机数 * 将获取到的不重复的数组,再到这里样本里面去取随机数 * 每取到一次,就将这个元素从 ...

  7. hdu 1394 Minimum Inversion Number 【线段树求逆序数】

    之前写过树状数组的,再用线段树写一下--- #include<cstdio> #include<cstring> #include<iostream> #inclu ...

  8. Xcode 下“ did not have any applicable content ”分析及解决

    问题的产生 a.新建项目时选的iPhone b.为了做成图片启动,按照惯例去掉了LaunchStoryboard的引用,建了个LaunchImage的资源,属性里随便勾了一个,找了张匹配的图拖了过去 ...

  9. 如何巧妙使用ZBrush中的Image Plane插件

    ZBrush®插件Image Plane提供了一种简单的方法加载图像到ZBrush中,以在添加纹理过程中进行使用,比如使用ZProject笔刷多边形着色,以及利用参考图建模等. ZBrush 中文版下 ...

  10. Bzoj 2502: 清理雪道 有上下界网络流_最小流

    好长时间没有写网络流了,感觉好手生.对于本题,设一个源点 $s$ 和 $t$.1.由 $s$ 向每个点连一条没有下界,容量为无限大的边,表示以该点为起点.2.由每个点向 $t$ 连一条没有下界,容量为 ...