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 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

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
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, 2003
Difficulty: 232
 
题意:有一个村庄,里面居住着n个村民, 其中有一个狼人n<=1000。现在有一些人死了。狼人不会杀自己祖先和后代。问有可能是狼人的编号。
分析:显然是很多棵树。有一个人被杀,那么从他以上的祖先和从他以下的后代都没有可能是狼人。
整个过程用递归做。
注意如果有一个点已经被标记,那么下次就不用递归找了,因为过程是一样的。

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name)
{
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} inline bool Getint(int &Ret)
{
Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == 'B') Flag = ;
if(Flag == && Ch == 'D') break;
Ch = getchar();
}
if(Flag) return ;
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return ;
} const int N = ;
int n;
vector<int> Parent[N], Child[N], Ans;
bool Visit[N]; inline void Input()
{
scanf("%d", &n);
int x, y;
while(Getint(x))
{
Getint(y);
Parent[x].pub(y), Child[y].pub(x);
}
} inline void GoUp(int x)
{
Visit[x] = ;
int Length = sz(Parent[x]);
Rep(i, Length) GoUp(Parent[x][i]);
Parent[x].clear();
} inline void GoDown(int x)
{
Visit[x] = ;
int Length = sz(Child[x]);
Rep(i, Length) GoDown(Child[x][i]);
Child[x].clear();
} inline void Solve()
{
int x;
while(~scanf("%d", &x))
{
GoUp(x);
GoDown(x);
} For(i, , n)
if(!Visit[i]) Ans.pub(i);
if(sz(Ans))
{
Rep(i, sz(Ans)-) printf("%d ", Ans[i]);
printf("%d\n", Ans.back());
}
else puts("");
} int main()
{
#ifndef ONLINE_JUDGE
SetIO("A");
#endif
Input();
Solve();
return ;
}
 

ural 1242. Werewolf的更多相关文章

  1. URAL 1242 Werewolf(DFS)

    Werewolf Time limit: 1.0 secondMemory limit: 64 MB Knife. Moonlit night. Rotten stump with a short b ...

  2. 1242. Werewolf(dfs)

    1242 简单dfs 往孩子方向搜一遍 父母方向搜一遍 输入还搞什么字符串.. #include <iostream> #include<cstdio> #include< ...

  3. WereWolf项目 Postmortem

    WereWolf项目 Postmortem (博客园的MarkDown编辑器好像有些问题,编号都显示1..) 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描 ...

  4. Werewolf流程分析

    werewolf大致流程 首先是房主创建房间,创建成功以后房主开启web socket连接. 其他成员加入房间,加入房间后新成员和老成员的游戏玩家列表都会更新,然后新成员也要开启web socket连 ...

  5. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  6. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  7. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  8. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  9. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

随机推荐

  1. Dynamic Morphing Square(动态变形矩阵)

    题目描述: 解题思路: 先对输入的N进行判断,是否不小于3,如果小于3,需要继续输入一个新的数,知道输入的N比3大. 第一个打印的矩阵,*号为最外面一圈,其余全为-. 第二个打印的矩阵,*号向内缩减了 ...

  2. 台大《机器学习基石》课程感受和总结---Part 2 (转)

    转自:http://blog.sina.com.cn/s/blog_641289eb0101e2ld.html Part 2总结一下一个粗略的建模过程: 首先,弄清楚问题是什么,能不能用机器学习的思路 ...

  3. HDOJ 1878 欧拉回路 nyoj 42一笔画问题

    #include<cstdio> #include<cstring> ]; int find(int x) { if(visited[x]!=x) return find(vi ...

  4. python 中接口的实现

    实际上,由于python是动态语言,支持多继承,因此接口在语言层面,是没有的东东. 然后,在架构设计上,我们又需要这么一个东西,来规范开发人员的行为. 因此就有了zope.interface的诞生. ...

  5. Git 怎样保证fork出来的project和原project(上游项目)同步更新

    1.  在 Fork 的代码库中添加上游代码库的 remote 源,该操作只需操作一次即可. 如: 其中# upstream 表示上游代码库名, 可以任意. git remote add upstre ...

  6. Docker跨主机通信之路由

    一.实验环境: 主机名 主机IP Docker0_IP Docker1 192.168.88.130 172.17.0.1 Docker2 192.168.88.131 172.18.0.1 二.实验 ...

  7. document.createElement

    document.createElement()的用法 document.createElement()是在对象中创建一个对象,要与appendChild() 或 insertBefore()方法联合 ...

  8. 编译qt

    进入开始菜单Microsoft Visual Studio 2010,Visual Studio Tools,Visual Studio Command Prompt (2010),需要注意的是,这里 ...

  9. javascript ASCII和Hex互转

    <script> var symbols = " !\"#$%&'()*+,-./0123456789:;<=>?@"; var loAZ ...

  10. Myeclipse 安装svn插件

    安装subclipse,  SVN插件1.从官网下载site-1.8.22.zip文件  访问不了可点我网盘2.从中解压出features与 plugins文件夹,复制到MyEclipse\MyEcl ...