CF 369C . Valera and Elections tree dfs 好题
2015-08-24 23:35
183 查看
C. Valera and ElectionsThe city Valera lives in is going to hold elections to the city Parliament.The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each road the residents decided if it is the problem road or not. A problem road is a road that needs to be repaired.There are n candidates running the elections. Let's enumerate all candidates in some way by integers from 1 to n, inclusive. If the candidate number i will be elected in the city Parliament, he will perform exactly one promise — to repair all problem roads on the way from the i-th district to the district 1, where the city Parliament is located.Help Valera and determine the subset of candidates such that if all candidates from the subset will be elected to the city Parliament, all problem roads in the city will be repaired. If there are several such subsets, you should choose the subset consisting of the minimum number of candidates.Input
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of districts in the city.Then n - 1 lines follow. Each line contains the description of a city road as three positive integers xi, yi, ti (1 ≤ xi, yi ≤ n,1 ≤ ti ≤ 2) — the districts connected by the i-th bidirectional road and the road type. If ti equals to one, then the i-th road isn't the problem road; if ti equals to two, then the i-th road is the problem road.It's guaranteed that the graph structure of the city is a tree.Output
In the first line print a single non-negative number k — the minimum size of the required subset of candidates. Then on the second line print k space-separated integers a1, a2, ... ak — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.Sample test(s)input
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of districts in the city.Then n - 1 lines follow. Each line contains the description of a city road as three positive integers xi, yi, ti (1 ≤ xi, yi ≤ n,1 ≤ ti ≤ 2) — the districts connected by the i-th bidirectional road and the road type. If ti equals to one, then the i-th road isn't the problem road; if ti equals to two, then the i-th road is the problem road.It's guaranteed that the graph structure of the city is a tree.Output
In the first line print a single non-negative number k — the minimum size of the required subset of candidates. Then on the second line print k space-separated integers a1, a2, ... ak — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.Sample test(s)input
5 1 2 2 2 3 2 3 4 2 4 5 2output
1 5input
5 1 2 1 2 3 2 2 4 1 4 5 1output
1 3input
5 1 2 2 1 3 2 1 4 2 1 5 2output
4 5 4 3 2 题意: 一个城市有n快区域,编号为1~n,这n块区域刚好是一个树的结构 有n-1条路,其中部分路是好的,部分是坏的,坏的路需要重修 现在城市要进行一个选举,要从n个候选人中选出部分人,这n个人也是编号为1~n 第i个人如果成功的话,他会把区域i到区域1之间的坏的道路修好 现在这个城市的人希望,从这个n个人选出一个尽量少的集合,并且这个集合可以把所有坏的路修好 如果方案不止一个,输出其中任意一个方案 明明是水题,我却做了很久(捂脸) 这道题相当于: 一棵树,以节点1为root 现在要从根节点引出尽量少的若干条路径,这些路径能够覆盖所有坏的边 然后输出路径的条数,和每条路径的终点节点 siz[i] : 以i为根的子树中, 坏的边的条数 son[i] : i的所有儿子节点j中,siz[j]最大的j就是son[i],即son[i]=max(siz[j]) use[i] : 若i为其中一条路径的终点,use[i]=true print[i] : 为了方便输出答案的数组 2次dfs dfs0 : 求出siz,son dfs1 : 求出use 注意:当所有道路都是好的的时候,引出的路径为0条
#include<cstdio> #include<cstring> using namespace std; const int maxn=1e5+5; inline int max(int a,int b) { return a>b?a:b; } struct Edge { int to,next,w; }; Edge edge[maxn<<1]; int head[maxn]; int tot; int siz[maxn]; int son[maxn]; int use[maxn]; int print[maxn]; void addedge(int u,int v,int w) { edge[tot].to=v; edge[tot].w=w; edge[tot].next=head[u]; head[u]=tot++; } void solve(int ); void dfs0(int ,int ); void dfs1(int ,int ,int ); int main() { memset(head,-1,sizeof head); tot=0; int n; scanf("%d",&n); for(int i=1;i<n;i++) { int u,v,w; scanf("%d %d %d",&u,&v,&w); if(w>1) w=0; addedge(u,v,w); addedge(v,u,w); } solve(n); return 0; } void solve(int n) { memset(use,0,sizeof use); dfs0(1,0); dfs1(1,0,1); tot=1; for(int i=1;i<=n;i++){ if(use[i]) print[tot++]=i; } printf("%d\n",tot-1); if(tot>1){ for(int i=1;i<tot-1;i++) printf("%d ",print[i]); printf("%d\n",print[tot-1]); } } void dfs0(int u,int pre) { siz[u]=0; son[u]=-1; for(int i=head[u];~i;i=edge[i].next) { int v=edge[i].to; if(v==pre) continue; int w=edge[i].w; if(!w) siz[u]++; dfs0(v,u); siz[u]+=siz[v]; if(son[u]==-1||siz[v]>siz[son[u]]) v=son[u]; } } void dfs1(int u,int pre,int w) { if(!w) use[u]=true; if(!siz[u]) return ; else{ use[son[u]]=use[u]; use[u]=false; } for(int i=head[u];~i;i=edge[i].next) { int v=edge[i].to; if(v==pre) continue; int w=edge[i].w; dfs1(v,u,w); } }View Code
相关文章推荐
- Linux下使用云笔记
- 区间赋值状态压缩线段树
- DLL导出函数和类 之 __declspec(dllexport)
- 查询语句实例
- Zookeeper 简单操作
- 近期打算整理的几篇文章
- javaScript实现动态取得不同的验证码
- 内省(反射)
- 2049 不容易系列之(4)——考新郎【错排+排列组合】
- latex简历
- Qt 常用类 (10)—— QDialog
- 对话框--QDialog(自定义对话框简单实例)
- HDU 4081Qin Shi Huang's National Road System(最小生成树+最小瓶颈路)
- XCODE6 提交至 App Store
- 关于JDK配置环境变量的问题
- 20150824朴素贝叶斯.md
- 151. Reverse Words in a String
- RPG黑暗之光(3)EasyTouch插件的使用、角色控制、相机跟随、状态记录
- 二维线段树区域修改,最大值最小值
- Hibernate 关系映射