Werewolftime limit:1.0 Second
Memory limit:64 Mbknife. Moonlit night. Rotten stump with a short black-handled knife in it. Those who know would understand. Disaster in the village. Werewolf.there is no so many residents in the village. Many of them is each of the other ' s relatives. The Werewolf. The werewolf is merciless, but he descendants never become his victims. The werewolf can drown the village in blood, but he never kills his ancestors. It is known on all the villagers, 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 is a hard task, if a very special condition would isn't hold. Namely, citizens of the village is not used to leave it. If some ancestor of some citizen lives in the village and then also he immediate ancestor does. It means, that, for example, if the father of the mother of some citizen still lives in the village, than also he mother Still lives. Inputthe first line contains an inteGer
N, 1 <
N≤1000, which is the number of the villagers. The villagers is assigned numbers from 1 to
N. Further is the description of the relation ' child-parent ': A sequence of lines, each of which contains the numbers Separat Ed with a space; The first number in each line 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 be no more than-parents, and there are no cycles. The list is followed by the word "BLOOD" written with capital letters in a separate line. After this word there are the list of the werewolf ' s victims, one number in each line. Outputthe output should contain the numbers of the residents. The numbers must is in the ascending order and separated with a space. If There is no suspects, the output should contain the only number 0.Samples
input |
Output |
Bayi 1blood38 |
4 5 7 |
The 4BLOOD25 |
0 |
problem Author:Leonid Volkov "Analysis" simple recursion.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineINF 0x3f3f3f3f#defineMoD 10000typedefLong Longll;using namespacestd;Const intn=1005;Const intm=100005;intn,m,k=0, A, b;intVis[n];Charstr[ -];BOOLflag=false; Vector<int>Vec,parent[n],chil[n];voidDfsintUintf) {//printf ("!!! %d%d\n ", u,f);vis[u]=1; if(!f) { for(intI=0; I<parent[u].size (); i++){ //printf ("@@@%d\n", Parent[u][i]); if(!Vis[parent[u][i]]) DFS (PARENT[U][I],F); } } Else { for(intI=0; I<chil[u].size (); i++){ if(!Vis[chil[u][i]]) DFS (CHIL[U][I],F); } }}void inch(Char*ch) { intfff=0, aa=0. B =0; Chara[ -],b[ -]; for(intI=0; I<strlen (ch); i++){ if(ch[i]==' ') {fff=1;Continue;} if(!FFF) a[aa++]=Ch[i]; Elseb[bb++]=Ch[i]; } A[aa]=0; b[bb]=0; A=atoi (A); b=atoi (B); return;}intMain () {scanf ("%d",&n); GetChar (); while(Gets (str)!=null) {k++; if(str[0]=='B') {flag=true;Continue;} inch(str); if(!flag) Parent[a].push_back (b), Chil[b].push_back (a); if(flag) Vec.push_back (a);//if (k>=9) break; } for(intI=0; I<vec.size (); i++){ intv=Vec[i]; DFS (V,0); DFS (V,1); } Flag=false; for(intI=1; i<=n;i++)if(!vis[i]) printf ("%d", i), flag=true; if(!flag) printf ("0"); printf ("\ n"); return 0;}
View Code
URAL 1242 Werewolf (DFS)