Codefoces 369C.Valera and Elections

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

最水的树形DP(可能算不上树形DP)。。。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=2e5+345;

int fa[maxn];
int f(int x){return x==fa[x]?x:fa[x]=f(fa[x]);}
int d[maxn];
int n;
int head[maxn],next[maxn],to[maxn];
int e=0;
int w[maxn];
void add(int u,int v,int c)
{
    to[++e]=v;
    next[e]=head[u];
    head[u]=e;
    w[e]=c;
}
bool vis[maxn];
int sol[maxn];
int dp[maxn];


void dfs(int u,int pa)
{
    dp[u]=0;
    for(int i=head[u];i>=0;i=next[i])
    {
        int v=to[i];
        if(v==pa)continue;
        dfs(v,u);
        if(w[i]==2)
        {
            if(dp[v]==0)dp[u]+=dp[v]+1,vis[v]=1; ///zai v fang
            else
                dp[u]+=dp[v];
        }
        else
            dp[u]+=dp[v];
    }
}
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)fa[i]=i;
        memset(head,-1,sizeof head);
        memset(vis,0,sizeof vis);
        e=0;
        int ans=0;
        for(int i=1;i<n;i++)
        {
            int u,v,c;
            cin>>u>>v>>c;
            add(u,v,c);   add(v,u,c);
            int a=f(u);
            int b=f(v);


        }
        dfs(1,-1);
        cout<<dp[1]<<endl;
        ///for(int i=0;i<ans;i++)cout<<sol[i]<<' ';
        for(int i=1;i<=n;i++)
            if(vis[i])
            cout<<i<<' ';
        puts("");
    }
}


你可能感兴趣的:(Algorithm,dp,ACM,DFS,CF)