• C. Valera and Elections 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 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
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 300000;
    struct Edge
    {
        int from,to,zhi;
    };
    vector<Edge> edges;
    vector<int> g[maxn];
    vector<int> G[maxn];
    void addEdge(int from,int to,int zhi)
    {
        edges.push_back((Edge){from,to,zhi});
        edges.push_back((Edge){to,from,zhi});
        int num = edges.size();
        g[from].push_back(num - 2);
        g[to].push_back(num - 1);
    }
    int vis[maxn];
    int is[maxn];
    void dfs1(int x)
    {
        rep(i,0,g[x].size())
        {
            int v = edges[g[x][i]].to;
            if(vis[v] == 0)
            {
                vis[v] = 1;
                G[x].push_back(g[x][i]);
                dfs1(v);
            }
        }
    }
    int have[maxn];
    int dfs(int x)
    {
        if(G[x].size() == 0)
        {
            have[x] = 0;
            return is[x];
        }
        int flag = 0;
        rep(i,0,G[x].size())
        {
            int v = edges[G[x][i]].to;
            flag += dfs(v);
        }
        if(flag)
            have[x] = 1;
        else
            have[x] = 0;
        if(is[x]) return 1;
        return have[x];
    }
    vector<int> ans;
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int n;
        while(cin>>n)
        {
            clr(is);
            clr(have);
            repf(i,1,n-1)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                if(c == 2)
                {
                    is[a] = 1;
                    is[b] = 1;
                }
                addEdge(a,b,c);
            }
            clr(vis);
            vis[1] = 1;
            dfs1(1);
            clr(vis);
            clr(have);
            dfs(1);
            int Ans = 0;
            repf(i,1,n)
                if(have[i] == 0 && is[i])
                {
                    Ans++;
                    ans.push_back(i);
                }
            cout<<Ans<<endl;
            rep(i,0,ans.size())
                cout<<ans[i]<<" ";
            if(Ans == 0) continue;
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    (zt)再给正直一次机会(最新进展)
    迁勇
    一本书、四部电影
    巴乔到北京了
    MLDN
    (zt)沉默是美德(转自连岳)
    十分钟
    不推荐两部电影
    Project Processing ...... Requirement
    Oracle Data Guard Linux 平台 Logical Standby 创建实例
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3451725.html
Copyright © 2020-2023  润新知