hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
来源:互联网 发布:如何应用安卓源码 编辑:程序博客网 时间:2024/06/18 04:42
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 verticesu 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 givens , to calculate the number of unordered pairs (u,v) such that f(u,v) = s . Can you help him?
For any two vertices
CRB’s task is for given
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 integerN denoting the number of vertices.
Each of the nextN - 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 integerQ denoting the number of queries.
Each of the nextQ 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.
The first line contains an integer
Each of the next
The next line contains an integer
Each of the next
1 ≤
1 ≤
1 ≤
1 ≤
0 ≤
It is guaranteed that given edges form a tree.
Output
For each query, output one line containing the answer.
Sample Input
131 2 12 3 23234
Sample Output
110HintFor 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
KUT(DPRK)
Source
2015 Multi-University Training Contest 10
解题思路:
首先对于从节点u到节点v的异或值等于u到根节点的异或值再异或v到根节点的异或值,这是因为a^b=a^c^c^b,
于是可以dfs求出所有节点到根节点的异或值,接着就是求所有异或值为s的情况,我们枚举一个u到根节点的值x,
则v到根节点值为s^x,根据dfs的结果可以直接找到,因为u,v是无序的,会出现x==s^x的情况,特殊考虑就可。
代码:
#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; } }}
0 0
- hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
- 2015 Multi-University Training Contest 10 CRB and String
- HDU 5414 CRB and String (2015 Multi-University Training Contest 10 2015多校联合)
- hdu 5414 CRB and String(2015 Multi-University Training Contest 10)
- HDU 5406 CRB and Apple (2015 Multi-University Training Contest 10)
- HDU 5414 CRB and String( 2015 Multi-University Training Contest 10)
- hdu 5410 CRB and His Birthday 背包问题 2015 Multi-University Training Contest 10 ★★★
- HDU 5407 CRB and Candies(素数筛 + 逆元 + 组合数公式)——2015 Multi-University Training Contest 10
- hdu 5336 XYZ and Drops 2015 Multi-University Training Contest 4 10
- hdu 4925 Apple Tree--2014 Multi-University Training Contest 6
- hdu 4925 Apple Tree 2014 Multi-University Training Contest 6
- 2017 Multi-University Training Contest 1003(hdu 6035) Colorful Tree
- 2015 Multi-University Training Contest 10(hdu 5406 - hdu 5416)
- 【HDU5411 2015 Multi-University Training Contest 10F】【矩阵快速幂 加一行构造法】CRB and Puzzle 矩阵的1次方到n次方的数值和
- hdu 5379 Mahjong tree DFS 心塞,比赛时没取模 2015 Multi-University Training Contest 7
- HDU 5336 XYZ and Drops 2015 Multi-University Training Contest 4 1010
- HDU 5336 XYZ and Drops(BFS 水滴爆破)2015 Multi-University Training Contest 4
- hdu 5336 XYZ and Drops 2015 Multi-University Training Contest 4
- 百度地图Javascript SDK
- jdk旧版本下载地址
- c排序算法总结
- java annotation基本概念
- Linux下如何搭建VPN服务器
- hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
- UNIX Tutorial Seven_下载配置安装软件剥离代码
- Java 构建器
- SQL Server 维护计划实现数据库备份(Step by Step)
- Android Studio 使用夜神模拟器Nox
- require与include的区别
- mac mysql
- 架构与系统设计资料集合TODO
- 解决jar包依赖:Spring IO platform推出bom