HDOJ 5416 CRB and Tree
来源:互联网 发布:如何寻找淘宝达人 编辑:程序博客网 时间:2024/06/06 03:54
点分治的做法很好想,但是会超时。对于这个题,找一个点dfs,统计异或值的个数就行了。。
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 100005;const int maxm = 200005;struct Edge{int v, w;Edge *next;}E[maxm], *H[maxn], *edges;int cnt[maxn << 1];int a[maxn], n, m;void addedges(int u, int v, int w){edges->v = v;edges->w = w;edges->next = H[u];H[u] = edges++;}void init(){edges = E;memset(H, 0, sizeof H);}void dfs(int u, int fa, int dist){cnt[dist]++;a[u] = dist;for(Edge *e = H[u]; e; e = e->next) if(e->v != fa) dfs(e->v, u, dist ^ e->w);}void work(){scanf("%d", &n);for(int i = 1; i < n; i++) {int a, b, c;scanf("%d%d%d", &a, &b, &c);addedges(a, b, c);addedges(b, a, c);}memset(cnt, 0, sizeof cnt);dfs(1, 1, 0);scanf("%d", &m);while(m--) {LL ans = 0;int s;scanf("%d", &s);for(int i = 1; i <= n; i++) {if(s == 0) ans += cnt[a[i]] + 1;else ans += cnt[a[i] ^ s];}printf("%lld\n", ans / 2);}}int main(){int _;scanf("%d", &_);while(_--) {init();work();}return 0;}
0 0
- HDOJ 5416 CRB and Tree
- HDOJ 5416 CRB and Tree DFS
- 5416 CRB and Tree
- HDU 5416 CRB and Tree
- 【瞎搞】 HDU 5416 CRB and Tree
- HDU 5416 CRB and Tree dfs
- HDU 5416 CRB and Tree (树形DP)
- HDU 5416 CRB and Tree (DFS)
- hdu 5416 CRB and Tree (DFS)
- hdu 5416 CRB and Tree(暴力)
- HDU 5416 CRB and Tree (树形dp)
- HDU 5416 CRB and Tree(dfs)
- hdu 5416 CRB and Tree [思维]【树】
- 【HDOJ 1009】 CRB and String
- HDOJ 5409 CRB and Graph
- hdu 5416 CRB and Tree(dfs+前缀和)
- HDU 5416 CRB and Tree (2015多校第10场)
- HDU 5416 CRB and Tree(dfs 异或逆运算)
- 问号运算符
- 代码面试最常用的10大算法(Java)
- 面试题23从上往下打印二叉树
- R入门
- Activity启动过程(一)AMS
- HDOJ 5416 CRB and Tree
- 在OnPaint中必须调用一次BeginPaint和EndPaint,且也只能调用一次
- 控制反转与依赖注入
- Oracle Instant Client(即时客户端) 安装与配置
- Validform的基本使用-表单校验
- Android性能优化典范(三)
- Deep Learning(深度学习)学习笔记整理系列之(五)
- 欢迎使用CSDN-markdown编辑器
- 文件读取之getline函数