• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #216 Div2 C

C. Valera and Elections
time limit per test 1 second
memory limit per test 256 megabytes
 

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 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, then the i-th road isn't the problem road; if ti equals 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
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <vector>
 4 #include <set>
 5 #include <cstring>
 6 #include <string>
 7 #include <map>
 8 #include <cmath>
 9 #include <ctime>
10 #include <algorithm>
11 #include <queue>
12 
13 using namespace std;
14 #define INF 0x7fffffff
15 #define maxm 1001
16 #define mod 1000000007
17 #define mp make_pair
18 #define pb push_back
19 #define rep(i,n) for(int i = 0; i < (n); i++)
20 #define re return
21 #define fi first
22 #define se second
23 #define sz(x) ((int) (x).size())
24 #define all(x) (x).begin(), (x).end()
25 #define sqr(x) ((x) * (x))
26 #define sqrt(x) sqrt(abs(x))
27 #define y0 y3487465
28 #define y1 y8687969
29 #define fill(x,y) memset(x,y,sizeof(x))
30 
31 typedef vector<int> vi;
32 typedef long long ll;
33 typedef long double ld;
34 typedef double D;
35 typedef pair<int, int> ii;
36 typedef vector<ii> vii;
37 typedef vector<string> vs;
38 typedef vector<vi> vvi;
39 
40 template<class T> T abs(T x) { re x > 0 ? x : -x; }
41 const int maxn = 200015;
42 int n, m, t, k, x, l, r, s, sk,y;
43 struct edge{
44     int u, v, flag,next;
45 }edges[maxn];
46 int head[maxn],p[maxn],vis[maxn],v[maxn];
47 void init(){
48     for (int i = 0; i <= n; i++)head[i] = -1;
49 }
50 void add(int u, int v, int flag,int id){
51     edges[id].v = v; edges[id].flag = flag; edges[id].next = head[u]; head[u] = id;
52 }
53 int a[maxn], b[maxn];
54 void dfs(int u){
55     vis[u] = p[u];
56     if (v[u])return;
57     v[u] = 1;
58     for (int i = head[u]; i != -1; i = edges[i].next){
59         if (v[edges[i].v])continue;
60         if (edges[i].flag == 2)p[edges[i].v] = 1;
61         dfs(edges[i].v);
62         vis[u] += vis[edges[i].v];
63     }
64 }
65 int main(){
66     scanf("%d", &n);
67     init();
68     int cnt = 0;
69     for (int i = 0; i < n - 1; i++){
70         int u, v, op;
71         scanf("%d%d%d", &u, &v, &op);
72         add(u, v, op,i+1);
73         add(v, u, op, n+i);
74     }
75     dfs(1);
76     for (int i = 1; i <= n; i++)if (vis[i]==1&&p[i]==1)cnt++;
77     printf("%d\n", cnt);
78     for (int i = 1; i <= n; i++)if(vis[i]==1&&p[i]==1)printf("%d ", i);
79     return 0;
80 }
View Code
posted @ 2013-11-30 22:59  HaibaraAi  阅读(107)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3