zoukankan      html  css  js  c++  java
  • CodeForces #549 Div.2 C Queen

    题目

    水题,dfs

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    
    using namespace std;
    
    #define MAX 100000
    
    struct Node
    {
        int value;
        int c;
        int next;
    }edge[MAX+5];
    int pos;
    int head[MAX+5];
    int a[MAX+5];
    int aa=0;
    int n;
    void Init()
    {
        for(int i=1;i<=n;i++)
            head[i]=-1;
        pos=0;
    }
    
    void add(int x,int y,int c)
    {
        edge[pos].value =y;
        edge[pos].c = c;
        edge[pos].next = head[x];
        head[x]=pos++;
    }
    
    void dfs(int root,int c)
    {
        int x=head[root];
        
        int t=0;
        while(x!=-1)
        {
           if(!(c==1&&edge[x].c==1))
           {
               t=1;
           }
            
            dfs(edge[x].value,edge[x].c);
            x=edge[x].next;
        }
        
        if(t==0&&c==1)
            a[aa++]=root;
    }
    
    int main()
    {
        scanf("%d",&n);
        Init();
        int p,c;
        int root=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p,&c);
            if(p==-1)
            {
                root=i;
                continue;
            }
            add(p,i,c);
        }
        
        dfs(root,0);
        
        if(aa==0)
            printf("-1
    ");
        else
        {
            sort(a,a+aa);
            for(int i=0;i<aa;i++)
            {
                if(i!=aa-1)
                    printf("%d ",a[i]);
                else
                    printf("%d
    ",a[i]);
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    UVA 10608 Friends
    UVA 10806 Dijkstra, Dijkstra.
    HDU 3715 Go Deeper
    poj1315
    poj1383
    poj1650
    poj1265
    poj1523
    RedHat9.0虚拟机安装
    注册DirectShow filter时应该注意中文路径
  • 原文地址:https://www.cnblogs.com/dacc123/p/10717719.html