#include <iostream>
#include <vector>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
const int LEN = 100;
vector< int > graph[LEN];
bool ckd[LEN]; //check for visited
int pai[LEN]; //parent of node i
int cnt[LEN]; //the next son to visit
void dfs(int i)
{
int nxt = i;
while(1)
{
bool find = false;
if(!ckd[nxt])
{
cout<<"visit "<<nxt<<endl;
ckd[nxt]=true;
}
while(cnt[nxt]<graph[nxt].size())
{
if(!ckd[graph[nxt][cnt[nxt]]])
{
int temnxt = graph[nxt][cnt[nxt]];
pai[temnxt] = nxt;
cnt[nxt]++;
nxt=temnxt;
find = true;
break;
}
cnt[nxt]++;
}
if(!find)
{
cout<<"node "<<nxt<<" can't find nxt and now return to pai ";
nxt = pai[nxt];
cout<<nxt<<endl;
if(nxt==-1)
break;
assert(ckd[nxt]);
}
}
}
int main()
{
freopen("in.txt","r",stdin);
memset(pai,-1,sizeof(pai));
memset(ckd,0,sizeof(ckd));
memset(cnt,0,sizeof(cnt));
int n,s;
cin>>n>>s;
for(int i=0;i<n;++i)
{
int node;
while(cin>>node)
{
if(node==-1)break;
graph[i].push_back(node);
}
}
dfs(s);
return 0;
}
|