zoukankan      html  css  js  c++  java
  • 1242. Werewolf

    1242. Werewolf

    Time limit: 1.0 second Memory limit: 64 MB
     
    Knife. Moonlit night. Rotten stump with a short black-handled knife in it. Those who know will understand. Disaster in the village. Werewolf.
    There are no so many residents in the village. Many of them are each other's relatives. Only this may help to find the werewolf. The werewolf is merciless, but his descendants never become his victims. The werewolf can drown the village in blood, but he never kills his ancestors.
    It is known about all the villagers who is the child of whom. Also, the sad list of the werewolf's victims is known. Your program should help to determine the suspects. It would be a hard task, if a very special condition would not hold. Namely, citizens of the village are not used to leave it. If some ancestor of some citizen lives in the village, then also his immediate ancestor does. It means, that, for example, if the father of the mother of some citizen still lives in the village, than also his mother still lives.

    Input

    The first line contains an integer N, 1 < N ≤ 1000, which is the number of the villagers. The villagers are assigned numbers from 1 to N. Further is the description of the relation "child-parent": a sequence of strings, each of which contains two numbers separated with a space; the first number in each string is the number of a child and the second number is the number of the child's parent. The data is correct: for each of the residents there are no more than two parents, and there are no cycles. The list is followed by the word "BLOOD" written with capital letters in a separate string. After this word there is the list of the werewolf's victims, one number in each string.

    Output

    The output should contain the numbers of the residents who may be the werewolf. The numbers must be in the ascending order and separated with a space. If there are no suspects, the output should contain the only number 0.

    Samples

    inputoutput
    8
    1 3
    3 6
    4 5
    6 2
    4 6
    8 1
    BLOOD
    3
    8
    
    4 5 7
    
    6
    1 2
    3 2
    1 4
    3 4
    2 6
    5 2
    5 4
    BLOOD
    2
    5
    
    0
    
    Problem Author: Leonid Volkov Problem Source: Ural State University Personal Programming Contest, March 1, 20
    ***********************************************************************************************
    链式前向星&&双向深搜(参考了代码,觉得经典)
    ***********************************************************************************************
      1 #include<iostream>
      2 #include<string>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<cctype>
      6 #include<cstdio>
      7 #include<stack>
      8 #include<queue>
      9 #include<vector>
     10 #include<algorithm>
     11 using namespace std;
     12 struct node//链式前向星
     13 {
     14     int to;
     15     int next;
     16 }side1[10050],side2[10050];//定义两个为了向祖先和后裔分别搜
     17 int head1[10060],head2[10060];
     18 int n,a,b,x1,x2,g,i,j;
     19 bool woof[1000];//标记
     20 char  str[1000];
     21 void add1(int x,int y)//插数
     22 {
     23     side1[x1].to=y;
     24     side1[x1].next=head1[x];
     25     head1[x]=x1++;
     26 }
     27 void add2(int x,int y)
     28   {
     29       side2[x2].to=y;
     30       side2[x2].next=head2[x];
     31       head2[x]=x2++;
     32   }
     33 void dfs1(int x)//父子深搜
     34   {
     35       woof[x]=true;
     36       for(int k=head1[x];k!=-1;k=side1[k].next)
     37         dfs1(side1[k].to);
     38   }
     39 void dfs2(int y)
     40  {
     41      woof[y]=true;
     42      for(int k=head2[y];k!=-1;k=side2[k].next)
     43        dfs2(side2[k].to);
     44  }
     45  bool findrelative(int &L1,int &L2,char *str)//对输入进行处理
     46   {
     47       int  i=0;
     48       while(str[i]==' ')
     49        i++;
     50       if(str[i]=='B')
     51         return false;
     52       L1=0;
     53       while(1)
     54       {
     55           if(str[i]>'9'||str[i]<'0')
     56            break;
     57           L1=L1*10+str[i]-'0';
     58            i++;
     59 
     60       }
     61       while(str[i]==' ')
     62        i++;
     63       L2=0;
     64       while(1)
     65        {
     66            if(str[i]>'9'||str[i]<'0')
     67               break;
     68            L2=L2*10+str[i]-'0';
     69            i++;
     70        }
     71        return true;
     72   }
     73   int main()
     74   {
     75       cin>>n;
     76       getchar();
     77       x1=0;x2=0;
     78       memset(head1,-1,sizeof(head1));
     79       memset(head2,-1,sizeof(head2));
     80       while(gets(str))
     81       {
     82           int h1,h2;
     83           if(!findrelative(h1,h2,str))
     84             break;
     85            add1(h1,h2);//为了向两个方向搜索有add1,add2
     86            add2(h2,h1);
     87       }
     88       memset(woof,false,sizeof(woof));
     89       while(scanf("%d",&g)!=EOF)
     90        {
     91            dfs1(g);//两方向深搜
     92            dfs2(g);
     93        }
     94        int ans[100050];
     95        int t=0;
     96       for(int i=1;i<=n;i++)
     97        {
     98            if(woof[i]==false)
     99              ans[++t]=i;
    100        }
    101        if(t==0)//特殊情况的处理
    102         {
    103             cout<<'0'<<endl;
    104             return 0;
    105         }
    106        sort(ans+1,ans+t+1);
    107        for(i=1;i<=t;i++)
    108        cout<<ans[i]<<' ';
    109        cout<<endl;
    110        return 0;
    111 
    112   }
    View Code
  • 相关阅读:
    Linux 常用命令
    去除重叠区间
    Python 小工具之大文件去重
    有趣的pyfiglet
    杨辉三角
    Paginator分页
    Linux用户和用户组
    Grub介绍
    Linux系统运行级别
    Linux系统启动流程
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3243257.html
Copyright © 2011-2022 走看看