ural 1242. Werewolf
2015-11-14 15:48
316 查看
1242. 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
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 Source: Ural State University Personal Programming Contest, March 1, 2003
Tags: graph theory (hide tags for unsolved problems)
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 = 0; char Ch = ' '; bool Flag = 0; while(!(Ch >= '0' && Ch <= '9')) { if(Ch == 'B') Flag = 1; if(Flag == 1 && Ch == 'D') break; Ch = getchar(); } if(Flag) return 0; while(Ch >= '0' && Ch <= '9') { Ret = Ret * 10 + Ch - '0'; Ch = getchar(); } return 1; } const int N = 1010; int n; vector<int> Parent , Child , Ans; bool Visit ; 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] = 1; int Length = sz(Parent[x]); Rep(i, Length) GoUp(Parent[x][i]); Parent[x].clear(); } inline void GoDown(int x) { Visit[x] = 1; 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, 1, n) if(!Visit[i]) Ans.pub(i); if(sz(Ans)) { Rep(i, sz(Ans)-1) printf("%d ", Ans[i]); printf("%d\n", Ans.back()); } else puts("0"); } int main() { #ifndef ONLINE_JUDGE SetIO("A"); #endif Input(); Solve(); return 0; }
View Code
相关文章推荐
- 推荐大家一个不错android模拟器工具Genymotion
- Linux安装oracle常用命令
- Windows10中SC_CLOSE消息的变化
- c++关于顺序容器指针迭代器失效问题
- Linux spi驱动分析(四)----SPI设备驱动(W25Q32BV)
- POJSeek the Name, Seek the Fame(KMP算法)
- 【软工文档总结之后五个文档总结(重点篇)】
- opencv2矩阵操作
- 手机号码帮助类
- shell入门-shell特性
- MySQL常用操作
- How to unload a kernel module in use
- Linux spi驱动分析(三)----spiddev分析
- Java过滤特殊字符
- .htaccess文件url重写小记
- android studio 添加快捷按钮.
- C语言中enum类型的用法
- 虚幻4 D3D渲染过程的笔记:04
- JAVA打印九九乘法表
- Web移动端Fixed布局的解决方案