CF369C(深搜巧用)

来源:互联网 发布:删除有条件的数据sql 编辑:程序博客网 时间:2024/05/21 17:23

地址:http://codeforces.com/contest/369/problem/C

C. Valera and Elections
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ n1 ≤ 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 tiequals 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
51 2 22 3 23 4 24 5 2
output
15 
input
51 2 12 3 22 4 14 5 1
output
13 
input
51 2 21 3 21 4 21 5 2
output
45 4 3 2 

题意:有n个地方由n-1条路相连(树),其中有些路是坏的。当选一块区域修路时,会把从该块区域到1号区域所经过的道路全部修好。问最少可以从几个区域开始修使得全部坏的路修好。

思路:本来是想自己写,但是路是双向的,所以写不动了。看了下大神的代码,好简便。

            记录现在答案数并向下深搜,当出现最底层节点时回溯,知道遇见第一个坏的道路时,判断答案数是否有增长。如无增长则说明给节点向下的节点中没有作为修路起点的节点。因此该节点可以添加到答案中。

代码:

#include<iostream>//#include<cmath>#include<vector>#include<cstdio>#include<cstring>//#include<algorithm>using namespace std;//#define M 11111vector<int> tree[100010],ans;void dfs(int st,int x,int t){    int i,y,z,l=ans.size();  //在这里保存已得到的ans的个数    for(i=0;i<tree[x].size();i++)    {        y=tree[x][i];        if(y<0) {z=2;y*=-1;}        else z=1;        if(y==st) continue;        dfs(x,y,z);    }    if(t==2&&ans.size()==l)  //这里判断条件有两个,一个是路是坏的,一个是ans的个数没有增加        ans.push_back(x);}int main(){    int i,n,x,y,t;    scanf("%d",&n);    for(i=0;i<n-1;i++)    {        scanf("%d%d%d",&x,&y,&t);        tree[x].push_back((3-2*t)*y);  //3-2*t大于0表示路没坏,小于0表示路坏了        tree[y].push_back((3-2*t)*x);    }    dfs(0,1,1);    printf("%d\n",ans.size());    for(i=0;i<ans.size();i++) printf("%d ",ans[i]);    return 0;}


原创粉丝点击