CodeForces - 369C - Valera and Elections
阿新 • • 發佈:2017-08-03
cti als space cto bits tdi std its pro
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; }
CodeForces - 369C - Valera and Elections