1242. Werewolf(dfs)

时间:2023-12-22 11:57:32

1242

简单dfs 往孩子方向搜一遍 父母方向搜一遍 输入还搞什么字符串。。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
vector<int>fa[];
vector<int>ch[];
int u,v,q[];
int w[],vis1[],vis2[],o;
void dfs1(int u)
{
int i;
for(i = ; i < (int)fa[u].size() ; i++)
{
int v = fa[u][i];
if(!vis1[v])
{
vis1[v] = ;
dfs1(v);
}
}
}
void dfs2(int u)
{
int i;
for(i = ; i < (int)ch[u].size() ; i++)
{
int v = ch[u][i];
if(!vis2[v])
{
vis2[v] = ;
dfs2(v);
}
}
}
int main()
{
int i,j,n;
char s1[],s2[];
scanf("%d",&n);
while(cin>>s1)
{
if(strcmp(s1,"BLOOD")==)
break;
cin>>s2;
u = atoi(s1);
v = atoi(s2);
fa[u].push_back(v);
ch[v].push_back(u);
}
int g = ;
while(scanf("%d",&j)!=EOF)
{
g++;
q[g] = j;
}
for(i = ; i <= g ; i++)
{
if(!vis1[q[i]])
{
vis1[q[i]] = ;
dfs1(q[i]);
}
}
for(i = ; i <= g ; i++)
{
if(!vis2[q[i]])
{
vis2[q[i]] = ;
dfs2(q[i]);
}
}
for(i = ; i <= n ;i++)
if(!vis2[i]&&!vis1[i])
{
o++;
w[o] = i;
}
if(!o)
printf("0\n");
else
{
for(i = ; i < o ; i++)
printf("%d ",w[i]);
printf("%d\n",w[o]);
}
return ;
}