CF 369C . Valera and Elections tree dfs 好题
The 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.
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.
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.
5
1 2 2
2 3 2
3 4 2
4 5 2
1
5
5
1 2 1
2 3 2
2 4 1
4 5 1
1
3
5
1 2 2
1 3 2
1 4 2
1 5 2
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+;
inline int max(int a,int b)
{
return a>b?a:b;
} struct Edge
{
int to,next,w;
};
Edge edge[maxn<<];
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,-,sizeof head);
tot=;
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
if(w>)
w=;
addedge(u,v,w);
addedge(v,u,w);
}
solve(n);
return ;
} void solve(int n)
{
memset(use,,sizeof use);
dfs0(,);
dfs1(,,);
tot=;
for(int i=;i<=n;i++){
if(use[i])
print[tot++]=i;
}
printf("%d\n",tot-);
if(tot>){
for(int i=;i<tot-;i++)
printf("%d ",print[i]);
printf("%d\n",print[tot-]);
}
} void dfs0(int u,int pre)
{
siz[u]=;
son[u]=-;
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]==-||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);
}
}
CF 369C . Valera and Elections tree dfs 好题的更多相关文章
- CodeForces - 369C - Valera and Elections
369C - Valera and Elections 思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案. 代码: #include<bits/std ...
- cf C. Valera and Elections
http://codeforces.com/contest/369/problem/C 先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还 ...
- 369C Valera and Elections
http://codeforces.com/problemset/problem/369/C 树的遍历,dfs搜一下,从根节点搜到每个分叉末尾,记录一下路况,如果有需要修复的,就把分叉末尾的节点加入答 ...
- Codeforces 369 C Valera and Elections
Valera and Elections 题意:现在有n个候选人, 有n-1条路, 如果选择了这个候选人, 这个候选人就会将从自己这个城市到1号城市上所有坏的路都修复一下,现在求最小的候选人数目, 如 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ 1321 棋盘问题(DFS板子题,简单搜索练习)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44012 Accepted: 21375 Descriptio ...
- poj1564 Sum It Up dfs水题
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...
- 咸鱼的ACM之路:DFS水题集
DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...
- CF E. Vasya and a Tree】 dfs+树状数组(给你一棵n个节点的树,每个点有一个权值,初始全为0,m次操作,每次三个数(v, d, x)表示只考虑以v为根的子树,将所有与v点距离小于等于d的点权值全部加上x,求所有操作完毕后,所有节点的值)
题意: 给你一棵n个节点的树,每个点有一个权值,初始全为0,m次操作,每次三个数(v, d, x)表示只考虑以v为根的子树,将所有与v点距离小于等于d的点权值全部加上x,求所有操作完毕后,所有节点的值 ...
随机推荐
- 回调函数的实现 & 结构体的继承
------------------------------------------------------------------------------------[1]------------- ...
- 磁盘与目录的容量[转自vbird]
磁盘与目录的容量 现在我们知道磁盘的整体数据是在 superblock 区块中,但是每个各别文件的容量则在 inode 当中记载的. 那在文字接口底下该如何叫出这几个数据呢?底下就让我们来谈一谈这两个 ...
- 关于ASP.NET MVC4 Web API简单总结
原文地址:http://www.cnblogs.com/lei2007/archive/2013/02/01/2888706.html wcf web api 和 asp.net web api , ...
- codevs-1447取出整数的一部分
说实在的,这个题目真不想写了…… 1447 取出整数的一部分 题目描述 Description 假如有一个整数(int):145678,现在我做截取该数一部份的操作,如输入4,返回前4位即1456;如 ...
- mysql 多行合并一列
mysql 多行合并一列 使用的函数为: GROUP_CONCAT(exp) 其中exp 的参数类似如下: (field order by field desc separator ';') ...
- [Android设计模式]Android退出应用程序终极方法
如何干净彻底地退出Android应用程序,是很多开发者的心头痒.如何干净地关闭所有已打开的Activity? 如何关闭指定的Activity? 如何关闭一类Activity? 这里,我们提出一种通过实 ...
- postman使用教程
最近很多朋友在问postman的使用方法,现我经过整理,分享给大家. Postman 是一个很强大的 API调试.Http请求的工具,当你还准备拿着记事本傻傻的去写 Form 表单的时候,你来试试 P ...
- 解决504 Gateway Time-out(nginx)
504 Gateway Time-out问题常见于使用nginx作为web server的服务器的网站 我遇到这个问题是在需要插入一万多条数据时候遇到的 一般看来, 这种情况可能是由于nginx默认的 ...
- BI案例:BI在连锁零售业应用(ZT)
第一部分:连锁零售企业上BI的必要性. 目前国内的连锁零售行业的发展趋势,呈现出产业规模化,经营业态多样化,管理精细化的特点.所谓管理精细化就是"精耕细作搞管理,领先一步订系统". ...
- 史上最全Java表单验证封装类
package com.tongrong.utils; import java.util.Collection; import java.util.Map; import java.util.rege ...