CodeForces 369C. Valera and Elections


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

The city Valera lives in is going to hold elections to the city Parliament.

The city hasndistricts andn - 1bidirectional 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 from1ton, inclusive. Furthermore, for each road the residents decided if it is the problem road or not.A problem roadis a road that needs to be repaired.

There arencandidates running the elections. Let's enumerate all candidates in some way by integers from1ton, inclusive. If the candidate numberiwill be elected in the city Parliament, he will perform exactly one promise — to repair all problem roads on the way from thei-th district to the district1, 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 integern(2 ≤ n ≤ 105) — the number of districts in the city.

Thenn - 1lines follow. Each line contains the description of a city road as three positive integersxi,yi,ti(1 ≤ xi, yi ≤ n,1 ≤ ti ≤ 2) — the districts connected by thei-th bidirectional road and the road type. Iftiequals to one, then thei-th road isn't the problem road; iftiequals to two, then thei-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 numberk— the minimum size of the required subset of candidates. Then on the second line printkspace-separated integersa1, 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 

深搜更新节点。。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int maxn=320000;

int Adj[maxn],Size,n,repair[maxn];
bool vis[maxn],rep[maxn];

typedef struct node
{
    int to,next,rp;
}Edge;

Edge E[maxn];

void init()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v,int r)
{
    E[Size].to=v; E[Size].rp=r;
    E[Size].next=Adj[u];
    Adj[u]=Size++;
}

void dfs(int x)
{
    vis[x]=true;
    for(int j=Adj[x];~j;j=E[j].next)
    {
        int TO=E[j].to;
        if(vis[TO]) continue;
        if(E[j].rp==1)///不要修路
        {
            repair[TO]=repair[x];
            dfs(TO);
        }
        else if(E[j].rp==2)///要修路
        {
            rep[TO]=true;
            rep[repair[x]]=false;
            repair[TO]=TO;
            dfs(TO);
        }
    }
}

int main()
{
    cin>>n;
    init();
    for(int i=0;i<n-1;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        Add_Edge(a,b,c);
        Add_Edge(b,a,c);
    }
    dfs(1);
    int ans=0;
    for(int i=1;i<=n;i++) if(rep[i]) ans++;
    cout<<ans<<endl;
    for(int i=1;i<=n;i++) if(rep[i]) cout<<i<<" ";
 return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值