CRB and Tree

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

                                                                                           Total Submission(s): 79    Accepted Submission(s): 16

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
 


   
   
  解题思路:


       首先对于从节点u到节点v的异或值等于u到根节点的异或值再异或v到根节点的异或值,这是由于a^b=a^c^c^b,
   
   于是能够dfs求出全部节点到根节点的异或值,接着就是求全部异或值为s的情况,我们枚举一个u到根节点的值x,
   
   则v到根节点值为s^x,依据dfs的结果能够直接找到。由于u,v是无序的,会出现x==s^x的情况。特殊考虑就可。



  代码:
  1. #include <iostream>
  2. #include <cstring>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<algorithm>
  7. using namespace std;
  8. const int maxn=131072;
  9. struct EDGE
  10. {
  11. int to,v,next;
  12. }edge[200010];
  13.  
  14. int ne=0;
  15. int head[100010];
  16. int sum[200010];
  17. int n;
  18. void addedge(int s,int e,int v)
  19. {
  20. edge[ne].to=e;
  21. edge[ne].next=head[s];
  22. edge[ne].v=v;
  23. head[s]=ne++;
  24. }
  25.  
  26. void dfs(int now,int pre,int nows)
  27. {
  28. sum[nows]++;
  29. for(int i=head[now];i!=-1;i=edge[i].next)
  30. {
  31. if(edge[i].to==pre) continue;
  32. dfs(edge[i].to,now,nows^edge[i].v);
  33. }
  34. }
  35.  
  36. int main()
  37. {
  38. int T,i;
  39. cin>>T;
  40. while(T--)
  41. {
  42. ne=0;
  43. memset(head,-1,sizeof(head));
  44. cin>>n;
  45. for(i=0;i<n-1;i++)
  46. {
  47. int a,b,c;
  48. scanf("%d %d %d",&a,&b,&c);
  49. addedge(a,b,c);
  50. addedge(b,a,c);
  51. }
  52. memset(sum,0,sizeof(sum));
  53. dfs(1,0,0);
  54. int q,s;
  55. cin>>q;
  56. while(q--)
  57. {
  58. long long ans1=0,ans2=0;
  59. cin>>s;
  60. for(i=0;i<131072;i++)
  61. {
  62. int x=i,y=s^i;
  63. if(x!=y)
  64. ans1+=(1ll*sum[x]*sum[y]);
  65. else
  66. {
  67. ans1+=(1ll*sum[x]*(sum[x]-1));
  68. ans2+=1ll*sum[x];
  69. }
  70. }
  71. cout<<ans1/2+ans2<<endl;
  72. }
  73. }
  74. }

hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)的更多相关文章

  1. Hdu 5416 CRB and Tree (bfs)

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

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

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

  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. HDOJ 5416 CRB and Tree DFS

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

  8. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. 配置redis三主三从

    主从环境 centos7.6 redis4.0.1 主 从 192.168.181.139:6379 192.168.181.136:6379 192.168.181.136:6380 192.168 ...

  2. jquery 五星评价(图片实现)

    1111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  3. Spring Boot 与任务

    一.任务 1.异步任务 package com.yunche.task.service; import org.springframework.stereotype.Service; /** * @C ...

  4. UVA - 12661 Funny Car Racing (Dijkstra算法)

    题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...

  5. 创建和获取cookie

    创建和获取cookie 制作人:全心全意 cookie:在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用 ...

  6. 利用ajax全局设置实现拦截器

    var token = localStorage.getItem("token"); $.ajaxSetup({ dataType: "json", cache ...

  7. IDLE in Python (Ubuntu)

    To lauch IDLE in the Current Woking Directory >>> usr/bin/idle3 Alt + n  # next command Alt ...

  8. hdu 1754 I Hate It(线段树水题)

    >>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...

  9. Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了

    insertSelective---Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了 https://www.cnblogs.com/xi ...

  10. BNUOJ 33898 Cannon

    Cannon Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ID: 449 ...