bzoj 4813: [Cqoi2017]小Q的棋盘 树形dp
2017-10-24 15:50
513 查看
题意
小Q正在设计一种棋类游戏。在小Q设计的游戏中,棋子可以放在棋盘上的格点中。某些格点之间有连线,棋子只能在有连线的格点之间移动。整个棋盘上共有V个格点,编号为0,1,2…,V-1,它们是连通的,也就是说棋子从任意格点出发,总能到达所有的格点。小Q在设计棋盘时,还保证棋子从一个格点移动到另外任一格点的路径是唯一的。小Q现在想知道,当棋子从格点0出发,移动N步最多能经过多少格点。格点可以重复经过多次,但不重复计数。V,N<=100
分析
设f[i,j]表示从i开始走了j步且最后回到自己的最大贡献,g[i,j]表示不回到自己的最大贡献。瞎转移一下即可。代码
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int N=105; int n,f ,g ,last ,cnt,m,tmpf ,tmpg ; struct edge{int to,next;}e[N*2]; int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void addedge(int u,int v) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt; e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt; } void dfs(int x,int fa) { f[x][0]=g[x][0]=1; for (int i=last[x];i;i=e[i].next) { if (e[i].to==fa) continue; dfs(e[i].to,x); for (int j=0;j<=m;j++) tmpf[j]=tmpg[j]=0; for (int j=0;j<=m;j++) for (int k=0;k<=m-j-1;k++) { tmpf[j+k+2]=max(tmpf[j+k+2],f[x][j]+f[e[i].to][k]); tmpg[j+k+1]=max(tmpg[j+k+1],f[x][j]+g[e[i].to][k]); tmpg[j+k+2]=max(tmpg[j+k+2],g[x][j]+f[e[i].to][k]); } for (int j=0;j<=m;j++) f[x][j]=max(f[x][j],tmpf[j]),g[x][j]=max(g[x][j],tmpg[j]); } } int main() { n=read();m=read(); for (int i=1;i<n;i++) { int x=read(),y=read(); addedge(x+1,y+1); } dfs(1,0); int ans=0; for (int i=0;i<=m;i++) ans=max(ans,max(f[1][i],g[1][i])); printf("%d",ans); return 0; }
相关文章推荐
- BZOJ 4813: [Cqoi2017]小Q的棋盘 (树形dp)
- bzoj 4813: [Cqoi2017]小Q的棋盘【树形dp】
- bzoj 4813: [Cqoi2017]小Q的棋盘 [树形背包dp]
- bzoj4813 [Cqoi2017]小Q的棋盘 树上DP
- [BZOJ4813][CQOI2017]小Q的棋盘(DP,贪心)
- BZOJ 1813 [Cqoi2017]小Q的棋盘 ——树形DP
- bzoj4813 [Cqoi2017]小Q的棋盘
- bzoj 4813: [Cqoi2017]小Q的棋盘
- bzoj 4813 [Cqoi2017]小Q的棋盘
- cqoi2017,bzoj4813小Q的棋盘(树形dp或瞎搞)
- 【bzoj4813】[Cqoi2017]小Q的棋盘 树上dfs+贪心
- BZOJ 4813 [Cqoi2017]小Q的棋盘
- bzoj4813: [Cqoi2017]小Q的棋盘
- 【BZOJ4813】 [Cqoi2017]小Q的棋盘
- [bzoj4813][树形dp]小Q的棋盘
- BZOJ 4813 [Cqoi2017]小Q的棋盘
- bzoj 4813 [Cqoi2017]小Q的棋盘(思维)
- BZOJ4813 [Cqoi2017]小Q的棋盘
- bzoj4813 [Cqoi2017]小Q的棋盘(树形dp)
- BZOJ_4813_[Cqoi2017]小Q的棋盘_dfs