Codeforces Round #216 (Div. 2) C. Valera and Elections (树形dp)

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


思路:

从1出发进行搜索,然后儿子节点返回时统计值就好了,画一个大一点的树就能找到所有的情况了。

4种情况:

1.叶子节点,通过边1来访问,return 0;

2.叶子节点,通过边2来访问,return 1;

3.非叶子节点,通过边2来访问而且他的孩子统计为0(他的孩子都不需要加到集合里面),return 1;

4.非叶子节点,其他情况return 0。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 200005
#define MAXN 200005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.000001
typedef long long ll;
using namespace std;

int n,m,ans,cnt,cxx;
int pp[maxn],vis[maxn],a[maxn];
struct Node
{
    int v,next;
    int k;
} edge[maxn];

void addedge(int u,int v,int k)
{
    cnt++;
    edge[cnt].v=v;
    edge[cnt].k=k;
    edge[cnt].next=pp[u];
    pp[u]=cnt;
}
int dfs(int u,int k)
{
    int i,j,v,t,flag=0,res=0;
    for(i=pp[u]; i; i=edge[i].next)
    {
        v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            flag=1;
            res+=dfs(v,edge[i].k);
        }
    }
    if(!flag)
    {
        if(k==2)
        {
            a[++cxx]=u;
            return 1;
        }
        return 0;
    }
    if(res==0&&k==2)
    {
        a[++cxx]=u;
        return 1;
    }
    return res;
}
int main()
{
    int i,j,t,x,y;
    while(~scanf("%d",&n))
    {
        cnt=cxx=0;
        memset(pp,0,sizeof(pp));
        for(i=1; i<n; i++)
        {
            scanf("%d%d%d",&x,&y,&t);
            addedge(x,y,t);
            addedge(y,x,t);
        }
        memset(vis,0,sizeof(vis));
        vis[1]=1;
        ans=dfs(1,1);
        printf("%d\n",ans);
        if(ans)
        {
            for(i=1; i<ans; i++)
            {
                printf("%d ",a[i]);
            }
            printf("%d\n",a[i]);
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值