HDU 5416 CRB and Tree (树形DP)
来源:互联网 发布:addictive drums2 mac 编辑:程序博客网 时间:2024/05/14 09:39
题目链接:传送门
题意:
在一棵树上,给定一个函数F(u,v)表示从u到v的路径上的边的权值异或起来的结果,然后求F(u,v)=s有多少种情况。
分析:
设dp[u]表示点u到根节点的路径异或起来的值,然后F(u,v)=dp[u]^dp[v],我们设cnt[x]表示dp[u]=x的情况有多少种,那么在s不等于0的情况时ans[s] = sigma(cnt[i]*cnt[s^i]),注意s = 0 的情况。
代码如下:
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int maxn = 1e6+10;typedef long long LL;int dp[maxn];int head[maxn],ip;LL cnt[maxn];int n;struct nod{ int to,next,w;}edge[maxn];bool vis[maxn];void init(){ ip=0; memset(head,-1,sizeof(head)); memset(cnt,0,sizeof(cnt)); memset(vis,0,sizeof(vis));}void add(int u,int v,int w){ edge[ip].to=v; edge[ip].w=w; edge[ip].next=head[u]; head[u]=ip++;}int mmax;void dfs(int u,int pre,int val){ for(int i=head[u];i!=-1;i=edge[i].next){ int v= edge[i].to; if(v==pre) continue; if(edge[i].w^val>mmax) mmax = edge[i].w^val; cnt[edge[i].w^val]++; dfs(v,u,val^edge[i].w); }}int main(){ int t,m; scanf("%d",&t); while(t--){ init(); scanf("%d",&n); for(int i=0;i<n-1;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } mmax = 0; dfs(1,1,0); scanf("%d",&m); cnt[0]++; for(int i=0;i<m;i++){ int s; scanf("%d",&s); LL ans = 0; for(int j=0;j<maxn;j++){ if((s^j)==j) ans=ans+(cnt[j]*(cnt[j]-1)); else ans = ans=ans+cnt[j]*cnt[s^j]; } ans=ans/2; if(s==0) ans=ans+n; printf("%I64d\n",ans); } } return 0;}/***4571 2 11 3 11 4 14 5 23 6 22 7 211231230**/
1 0
- HDU 5416 CRB and Tree (树形DP)
- HDU 5416 CRB and Tree (树形dp)
- 多校第十场 1011 hdu 5416 CRB and Tree(树形dp)
- hdu5416 CRB and Tree(树形DP)
- HDU 5416 CRB and Tree
- 【瞎搞】 HDU 5416 CRB and Tree
- HDU 5416 CRB and Tree dfs
- HDU 5416 CRB and Tree (DFS)
- hdu 5416 CRB and Tree (DFS)
- hdu 5416 CRB and Tree(暴力)
- HDU 5416 CRB and Tree(dfs)
- hdu 5416 CRB and Tree [思维]【树】
- 5416 CRB and Tree
- hdu 5416 CRB and Tree(dfs+前缀和)
- HDU 5416 CRB and Tree (2015多校第10场)
- HDU 5416 CRB and Tree(dfs 异或逆运算)
- hdu 5416 CRB and Tree(异或运算)
- hdu 5416 CRB and Tree 离线处理树xor前缀
- 字符串排序(2015华为校招)
- [LeetCode]Remove Duplicates from Sorted Array II
- graph slam tutorial : g2o 的使用
- c++中类型转换积累(持续更新)
- [LeetCode]Search in Rotated Sorted Array
- HDU 5416 CRB and Tree (树形DP)
- Evaluating Simple C Expressions
- JSTL标准标签库详解
- HDU 5410 CRB and His Birthday(DP)
- Android NDK编译常见错误及解决方案
- ListView的Intent跳转详情页, 滑动与点击Position错乱导致数据混乱问题解决
- poj 2602 大数相加(字符串输出)
- MAC下NDK环境搭建及hello world程序
- float是什么?浮动在CSS中的作用