hdu 5416
来源:互联网 发布:苍蝇枪淘宝 编辑:程序博客网 时间:2024/05/17 07:56
CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 607 Accepted Submission(s): 194
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
Recommend
wange2014 | We have carefully selected several similar problems for you: 5416 5415 5414 5413 5412
#include <cstdio>#include <cstring>#include <iostream>#include <set>using namespace std;#define LL long long#define N 100000 + 10int n, q, s, T;int head[N], tot;int m[2 * N];int dist[N];struct edge{ int v, w, next;}e[2 * N];void init(){ tot = 0; memset(head, -1, sizeof head); memset(m, 0, sizeof m);}void adde(int u, int v, int w){ e[tot].v = v; e[tot].w = w; e[tot].next = head[u]; head[u] = tot++;}void dfs(int u, int fa, int val){ dist[u] = val; m[val]++; for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].v; if(v == fa) continue; dfs(v, u, val ^ e[i].w); }}void solve(){ LL ans = 0; int j ; for(int i = 1; i <= n; i++) { j = dist[i] ^ s; if(j == dist[i]) ans += m[j] - 1; else ans += m[j]; } ans /= 2; if(s == 0) ans += n; printf("%I64d\n", ans);}int main(){ scanf("%d", &T); while(T--) { scanf("%d", &n); init(); int u, v, w; for(int i = 1; i < n; i++) { scanf("%d%d%d", &u, &v, &w); adde(u, v, w); adde(v, u, w); } dfs(1, -1, 0); scanf("%d", &q); for(int i = 0; i < q; i++) { scanf("%d", &s); solve(); } } return 0;}/*131 2 12 3 23234151 2 13 5 21 3 22 4 210032*/
1 0
- hdu 5416
- HDU 5416 dfs
- 树形DP HDU 5416
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- HDU 5407 CRB and Candies (2015年多校比赛第10场)
- 统计简单学_正态分布
- 系统架构 很喜欢所以转发了
- win7下安装matplotlib+six+pysparsing
- YUV和RGB之间的转换公式
- hdu 5416
- Linux安装SVN服务器
- 原码、反码、补码
- 二分图之最大匹配匈牙利算法
- POJ 3264 Balanced Lineup(RMQ/线段树)
- mysql pattern matching
- Codevs3008 加工生产调度
- jmc监控工具
- 有一种感动叫ACM(记WJMZBMR在成都赛区开幕式上的讲话)