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,求所有操作完毕后,所有节点的值 ...
随机推荐
- 【转】轻松搞定FTP之FlashFxp全攻略
转载网址:http://www.newhua.com/2008/0603/39163.shtml 轻松搞定FTP之FlashFxp全攻略 导读: FlashFXP是一款功能强大的FXP/FTP软件,融 ...
- (转) TensorFlow深度学习,一篇文章就够了
TensorFlow深度学习,一篇文章就够了 2016/09/22 · IT技术 · TensorFlow, 深度学习 分享到:6 原文出处: 我爱计算机 (@tobe迪豪 ) 作者: 陈迪 ...
- C# 二维list
public class ValueList : List<double> { public ValueList() { } } public ValueList[] valListArr ...
- 如何让div水平垂直居中
引子 我们经常遇到需要把div中的内容进行水平和垂直居中.所以,这里介绍一种方法,可以使div水平居中和垂直居中. 代码: <!DOCTYPE html> <html lang=&q ...
- HTML,XML中的转义字符
HTML中的转义字符 HTML中<, >,&等有特别含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这三个字符时,应使用他们的转义序列,如下所示: & ...
- 使用JavaScript 实现注册表单的校验
说明:在点击提交时进行表单校验,具体要求如下: 1)用户名为3~16个字符,且不能包含”@”和”#”字符: 2)密码和校验密码必须一致,且长度在8个字符到16个字符: 3)兴趣爱好至少选择一项: 4) ...
- HTML5之Canvas绘图——使用Canvas绘制图形的基本教程
原文转自:http://www.cnblogs.com/picaso/archive/2012/11/26/2789077.html HTML5火的正热,最近有个想法也是要用到HTML的相关功能,所以 ...
- 运行Appium碰到的坑们
运行Appium的时候,碰到的那些坑 1. java命令会出现error:could not open ...jvm.cfg 出现这种情况大多是因为电脑上之前安装过JDK,卸载重装之后,运行java命 ...
- 通过改变计算机策略来解决“只能通过Chrome网上应用商店安装该程序”的方法及模版文件下载
通过改变计算机策略来解决“只能通过Chrome网上应用商店安装该程序”的方法及模版文件下载 操作步骤 1.开始 -> 运行 -> 输入gpedit.msc -> 回车确定打开计算机本 ...
- SpringQtz 时间任务调度
1.配置所需要maven jar包 <!-- 任务调度需要的jar包--> <dependency> <groupId>org.quartz-scheduler&l ...