hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
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
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.
- #include <iostream>
- #include <cstring>
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<algorithm>
- using namespace std;
- const int maxn=131072;
- struct EDGE
- {
- int to,v,next;
- }edge[200010];
- int ne=0;
- int head[100010];
- int sum[200010];
- int n;
- void addedge(int s,int e,int v)
- {
- edge[ne].to=e;
- edge[ne].next=head[s];
- edge[ne].v=v;
- head[s]=ne++;
- }
- void dfs(int now,int pre,int nows)
- {
- sum[nows]++;
- for(int i=head[now];i!=-1;i=edge[i].next)
- {
- if(edge[i].to==pre) continue;
- dfs(edge[i].to,now,nows^edge[i].v);
- }
- }
- int main()
- {
- int T,i;
- cin>>T;
- while(T--)
- {
- ne=0;
- memset(head,-1,sizeof(head));
- cin>>n;
- for(i=0;i<n-1;i++)
- {
- int a,b,c;
- scanf("%d %d %d",&a,&b,&c);
- addedge(a,b,c);
- addedge(b,a,c);
- }
- memset(sum,0,sizeof(sum));
- dfs(1,0,0);
- int q,s;
- cin>>q;
- while(q--)
- {
- long long ans1=0,ans2=0;
- cin>>s;
- for(i=0;i<131072;i++)
- {
- int x=i,y=s^i;
- if(x!=y)
- ans1+=(1ll*sum[x]*sum[y]);
- else
- {
- ans1+=(1ll*sum[x]*(sum[x]-1));
- ans2+=1ll*sum[x];
- }
- }
- cout<<ans1/2+ans2<<endl;
- }
- }
- }
hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)的更多相关文章
- 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 (2015多校第10场)
欢迎參加--每周六晚的BestCoder(有米!) CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536 ...
- 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
题目大意: 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也是合法的. 思路:任意点对之间的路径肯定经 ...
- HDOJ 5416 CRB and Tree DFS
CRB and Tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- 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 ...
- 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 ...
随机推荐
- 配置redis三主三从
主从环境 centos7.6 redis4.0.1 主 从 192.168.181.139:6379 192.168.181.136:6379 192.168.181.136:6380 192.168 ...
- jquery 五星评价(图片实现)
1111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- Spring Boot 与任务
一.任务 1.异步任务 package com.yunche.task.service; import org.springframework.stereotype.Service; /** * @C ...
- UVA - 12661 Funny Car Racing (Dijkstra算法)
题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...
- 创建和获取cookie
创建和获取cookie 制作人:全心全意 cookie:在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用 ...
- 利用ajax全局设置实现拦截器
var token = localStorage.getItem("token"); $.ajaxSetup({ dataType: "json", cache ...
- IDLE in Python (Ubuntu)
To lauch IDLE in the Current Woking Directory >>> usr/bin/idle3 Alt + n # next command Alt ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了
insertSelective---Java MyBatis 插入数据库返回主键--insertSelective这样就不用每次到数据库里面查询了 https://www.cnblogs.com/xi ...
- BNUOJ 33898 Cannon
Cannon Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ID: 449 ...