URAL 1242 Werewolf(DFS)
2016-09-15 18:39
507 查看
Werewolf
Time limit: 1.0 secondMemory 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 lines, each of which contains two numbers separated 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 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 line. After this word there is the list of the
werewolf's victims, one number in each line.
Output
Theoutput 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 |
---|---|
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 |
【分析】简单的递归。
#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>
#define inf 0x3f3f3f3f
#define mod 10000typedef long long ll;
using namespace std;
const int N=1005;
const int M=100005;
int n,m,k=0,a,b;
int vis
;
char str[20];
bool flag=false;
vector<int>vec,parent
,chil
;
void dfs(int u,int f)
{//printf("!!!%d %d\n",u,f);
vis[u]=1;
if(!f){
for(int i=0;i<parent[u].size();i++){
//printf("@@@%d\n",parent[u][i]);
if(!vis[parent[u][i]])dfs(parent[u][i],f);
}
}
else {
for(int i=0;i<chil[u].size();i++){
if(!vis[chil[u][i]])dfs(chil[u][i],f);
}
}
}
void in(char *ch){
int fff=0,aa=0,bb=0;
char A[20],B[20];
for(int i=0;i<strlen(ch);i++){
if(ch[i]==' '){fff=1;continue;}
if(!fff)A[aa++]=ch[i];
else B[bb++]=ch[i];
}
A[aa]=0;B[bb]=0;
a=atoi(A);
b=atoi(B);
return;
}
int main() {
scanf("%d",&n);getchar();
while(gets(str)!=NULL){k++;
if(str[0]=='B'){flag=true;continue;}
in(str);
if(!flag)parent[a].push_back(b),chil[b].push_back(a);
if(flag)vec.push_back(a);//if(k>=9)break;
}
for(int i=0;i<vec.size();i++){
int v=vec[i];
dfs(v,0);
dfs(v,1);
}
flag=false;
for(int i=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)
- ural 1242. Werewolf
- ural 1242. Werewolf
- 1242. Werewolf(dfs)
- ural 1005 dp dfs or 0 1背包
- URAL 1208 Legendary Teams Contest(DFS)
- HDU 1242 -- Rescue (bfs,dfs)
- ural1156. Two Rounds(二分染色+dfs)
- HDU 1242 dfs+剪枝或BFS+优先队列 求最短路
- ural Bus Routes(dfs深搜)
- HDU 1242 Rescue (BFS or DFS)
- URAL - 1056(DFS)
- DFS水题 URAL 1152 False Mirrors
- URAL 1009 K-based Numbers (DFS)
- URAL 1005 Stone Pile (DFS)
- hdu 1242 Rescue(方法一:BFS+优先队列,方法二:DFS)
- HDU 1242 Rescue(dfs深搜)
- ural1009 第一个动态规划(dp)题目 。。。似乎也可以用dfs+记忆化收索 解。。。
- hdoj1242(dfs 剪枝 解法)
- URAL 1329 Galactic History (DFS)