HDU 5416 CRB and Tree (2015多校第10场)
来源:互联网 发布:论文引用格式网络文章 编辑:程序博客网 时间:2024/05/29 02:38
CRB and Tree
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 481 Accepted Submission(s): 151
Problem Description
CRB has a tree, whose vertices are labeled by 1, 2, …, . They are connected by – 1 edges. Each edge has a weight.
For any two vertices and (possibly equal), is xor(exclusive-or) sum of weights of all edges on the path from to .
CRB’s task is for given , to calculate the number of unordered pairs such that . 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 , indicating the number of test cases. For each test case:
The first line contains an integer denoting the number of vertices.
Each of the next - 1 lines contains three space separated integers , and denoting an edge between and , whose weight is .
The next line contains an integer denoting the number of queries.
Each of the next lines contains a single integer .
1 ≤ ≤ 25
1 ≤ ≤
1 ≤ ≤ 10
1 ≤ , ≤
0 ≤ , ≤
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)
解题思路:
因为异或是可逆的,因此从前到后记录前缀异或和,用hash表记录每个值出现的次数,每次只需要加上x ^ sum[v]出现的次数即可。因为此时,u到v的异或和就是x。
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>#include <queue>#include <set>#include <map>#include <stack>#include <algorithm>#define LL long long using namespace std;const int MAXN = 100000 + 10;struct Edge{int to, next, w;}edge[MAXN<<1];int tot, head[MAXN];int read(){int res = 0, f = 1; char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-') f *= -1; ch = getchar();}while(ch >= '0' && ch <= '9'){res = res * 10 + ch - '0'; ch = getchar();}return res;}void init(){tot = 0;memset(head, -1, sizeof(head));}void addedge(int u, int v, int w){edge[tot].to = v;edge[tot].w = w;edge[tot].next = head[u];head[u] = tot++;}int N, Q;int vis[MAXN], st[MAXN<<2], op;LL ans;void dfs(int u, int x){vis[u] = 1; st[x]++;ans += st[op ^ x];for(int i=head[u];i!=-1;i=edge[i].next){int v = edge[i].to, w = edge[i].w;if(!vis[v]){dfs(v, x ^ w);}}}int main(){int T;scanf("%d", &T);while(T--){N = read();int u, v, w;init();for(int i=1;i<N;i++){u = read(); v = read(); w = read();addedge(u, v, w);addedge(v, u, w);}scanf("%d", &Q);while(Q--){op = read();memset(vis, 0, sizeof(vis));memset(st, 0, sizeof(st));ans = 0;dfs(1, 0);printf("%I64d\n", ans);}}return 0;}
0 0
- HDU 5416 CRB and Tree (2015多校第10场)
- hdu 5416 CRB and Tree(暴力)
- HDU 5416 CRB and Tree(dfs)
- HDU 5416 CRB and Tree
- 多校第十场 1011 hdu 5416 CRB and Tree(树形dp)
- HDU 5414 CRB and String (2015年多校比赛第10场)
- HDU 5411 CRB and Puzzle (2015年多校比赛第10场)
- HDU 5410 CRB and His Birthday (2015年多校比赛第10场)
- HDU 5407 CRB and Candies (2015年多校比赛第10场)
- hdu 5416 CRB and Tree(dfs+前缀和)
- HDU 5416 CRB and Tree(dfs 异或逆运算)
- hdu 5416 CRB and Tree(异或运算)
- hdu 5416 CRB and Tree(异或,思路题)
- 前缀和,LCA(CRB and Tree,HDU 5416)
- 【瞎搞】 HDU 5416 CRB and Tree
- HDU 5416 CRB and Tree dfs
- HDU 5416 CRB and Tree (树形DP)
- HDU 5416 CRB and Tree (DFS)
- 亚马逊AWS学习——创建RDS实例
- Java泛型的使用
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
- pypcap安装过程记录
- UVA 101 The Blocks Problem
- HDU 5416 CRB and Tree (2015多校第10场)
- 面试宝典_JVM
- luaplus和c++之间的代码交互
- 汇编语言的重要性
- adb logcat获取异常日志代码
- PopupWindow实现弹窗效果
- leetcode: Minimum Depth of Binary Tree
- Hibernate知识点
- UIButton设置圆角