• cf C. Valera and Elections


    http://codeforces.com/contest/369/problem/C

    先见边,然后dfs,在回溯的过程中,如果在这个点之后有多条有问题的边,就不选这个点,如果没有而且连接这个点的边还是有问题的边,这个点就是所求的点。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 200000
     5 using namespace std;
     6 
     7 int head[maxn],e,cnt,n;
     8 int ans[maxn];
     9 bool vis[maxn];
    10 int point[maxn];
    11 struct node
    12 {
    13     int u,v,w;
    14     int next;
    15 } p[maxn];
    16 
    17 void add(int u,int v,int w)
    18 {
    19     p[e].u=u;
    20     p[e].v=v;
    21     p[e].w=w;
    22     p[e].next=head[u];
    23     head[u]=e++;
    24 }
    25 
    26 void cls()
    27 {
    28     memset(head,-1,sizeof(head));
    29     memset(vis,false,sizeof(vis));
    30     memset(ans,0,sizeof(ans));
    31     e=0;
    32     cnt=0;
    33 }
    34 
    35 int dfs(int u,int flag)
    36 {
    37     vis[u]=1;
    38     for(int i=head[u]; i!=-1; i=p[i].next)
    39     {
    40         int v=p[i].v,w=p[i].w;
    41         if(!vis[v])
    42         {
    43             ans[u]+=dfs(v,w);
    44         }
    45     }
    46     if(flag==2||ans[u]>=1)
    47     {
    48         if(ans[u]==0)
    49             point[cnt++]=u;
    50         return 1;
    51     }
    52     return 0;
    53 }
    54 
    55 int main()
    56 {
    57     while(scanf("%d",&n)!=EOF)
    58     {
    59         cls();
    60         for(int i=1; i<=n-1; i++)
    61         {
    62            int a,b,c;
    63            scanf("%d%d%d",&a,&b,&c);
    64            add(a,b,c);
    65            add(b,a,c);
    66         }
    67         dfs(1,0);
    68         printf("%d
    ",cnt);
    69         for(int i=0; i<cnt; i++)
    70         {
    71            if(i==0) printf("%d",point[i]);
    72            else printf(" %d",point[i]);
    73         }
    74         printf("
    ");
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    日期时间检查(格式为:YYYY-MM-DD HH:MM:SS)
    日期合法性验证(格式为:YYYY-MM-DD或YYYY/MM/DD )
    [转]Attribute在.net编程中的应用
    WCF
    [转]使用C#开发ActiveX控件
    在 IIS 中承载 WCF 服务
    [转]一个完整的Installshield安装程序实例
    反射
    特性
    迭代器
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3967237.html
Copyright © 2020-2023  润新知