Codeforces Round #216 (Div. 2)---C. Valera and Elections

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

dfs一遍即可,对节点进行标记

/*************************************************************************
    > File Name: CF-216-C.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月07日 星期二 11时15分05秒
 ************************************************************************/

#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 double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100100;
vector <PLL> edge[N];
vector <int> ans;
bool got[N];

void dfs(int u, int fa)
{
    int size = edge[u].size();
    for (int i = 0; i < size; ++i)
    {
        int v = edge[u][i].first;
        int flag = 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];
    }
}

int main()
{
    int n;
    while (~scanf("%d", &n))
    {
        memset(got, 0, sizeof(got));
        for (int i = 1; i <= n; ++i)
        {
            edge[i].clear();
        }
        int u, v, flag;
        for (int i = 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();
        dfs(1, -1);
        int size = ans.size();
        printf("%d\n", size);
        for (int i = 0; i < size; ++i)
        {
            printf("%d", ans[i]);
            if (i < size - 1)
            {
                printf(" ");
            }
            else
            {
                printf("\n");
            }
        }
    }
    return 0;
}
时间: 2024-11-12 13:35:52

Codeforces Round #216 (Div. 2)---C. Valera and Elections的相关文章

Codeforces Round #216 (Div. 2) E. Valera and Queries (BIT)

题目大意: 给出很多条分布在 x 轴上的线段. 然后给出很多点集,问这些点集分布在多少条不同的线段上. 思路分析: 把点集分散成若干条线段. 如果点集做出的线段包含了某一条给出的线段的话,也就是说这个点集上不会有点在这条线段上. 所以我们就是求出 点集做出的线段包含了多少个给出的线段就可以了. 那么也就是比较l r的大小,排序之后用BIT #include <cstdio> #include <iostream> #include <algorithm> #includ

Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求不包含这个cnt个点的线段的总数, 那么不包含这些点的线段必然在cnt个点之间(这里需要再加两个点一个是0, 一个是MAX), 我们可以把所有线段按Ri 分类, 然后按右端点遍历,对于当前的线段可以在Li 处+1, 然后对于每一次询问中两个点(x, y)之间线段的个数, 只需要查询 左端点大于等于x

Codeforces Round #252 (Div. 2) A - Valera and Antique Items

水题 #include <iostream> #include <set> #include <vector> #include <algorithm> using namespace std; int main(){ int n,v; cin >> n >> v; vector<int> res; for(int i = 0; i < n; ++ i){ int k,s; bool flag = false; ci

Codeforces Round #216 (Div. 2)

以后争取补题不看别人代码,只看思路,今天就是只看思路补完的题,有点小激动. A. Valera and Plates 水题,贪心地先放完第一种食物,在考虑第二种. 居然被卡了一会,心态要蹦 :(: #include<bits/stdc++.h> using namespace std; int n,m,k; int main() { cin>>n>>m>>k; int cnt1=0,cnt2=0; for(int i=1;i<=n;i++) { int

codeforces Round #252 (Div. 2) C - Valera and Tubes

贪心算法,每条路径最短2格,故前k-1步每次走2格,最后一步全走完 由于数据比较小,可以先打表 #include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; typedef pair<int,int> Point; int main(){ int n, m, k, flag = -1; cin >>

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代

Codeforces Round #318 (Div. 2) A Bear and Elections

优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main() { int n; scanf("%d",&n); int t; scanf("%d",&t); for(int i = 2; i <= n; i++){ int v; scanf("%d",&v); q.push(v

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i