1907: 树的路径覆盖 贪心
来源:互联网 发布:淘宝网违规处理一览表 编辑:程序博客网 时间:2024/05/06 00:11
从底向上,能合并就合并,标记一下经过根节点的路径是哪种路径(就两种很好yy)。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,cnt,T;int head[10005],list[20005],next[20005];int f[10005],v[10005];inline int read(){ int a=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();} return a*f;}inline void insert(int x,int y){ next[++cnt]=head[x]; head[x]=cnt; list[cnt]=y;}void dfs(int x,int fa){ int tmp=0; f[x]=1; for (int i=head[x];i;i=next[i]) if (list[i]!=fa) { dfs(list[i],x); f[x]+=f[list[i]]; if (!v[list[i]]) tmp++; } if (tmp>=2) f[x]-=2,v[x]=1; else if (tmp==1) f[x]--;}int main(){ T=read(); while (T--) { n=read(); cnt=0; memset(head,0,sizeof(head)); memset(f,0,sizeof(f)); memset(v,0,sizeof(v)); for (int i=1;i<n;i++) { int u=read(),v=read(); insert(u,v); insert(v,u); } dfs(1,0); printf("%d\n",f[1]); } return 0;}
0 0
- 1907: 树的路径覆盖 贪心
- BZOJ 1907: 树的路径覆盖|贪心
- bzoj 1907 树的路径覆盖 [贪心] [树的最小路径覆盖]
- [树形DP || 贪心] BZOJ 1907 树的路径覆盖
- 【bzoj1907】【树的路径覆盖】【贪心】
- 【BZOJ1907】树的路径覆盖【贪心】
- [BZOJ1907]树的路径覆盖(贪心)
- 【BZOJ】【P1907】【树的路径覆盖】【题解】【贪心】
- [BZOJ1907]树的路径覆盖(贪心||树形dp)
- Fzu 2185 树的路径覆盖【思维+贪心】好题~
- bzoj 1907: 树的路径覆盖 (贪心+dfs+树形dp)
- 1907: 树的路径覆盖
- 树的路径覆盖
- bzoj 1907: 树的路径覆盖
- BZOJ 1907 树的路径覆盖
- BZOJ 1907 树的路径覆盖
- FZU2185 树的路径覆盖
- 【BZOJ1907】树的路径覆盖
- MTP in Android详解
- LeetCode #100 Same Tree
- 用AFNetworking3.0封装网络请求
- 树莓派报GPG签名错误
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
- 1907: 树的路径覆盖 贪心
- lwip tcp编程
- 堆栈简析
- Xcode开发技巧之Code Snippets Library
- 加载巨图不压缩(BitmapRegionDecoder, DestureDetetor)
- hdu-2795-Billboard- 线段树-单点
- xshell运行函数与参数时对应说明
- IOS视频压缩
- git 的一些使用命令