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
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
1 3 1 2 1 2 3 2 3 2 3 4
Sample Output
1 1 0HintFor 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
解题思路:
首先对于从节点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; } } }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步