小字辈(左子右兄)
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
struct tree{
int father;
int child;
int bro;
int beifeng;
} a[100001];
void gettans(int x,int i)
{
a[x].beifeng = i;
if (a[x].child != 0)
{
gettans(a[x].child, i + 1);
}
if (a[x].bro != 0)
{
if (a[a[x].bro].bro == x)
a[a[x].bro].bro = 0;
gettans(a[x].bro, i);
}
}
int main()
{
int x, y;
int i, j, n;
int id;
int flag, flag1;
int m, l, r;
while (scanf("%d", &n) != EOF)
{
for(x = 1; x <= n; x++)
{
a[x].child = a[x].father = a[x].bro = 0;
a[x].beifeng = 0;
}
for (x = 1; x <= n; x++)
{
scanf("%d", &id);
scanf("%d%d", &l,&r);
if (l != 0)
{
a[id].child = l;
a[a[id].child].father = id;
}
if (r != 0)
{
a[id].bro = r;
if (a[r].bro == 0)
a[a[id].bro].bro = id;
}
}
for (x = 1; x <= n; x++)
{
if (a[x].father == 0 && a[x].bro == 0)
{
a[x].beifeng = 1;
break;
}
}
gettans(x,1);
flag = flag1 = 1;
for (x = 1; x <= n; x++)
{
if (a[x].beifeng > flag)
flag = a[x].beifeng;
}
printf("%d\n", flag);
for (x = 1; x <= n; x++)
{
if (flag == a[x].beifeng && flag1 == 1)
{
printf("%d", x);
flag1 = 2;
}
else if (flag == a[x].beifeng && flag1 != 1)
{
printf(" %d", x);
}
}
printf("\n");
}
}