CodeForces - 369C - Valera and Elections

369C - Valera and Elections

思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=1e5+5;
vector<int>g[N];
vector<int>edge[N];
vector<int>ans; 
int dfs(int o,int u)
{
    int res=0;
    for(int i=0;i<g[u].size();i++)
    {
        if(g[u][i]!=o)
        {
            int temp=dfs(u,g[u][i]);
            if(temp==0&&edge[u][i]==2)
            {
                res++;
                ans.push_back(g[u][i]);
            }
            res+=temp;
        }
    }
    return res;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0,x,y,t;i<n-1;i++)
    {
        cin>>x>>y>>t;
        g[x].pb(y);
        g[y].pb(x);
        edge[x].pb(t);
        edge[y].pb(t);
    }
    dfs(0,1);
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++)
    {
        cout<<ans[i];
        if(i!=ans.size()-1)cout<<' ';
        else cout<<endl; 
    }
    cout<<endl;
    return 0;
}

 

posted @ 2017-08-03 15:53  Wisdom+.+  阅读(306)  评论(0编辑  收藏  举报