https://vjudge.net/problem/UVA-627#
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
using namespace std;
string buf;
const int N=300+10;
int vis[N];
vector<int>adj[N];
int main(){
int n,m;
while(~scanf("%d",&n)){
getchar();
for(int i=1;i<=n;i++){
getline(cin,buf);
int len=buf.size();
int k=0;
while(buf[k]!='-')k++;
for(int j=k+1;j<len;j++){
int s=j;
int num=0;
while(isdigit(buf[j]))j++;
for(int x=s;x<j;x++)
num=num*10+buf[x]-'0';
adj[i].push_back(num);
}
}
scanf("%d",&m);
puts("-----");
while(m--){
memset(vis,-1,sizeof(vis));
int st,en;
scanf("%d%d",&st,&en);
vis[st]=0;
queue<int> q;
q.push(st);
int x,y;
while(!q.empty()){
x=q.front();
q.pop();
for(int i=0;i<adj[x].size();i++){
y=adj[x][i];
if(vis[y]==-1){
vis[y]=x;q.push(y);
}
}
}
if(vis[en]==-1)puts("connection impossible");
else{
x=en;
stack<int>s;
s.push(x);
while(x!=st){
s.push(vis[x]);
x=vis[x];
}
while(s.size()>1){
printf("%d ",s.top());
s.pop();
}
printf("%d\n",s.top());
}
}
for(int i=0;i<=n;i++)
adj[i].clear();
}
}