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,求所有操作完毕后,所有节点的值 ...
随机推荐
- 求解:php商品条件筛选功能你是怎么做出来的?
求解:php商品条件筛选功能你是怎么做出来的? 2013-09-25 13:43 chenhang607 | 浏览 2756 次 资源共享 求思路或者方法,最好能有些代码 2013-09-25 14: ...
- Android开发之多级下拉列表菜单实现(仿美团,淘宝等)
注:本文转载于:http://blog.csdn.net/minimicall/article/details/39484493 我们在常用的电商或者旅游APP中,例如美团,手机淘宝等等,都能够看的到 ...
- JSBinding+SharpKit / MonoBehaviour替换成JSComponent原理
Unity 是基于组件式的开发,gameObject 身上可以绑定任意个脚本.每个脚本组成 gameObject 的一个部分. 脚本里通过添加预定义好的函数来执行自己的任务.比如Awake,用于初始化 ...
- ORA-01089 数据库无法正常关闭
今天在做SOA几个数据库的重启操作,其中一个数据库在关闭过程中一直处于HANG住状态,十几分钟没有任何进展,具体操作过程如下: 一:当时的情景 SQL> shutdown immediate - ...
- 二十四种设计模式:享元模式(Flyweight Pattern)
享元模式(Flyweight Pattern) 介绍运用共享技术有效地支持大量细粒度的对象. 示例有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要运用共享技术支 ...
- curl命令使用大全
curl命令使用大全 可以看作命令行浏览器 1.开启gzip请求curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控 ...
- webstorm修改文件,webpack-dev-server不自动编译刷新的解决办法
webstorm设置中,"Settings"--"Appearance & Behavior"--"System Settings" ...
- SMO序列最小最优化算法
SMO例子: 1 from numpy import * 2 import matplotlib 3 import matplotlib.pyplot as plt 4 5 def loadDataS ...
- Transact-SQL 学习小结
1.把系统里所有用全局临时表的改成局部临时表,不然并发高时会引发对象已经存在的问题,不要用##要用#. 2.int 不能写成 id = '1',比如Select * from A where ID=' ...
- JAVA包命名规范
学习Java的童鞋们都知道,Java的包.类.接口.方法.变量.常量:JavaEE的三层模型等都有一套约定俗成的命名规则. 我学习每种语言都会关注相应的命名规则,一则体现自己比较专业:二来方便后检查, ...