CodeForces #549 Div.2 C Queen
水题,dfs
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define MAX 100000
struct Node
{
int value;
int c;
int next;
}edge[MAX+5];
int pos;
int head[MAX+5];
int a[MAX+5];
int aa=0;
int n;
void Init()
{
for(int i=1;i<=n;i++)
head[i]=-1;
pos=0;
}
void add(int x,int y,int c)
{
edge[pos].value =y;
edge[pos].c = c;
edge[pos].next = head[x];
head[x]=pos++;
}
void dfs(int root,int c)
{
int x=head[root];
int t=0;
while(x!=-1)
{
if(!(c==1&&edge[x].c==1))
{
t=1;
}
dfs(edge[x].value,edge[x].c);
x=edge[x].next;
}
if(t==0&&c==1)
a[aa++]=root;
}
int main()
{
scanf("%d",&n);
Init();
int p,c;
int root=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&p,&c);
if(p==-1)
{
root=i;
continue;
}
add(p,i,c);
}
dfs(root,0);
if(aa==0)
printf("-1\n");
else
{
sort(a,a+aa);
for(int i=0;i<aa;i++)
{
if(i!=aa-1)
printf("%d ",a[i]);
else
printf("%d\n",a[i]);
}
}
return 0;
}