hdu-5416(多校2015)
来源:互联网 发布:同声翻译手机软件知乎 编辑:程序博客网 时间:2024/05/29 12:26
CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 587 Accepted Submission(s): 187
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.
题意:给一棵树,树上的边对应一个值,q个询问s,即f(u,v),从u到v的路径上的边值异或操作,结果等于s的路径数。
思路:f(u,v)=f(1,u)^f(1,v);
s^f(1,v)=f(1,u);
//#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<stdio.h>#include<math.h>#include <string>#include<string.h>#include<map>#include<queue>#include<set>#include<utility>#include<vector>#include<algorithm>#include<stdlib.h>using namespace std;#define eps 1e-8#define pii pair<int,int>#define inf 0x3f3f3f3f#define rd(x) scanf("%d",&x)#define rd2(x,y) scanf("%d%d",&x,&y)#define ll long long int#define mod 1000000007#define maxn 100005#define maxm 1000005vector<pii> f[maxn];int ff[maxn];ll num[maxm];int t,n,q,x,y,z;void dfs(int x,int pre,int k){//求f(1,x) ff[x]=k; num[k]++; for(int i=0;i<f[x].size();i++) { int v=f[x][i].first; int vv=f[x][i].second; if(v==pre) continue; dfs(v,x,k^vv); }}int main(){ rd(t); while(t--){ rd(n); for(int i=1;i<=n;i++) f[i].clear(); for(int i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&z); f[x].push_back(make_pair(y,z)); f[y].push_back(make_pair(x,z)); } memset(num,0,sizeof(num)); dfs(1,-1,0); rd(q); for(int i=1;i<=q;i++){ rd(x); ll res=0; for(int j=1;j<=n;j++) { res+=num[x^ff[j]]; if((x^ff[j])==ff[j]) res++; } printf("%I64d\n",res/2); } } return 0;}
0 0
- hdu-5416(多校2015)
- hdu-5402(多校2015)
- 2015 多校(1) HDU 5288 HDU 5289
- 2015多校VI hdu
- hdu-5407(多校2015)
- 2015 Multi-University Training Contest 10(hdu 5406 - hdu 5416)
- HDU 5416(思路题目)
- hdu 5334 多校(4)
- hdu 5316 多校(3)
- HDU 5416 CRB and Tree (2015多校第10场)
- HDU 5288 OO’s Sequence(数学啊 多校2015)
- HDU 5317 RGCDQ(素数个数 多校2015啊)
- hdu 5416
- hdu 多校
- hdu 多校
- 多校 hdu
- 多校 hdu
- 多校 hdu
- Andriod开发中常遇到的问题
- TextView和EditView使用
- html5图片裁剪控件原型【含缩放,旋转,拖动功能】---3、实际演示效果
- 黑马66期最新Android视频教程开始更新【全套不加密】
- mac下配置phonegap(cordova) xcode_5.1.1开发环境
- hdu-5416(多校2015)
- 白话空间统计十一:线性方向平均值
- 代码备忘
- POJ3461Oulipo
- @GeneratedValue 四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
- Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误
- lucene PaodingAnalyzer 学习总结
- Ionic教程-modal的使用
- leetcode 147: Insertion Sort List