C. Valera and Elections
 

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.

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 xiyiti (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 2
output
1
5
input
5
1 2 1
2 3 2
2 4 1
4 5 1
output
1
3
input
5
1 2 2
1 3 2
1 4 2
1 5 2
output
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 好题的更多相关文章

  1. CodeForces - 369C - Valera and Elections

    369C - Valera and Elections 思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案. 代码: #include<bits/std ...

  2. cf C. Valera and Elections

    http://codeforces.com/contest/369/problem/C 先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还 ...

  3. 369C Valera and Elections

    http://codeforces.com/problemset/problem/369/C 树的遍历,dfs搜一下,从根节点搜到每个分叉末尾,记录一下路况,如果有需要修复的,就把分叉末尾的节点加入答 ...

  4. Codeforces 369 C Valera and Elections

    Valera and Elections 题意:现在有n个候选人, 有n-1条路, 如果选择了这个候选人, 这个候选人就会将从自己这个城市到1号城市上所有坏的路都修复一下,现在求最小的候选人数目, 如 ...

  5. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  6. POJ 1321 棋盘问题(DFS板子题,简单搜索练习)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44012   Accepted: 21375 Descriptio ...

  7. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  8. 咸鱼的ACM之路:DFS水题集

    DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...

  9. 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. N皇后回溯解法 leetcode N-Queens

    class Solution { public: vector<vector<string> > solveNQueens(int n) { vector<vector& ...

  2. linux内核启动流程[转]

    启动流程一览 既然启动是很严肃的一件事,那我们就来了解一下整个启动的过程吧! 好让大家比较容易发现启动过程里面可能会发生问题的地方,以及出现问题后的解决之道! 不过,由於启动的过程中,那个启动管理程序 ...

  3. dir:一行代码,提取出所有视频文件名称及路径

    某次,部门接到一个任务,要求对公司现有的视频文件资料做一个统计整理分类的工作. 领导召集开会,问:两周时间够用吗? 统计整理分类工作的第一步骤是把视频文件名称来源类别信息录入到 excel 表格中,才 ...

  4. 在div中设置文字与内部div垂直居中

    要实现如图一所示的结果: html代码如下: <!DOCTYPE html> <html> <head lang="zh"> <meta ...

  5. httplib

    可爆破目录 import httplib import urllib def sendhttp(): data = urllib.urlencode({'@number': 12524, '@type ...

  6. 如何用ABBYY把PDF转换成PPT

    在电子科技迅速发展的今天,文件格式转换并不是什么稀罕事,因为现在都是电子化办公,出现很多文件格式,但是不同的场合需要的格式不同,所以常常需要进行文件格式的转换.PDF转换成PPT也是众多文件格式转换中 ...

  7. 如何处理ABBYY中出现错误代码142和55的问题

    在使用ABBYY FineReader 12OCR文字识别软件创建PDF文件时,有时会出现以下错误提示:内部程序错误..\Src\SpecialFontFactory.cpp,142和内部程序错误.. ...

  8. CorelDRAW中拆清除调和效果的技巧

    图形对象应用的调和效果达不到用户的满意,可以将该种调和效果清除,清除调和效果后,只保留起始对象和结束对象.CorelDRAW软件支持两种方法来清除调和对象,本教程将详解CorelDRAW中清除调和效果 ...

  9. 二十四种设计模式:提供者模式(Provider Pattern)

    提供者模式(Provider Pattern) 介绍为一个API进行定义和实现的分离.示例有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库 ...

  10. Web前端开发笔试&面试_03

    WL: 1.如何显示.隐藏一个dom对象? 2.如何将一个网页中的内容水平置中?写出重要的html标签和css. (css:#content{align:center;float:left;}html ...