BZOJ 1907: 树的路径覆盖|贪心
2016-02-03 11:22
288 查看
来自大胆的yy:贪心随便构造出一种路径覆盖的情况都行,只要满足路径的端点所连的其他点不是其他路径的端点就行!!!
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<vector> #include<queue> #include<iostream> #include<algorithm> #include<set> #include<map> #define T 22222 using namespace std; int sc() { int i=0;char c=getchar(); while(c>'9'||c<'0')c=getchar(); while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar(); return i; } int head[T],lst[T],nxt[T],v[T],mark[T]; int n,tot; void insert(int x,int y) { lst[++tot]=y; nxt[tot]=head[x]; head[x]=tot; } void dfs(int x,int f) { v[x]=1; int cnt=0; for(int i=head[x];i;i=nxt[i]) if(lst[i]!=f) { dfs(lst[i],x); v[x]+=v[lst[i]]; if(!mark[lst[i]])cnt++; } if(cnt>=2)v[x]-=2,mark[x]=1; else if(cnt==1)v[x]--; } int main() { int Q=sc(); while(Q--) { memset(head,0,sizeof(head)); memset(v,0,sizeof(v)); memset(mark,0,sizeof(mark)); tot=0;n=sc(); for(int i=1;i<n;i++) { int x=sc(),y=sc(); insert(x,y); insert(y,x); } dfs(1,0); printf("%d\n",v[1]); } return 0; }
相关文章推荐
- 1.一些 贪心算法 的简单思维题:
- 贪心算法——字典序最小问题
- 贪心算法——区间调度问题
- Jump Game I,II 贪心
- Wildcard Matching
- 贪心法实现无向图的划分 代码
- 贪心题目循环和控制台折行
- HDOJ 1009
- 【解题报告】【USACO】酸奶工厂
- 【解题报告】【】交谊舞
- POJ 1328
- Best Cow Line
- HDU 1009 Fatmouse's Trade
- POJ2377 Bad Cowtractors
- 贪心 hdu 1003
- 最大容器
- 满足和为定值的两个数或多个数
- Codeforces Round #300
- HUD1052 __ P2 1002 __ Tian Ji -- The Horse Racing
- 2015年国家集训队测试 BZOJ3816矩阵变幻