zoj_最少点支配
2017-05-15 16:19
162 查看
Prohibition
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
Many people know Berland, the country that has been described at Saratov's contests for ten years. But this problem tells us about another country ― Beerland. Beerland has n cities
and n - 1bidirectional roads such that any city is reachable from the others, and the distance between two cities that are directly connected by a road can be passed in one day.
Despite the country's name, its new president introduced a prohibition. It is clear that atmosphere in the cities was heated up after that. To prevent the rebellion, the president decided
to place military squads at some cities in such a way that any city, if the rebellion fires up there, could be protected by some squad no later than in one day. Of course, the president cares about the budget of the country, so the number of military squads
should be minimal.
The first line of the input contains one integer n (1 <= n <= 100) - the number of cities in Beerland.
Each of next n - 1 lines contains two integers a and b (1 <= a, b <= n, a ≠ b) - the numbers of directly connected cities. Each
road is described exactly once.
If there are many possible solutions, you can output any of them.
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
Many people know Berland, the country that has been described at Saratov's contests for ten years. But this problem tells us about another country ― Beerland. Beerland has n cities
and n - 1bidirectional roads such that any city is reachable from the others, and the distance between two cities that are directly connected by a road can be passed in one day.
Despite the country's name, its new president introduced a prohibition. It is clear that atmosphere in the cities was heated up after that. To prevent the rebellion, the president decided
to place military squads at some cities in such a way that any city, if the rebellion fires up there, could be protected by some squad no later than in one day. Of course, the president cares about the budget of the country, so the number of military squads
should be minimal.
Input
There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:The first line of the input contains one integer n (1 <= n <= 100) - the number of cities in Beerland.
Each of next n - 1 lines contains two integers a and b (1 <= a, b <= n, a ≠ b) - the numbers of directly connected cities. Each
road is described exactly once.
Output
Output n numbers separated by space. The i-th number should be equal to 1 if the president should place a military squad in the i-th city, and 0 otherwise.If there are many possible solutions, you can output any of them.
Sample Input
3 3 1 2 2 3 4 4 2 2 1 1 3 7 1 2 2 3 3 5 4 2 5 7 6 5
Sample Output
0 1 0 1 1 0 0 0 1 0 0 1 0 0
贪心思路+dfs从叶子结点开始
#include<bits/stdc++.h> using namespace std; #define maxn 205 struct Node{ int to; int next; Node():to(0),next(0){ } }node[maxn]; int head[maxn],cnt=0,select[maxn],Set[maxn],p[maxn],now=0,newpos[maxn],n; int add_edge(int a,int b){ node[cnt].to=b; node[cnt].next=head[a]; head[a]=cnt++; node[cnt].to=a; node[cnt].next=head[b]; head[b]=cnt++; return 0; } void init(){ memset(node,0,sizeof(node)); memset(head,-1,sizeof(head)); cnt=0; memset(select,0,sizeof(select)); memset(Set,0,sizeof(Set)); memset(newpos,0,sizeof(newpos)); memset(p,0,sizeof(p)); now=0; } int dfs(int x){ newpos[now++]=x; for(int k=head[x];k!=-1;k=node[k].next){ int b=node[k].to; if(!select[b]){ select[b]=1; p[b]=x;//b is one of x's sons dfs(b); } } return 0; } int greedy(){ int s[maxn]; memset(s,0,sizeof(s)); int res=0; for(int i=n-1;i>=0;i--){ int t=newpos[i]; if(!s[t]){ if(!Set[p[t]]){ Set[p[t]]=1;//set i-th as one of V's vertex res++; } s[t]=1; s[p[t]]=1; s[p[p[t]]]=1; } } return res; } int main(){ int t; cin>>t; while(t--){ cin>>n; init(); for(int i=1;i<n;i++){ int a,b; cin>>a>>b; add_edge(a,b); } select[1]=1; p[1]=1; dfs(1); greedy(); for(int i=1;i<=n;i++){ cout<<Set[i]; if(i!=n)cout<<" "; } cout<<endl; } return 0; }
相关文章推荐
- [ACM_搜索] ZOJ 1103 || POJ 2415 Hike on a Graph (带条件移动3盘子到同一位置的最少步数 广搜)
- zoj 3209 Treasure Map 精确覆盖 DLX 给一些指定位置的矩形,使用最少的矩形去精确覆盖一个大的矩形,矩形间不能重叠。
- zoj 3130 最小费用最大流 (求 s到e 的两条总花费最少的 完全没有交点的 路径)
- ZOJ 1889 n的倍数使11111…位数最少
- ZOJ 1045 题目大意:叠放卡片,使上面第一张盖住第二张的1/2,第二张盖第三张的1/3,依次类推,输出达到c的最少卡片数
- zoj 2027 Travelling Fee 求a->b的最少花费(去除路上的最大花费后的费用)
- Cong! ZOJ 100AC!
- ZOJ 1926 Guessing Game
- ZOJ 3829 (2014 牡丹江现场赛 K 题)
- ZOJ 3332 Strange Country II
- [zoj] 1081 Points Within || 判断点是否在多边形内
- zoj 3543 Number String 已知排列数的增减顺序,求排列数的个数
- zoj 1576 Marriage is Stable
- ZOJ 3826 Hierarchical Notation Hash+模拟
- zoj3876 May Day Holiday(判断闰年)
- ZOJ1649 营救Rescue (BFS)
- zoj 1831
- zoj 1657
- zoj 2314 无源无汇有上下限的可行流
- 66.广搜练习:最少关卡路