博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1242. Werewolf
阅读量:6364 次
发布时间:2019-06-23

本文共 4276 字,大约阅读时间需要 14 分钟。

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

input output
81 33 64 56 24 68 1BLOOD38
4 5 7
61 23 21 43 42 65 25 4BLOOD25
0
Problem Author: Leonid Volkov
Problem Source: Ural State University Personal Programming Contest, March 1, 20
***********************************************************************************************
链式前向星&&双向深搜(参考了代码,觉得经典)
***********************************************************************************************
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
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'<
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3243257.html

你可能感兴趣的文章
go语言中在变量后加上接口是什么意思?
查看>>
day5-iptables
查看>>
版本配置
查看>>
python之进程
查看>>
wpf中嵌入winform控件的坑
查看>>
VMware Workstation and Hyper-V are not compatible. 解决方案
查看>>
POJ-3304Segments[计算几何]
查看>>
杭电2120--Ice_cream's world I(并查集)
查看>>
雅虎前段优化35条
查看>>
(转)接口100
查看>>
UIScrollView 大概是如何实现的,它是如何捕捉、响应手势的?
查看>>
asp.net MVC中实现调取web api
查看>>
keepalived实现服务高可用
查看>>
iOS模型以及使用
查看>>
NSString 去除空格
查看>>
swift - 网络请求数据处理 - 协议处理
查看>>
[BZOJ1588]营业额统计(Splay)
查看>>
[BZOJ 4869][SHOI&SXOI2017]相逢是问候(扩展欧拉定理+线段树)
查看>>
2017-08-13
查看>>
条件语句优化面面观
查看>>