#include <stdio.h>
#define MAX_VILLAGERS 1000
int numOfVillagers;
typedef struct Relative{
int villager;
int next;
}Relative;
Relative relativeArray[2 * MAX_VILLAGERS + 1];
int relativeNum;
int ancestorLastAdded[MAX_VILLAGERS + 1];
int descendantLastAdded[MAX_VILLAGERS + 1];
int isHuman[MAX_VILLAGERS + 1];
void addRelative(int head, int relativeLastAdded[], int relative){
relativeNum++;
relativeArray[relativeNum].villager = relative;
relativeArray[relativeNum].next = relativeLastAdded[head];
relativeLastAdded[head] = relativeNum;
}
void excludeRelatives(int victim, int relativeLastAdded[]){
isHuman[victim] = 1;
int relativeIndex;
for (relativeIndex = relativeLastAdded[victim]; relativeIndex != 0; relativeIndex = relativeArray[relativeIndex].next){
int villager = relativeArray[relativeIndex].villager;
//只会单单往ancestor或者单单往descendant深搜
excludeRelatives(villager, relativeLastAdded);
}
}
int main(){
scanf("%d", &numOfVillagers);
char str[6];
//注意输入处理
while (scanf("%s", str) != EOF && str[0] != 'B'){
int child = 0;
int i;
for (i = 0; str[i] != '\0'; i++)
child = child * 10 + str[i] - '0';
int parent;
scanf("%d", &parent);
addRelative(child, ancestorLastAdded, parent);
addRelative(parent, descendantLastAdded, child);
}
int victim;
while (scanf("%d", &victim) != EOF){
//无需再用的数据甭存储,输入后立马处理
excludeRelatives(victim, ancestorLastAdded);
excludeRelatives(victim, descendantLastAdded);
}
int found = 0;
int villager;
for (villager = 1; villager <= numOfVillagers; villager++)
if (!isHuman[villager]){
printf("%d ", villager);
found = 1;
}
printf("%s", found ? "\n" : "0\n");
return 0;
}
URAL 1242 Werewolf (DFS)
最新推荐文章于 2019-06-30 15:02:43 发布