详见:http://blog.sina.com.cn/s/blog_51cea4040100hf8v.html
n个发生器就是n个点,边是每个发生器到它产生的数字,单向边。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=1005;
struct edge{
int u,v,next;
}G[N*N];
int head[N],inum;
int vst[N*N];
inline void add(int u,int v,int p){
G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}
int n,m;
int path[N*N],pnt;
#define V G[p].v
int totm;
inline void dfs1(int u){
for (int p=head[u];p;p=G[p].next)
if (!vst[p]){
vst[p]=1; totm++;
dfs1(V);
path[++pnt]=V;
return;
}
}
inline void Print(){
for (int i=2;i<=pnt;i++)
printf("%d ",path[i]);
printf("0\n");
}
int tag[N];
int S[N],g;
int st;
inline bool dfs2(int u){
tag[u]=1;
for (int p=head[u];p;p=G[p].next)
if (V<n && !tag[V]){
S[++*S]=p;
if (!dfs2(V))
S[(*S)--]=0;
else
return 1;
}
else if (V==st){
g=p;
return 1;
}
return 0;
}
int main(){
int tem,a;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n);
for (int i=1;i<=n;i++){
read(tem);
for (int j=1;j<=tem;j++) read(a),add(i,a,++inum);
}
if (n==1){
printf("NIE\n");
return 0;
}
dfs1(1); path[++pnt]=1; reverse(path+1,path+pnt+1);
if (path[pnt]!=n || totm<inum){
Print(); return 0;
}else{
int flag=0;
for (st=1;st<n;st++){
memset(tag,0,sizeof(tag));
if (flag|=dfs2(st))
break;
}
if (!flag){
printf("NIE\n"); return 0;
}
memset(vst,0,sizeof(vst));
for (int i=1;i<=*S;i++) vst[S[i]]=1; vst[g]=1;
pnt=0; dfs1(1); path[++pnt]=1; reverse(path+1,path+pnt+1);
Print();
}
return 0;
}