The city Valera lives in are going to the Parliament of elections to the city.
The city has n districts and N?-? 1 bidirectional roads. We know that from any district there are a path along the roads to any other district. Let's enumerate all districts in some is integers from 1 to n, inclusive. Furthermore, for each road the residents decided if it's the problem road or not. A problem road is a road this needs to be repaired.
There is n candidates running the elections. Let's enumerate all candidates in some is integers from 1 to n, inclusive. If The candidate number I'll be elected in the city Parliament, he'll perform exactly one promise-to repair all prob Lem roads on the "the" from the I-th district to the District 1, where the city Parliament is located.
Help Valera and determine the subset of candidates such so if all candidates from the subset would be elected to the city Parliament, all problem roads in the city would be repaired. If There is 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 xi, Yi, Ti (1?≤?xi,?yi?≤?n, 1?≤?ti?≤?2)-the Districts connected by the I-th bidirectional Road and the road type. If ti equals to one and then the i-th road isn ' t the problem Road; If Ti equals to and then the I-th road is the problem road.
It's guaranteed that's 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 so form the Requir Ed subset. If There is multiple solutions, you is 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
DFS can be labeled on a node over and over again
/************************************************************************* > File name:cf-216-c.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 07 Tuesday 11:15 05 seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Const intN =100100; vector <PLL>Edge[n]; vector <int>AnsBOOLGot[n];voidDfsintUintFA) {intSize = Edge[u].size (); for(inti =0; i < size; ++i) {intv = edge[u][i].first;intflag = Edge[u][i].second;if(v = = FA) {Continue; } dfs (v, u);if(Flag = =2) {if(!got[v]) {Ans.push_back (v); GOT[V] =1; }} Got[u] = Got[u] | | GOT[V]; }}intMain () {intN while(~scanf("%d", &n)) {memset(Got,0,sizeof(got)); for(inti =1; I <= N; ++i) {edge[i].clear (); }intU, V, flag; for(inti =1; I <= N-1; ++i) {scanf("%d%d%d", &u, &v, &flag); Edge[u].push_back (Make_pair (v, flag)); Edge[v].push_back (Make_pair (U, flag)); } ans.clear (); Dfs1, -1);intSize = Ans.size ();printf("%d\n", size); for(inti =0; i < size; ++i) {printf("%d", Ans[i]);if(I < size-1) {printf(" "); }Else{printf("\ n"); } } }return 0;}
Codeforces Round #216 (Div. 2)---c. Valera and elections