1907: 树的路径覆盖
来源:互联网 发布:ios付费软件推荐 编辑:程序博客网 时间:2024/05/17 22:00
题目链接
题目大意:求树的最小路径覆盖数
题解:Tree dp或贪心
Orz题解
我的收获:贪心强啊,dp神啊
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int M=300005;int T,n;int t,head[M];int f[M][2];struct edge{int to,nex;}e[M*2];void add(int u,int v){e[t].to=v,e[t].nex=head[u],head[u]=t++;}void dfs(int x,int fa){ int cnt=0; f[x][0]=f[x][1]=1; for(int i=head[x];i!=-1;i=e[i].nex){ int v=e[i].to; if(v==fa) continue; dfs(v,x); f[x][0]=min(f[x][0]+f[v][0],f[x][1]+f[v][1]-1); f[x][1]=min(f[x][1]+f[v][0],cnt+f[v][1]); cnt+=f[v][0]; }} void work(){ memset(f,0x3f,sizeof(f)); dfs(1,0); printf("%d\n",min(f[1][0],f[1][1]));}void init(){ scanf("%d",&n); int x,y;t=0;memset(head,-1,sizeof(head)); for(int i=1;i<n;i++) scanf("%d%d",&x,&y),add(x,y),add(y,x);}int main(){ scanf("%d",&T); while(T--) init(),work(); return 0;}
阅读全文
0 0
- 1907: 树的路径覆盖
- 树的路径覆盖
- bzoj 1907: 树的路径覆盖
- 1907: 树的路径覆盖 贪心
- BZOJ 1907: 树的路径覆盖|贪心
- BZOJ 1907 树的路径覆盖
- BZOJ 1907 树的路径覆盖
- bzoj 1907 树的路径覆盖 [贪心] [树的最小路径覆盖]
- FZU2185 树的路径覆盖
- 【BZOJ1907】树的路径覆盖
- BZOJ1907 树的路径覆盖
- 树的最小路径覆盖
- BZOJ1907: 树的路径覆盖
- 【bzoj1907】树的路径覆盖
- BZOJ 1907 树的路径覆盖 树形DP
- [树形DP || 贪心] BZOJ 1907 树的路径覆盖
- BZOJ 1907: 树的路径覆盖 树型dp
- FOJ 2185 树的路径覆盖 (图)
- BZOJ1040 [ZJOI2008]骑士
- 计算机比赛反思
- Codeforces Round #441 (Div. 2) D
- 2017.10.23.拓展欧几里得
- Cookie的基本概念及设置
- 1907: 树的路径覆盖
- Learning Spark笔记14-通过集群运行
- 【2767】2017年10月10日提高组T1 xjh的旅行
- python中字典操作II
- 1617: [Usaco2008 Mar]River Crossing渡河问题
- tableau计算字段编辑器中,字段名称不能正常显示。
- BZOJ4027 [HEOI2015]兔子与樱花
- C++11——【USACO 4.4.3】——Frame Up
- 利用递归方法输出1~10所有整数的阶乘