369C.md (2465B)
1 Source: https://codeforces.com/problemset/problem/369/C 2 3 C. Valera and Elections 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 The city Valera lives in is going to hold elections to the city Parliament. 22 23 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. 24 25 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. 26 27 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. 28 29 Input 30 31 The first line contains a single integer *n* (2 ≤ *n* ≤ 105) — the number of districts in the city. 32 33 Then *n* - 1 lines follow. Each line contains the description of a city road as three positive integers *x**i*, *y**i*, *t**i* (1 ≤ *x**i*, *y**i* ≤ *n*, 1 ≤ *t**i* ≤ 2) — the districts connected by the *i*-th bidirectional road and the road type. If *t**i* equals to one, then the *i*-th road isn't the problem road; if *t**i* equals to two, then the *i*-th road is the problem road. 34 35 It's guaranteed that the graph structure of the city is a tree. 36 37 Output 38 39 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 *a*1, *a*2, ... *a**k* — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them. 40 41 Examples 42 43 Input 44 45 ``` 46 5 47 1 2 2 48 2 3 2 49 3 4 2 50 4 5 2 51 ``` 52 53 Output 54 55 ``` 56 1 57 5 58 ``` 59 60 Input 61 62 ``` 63 5 64 1 2 1 65 2 3 2 66 2 4 1 67 4 5 1 68 ``` 69 70 Output 71 72 ``` 73 1 74 3 75 ``` 76 77 Input 78 79 ``` 80 5 81 1 2 2 82 1 3 2 83 1 4 2 84 1 5 2 85 ``` 86 87 Output 88 89 ``` 90 4 91 5 4 3 2 92 ```