Codeforces 369C DFS

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 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.

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.

Examples
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 

题意:给一棵树,树上有些路径是坏的,需要修复,从任意一个节点走到根节点可以修复这条路径上的所有路径,问至少需要几个节点走到根节点,才能修复每一条路。

方法:深搜。递归查找该节点的左右子树是否存在需要修复的路径,在通过一次深搜,统计答案。

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[1500000];
int cont,cnt;
int n;
int del[105000];
int dp[105000];
int ans[105000];
int head[105000];

void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}

void Dfs(int u,int from)
{
    dp[u]=0;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        dp[u]=max(dp[u],w);
        if(v==from)continue;
        Dfs(v,u);
        dp[u]=max(dp[u],dp[v]);
    }
}

void Dfs2(int u,int from,int sum)
{
    if(sum==1)del[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(v==from)
            continue;
        Dfs2(v,u,w);
        if(dp[v]==1)del[u]=0;
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        cnt=0;
        cont=0;
        memset(del,0,sizeof(del));
        memset(dp,0,sizeof(dp));
        memset(head,-1,sizeof(head));
        for(int i=0;i<n-1;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            add(x,y,w-1);
            add(y,x,w-1);
        }
        Dfs(1,-1);
        Dfs2(1,-1,0);
        for(int i=1;i<=n;i++)
        {
            if(del[i]==1)
                ans[cnt++]=i;
        }
        printf("%d\n",cnt);
        for(int i=0;i<cnt;i++)
            printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值