CodeForces - 369C - Valera and Elections

369C - Valera and Elections

思路:dfs,对于搜索到的每个节点,看他后面有没有需要修的路,如果没有,那么这个节点就是答案。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=1e5+5;
vector<int>g[N];
vector<int>edge[N];
vector<int>ans;
int dfs(int o,int u)
{
    int res=0;
    for(int i=0;i<g[u].size();i++)
    {
        if(g[u][i]!=o)
        {
            int temp=dfs(u,g[u][i]);
            if(temp==0&&edge[u][i]==2)
            {
                res++;
                ans.push_back(g[u][i]);
            }
            res+=temp;
        }
    }
    return res;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0,x,y,t;i<n-1;i++)
    {
        cin>>x>>y>>t;
        g[x].pb(y);
        g[y].pb(x);
        edge[x].pb(t);
        edge[y].pb(t);
    }
    dfs(0,1);
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++)
    {
        cout<<ans[i];
        if(i!=ans.size()-1)cout<<‘ ‘;
        else cout<<endl;
    }
    cout<<endl;
    return 0;
}
时间: 2024-08-29 05:02:54

CodeForces - 369C - Valera and Elections的相关文章

Codeforces A. Valera and X 题解

判断二维字符串是否满足下面条件: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the progra

Codeforces 441C Valera and Tubes

题目链接:Codeforces 441C Valera and Tubes 没看到r >= 2一直错.让前几个管子占用2个格子.最后一个把剩下的都占用了.假设问题有解.这样做一定有解.其它策略就不一定了(比方让某个管子占用了3个格子.而总共同拥有四个格子,两个管子). #include <iostream> #include <cstdio> using namespace std; int main() { int n, m, k; scanf("%d%d%d&q

codeforces A. Valera and Plates 题解

Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl.

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

Codeforces 441D Valera and Swaps(置换群)

题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做边,f[i]=i,代表自环.那么根据置换原理,图中有k个环,则需要最少n-k次交换操作得到排列[1,2...n].所以,先找出图中的环,对同一个环的位置进行标记.这样对不在同一个环的两个位置进行交换,会将两个环合并.将在同一个环内的两个位置进行交换,会将这个环分成两个环. 只需要,判断需要加环还是去环.贪

CodeForces 369A Valera and Plates( 水)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int bowl,plate; int n; int dish[1100]; int main() { int i,j; while(scanf("%d%d%d",&n,&bowl,&plate)!=EOF) { int ans

Codeforces 369E Valera and Queries --树状数组+离线操作

题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询的任意一个点的.这时候我们可以建立点集的补集,以线段的形式,如果点集的补集线段包含了某条给出的线段,那么被包含的那条线段肯定不会包括任意一个点,那么该组查询的答案ans--即可. 用树状数组做,离线读入数据,按容易被包含的线段优先排个序,然后扫一遍,边统计边修改即可. 代码: #include <i

暑假集训-个人赛第三场

ID Origin Title 10 / 29 Problem A CodeForces 443B Kolya and Tandem Repeat   1 / 1 Problem B CodeForces 442A Borya and Hanabi 9 / 29 Problem C CodeForces 442B Andrey and Problem 3 / 17 Problem D CodeForces 442C Artem and Array 14 / 18 Problem E CodeFo

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