POJ 2491
#include<iostream>
#include<stdio.h>
#include<string>
#define MAXN 400
using namespace std;
struct node
{
string s1;
string s2;
};
node a[400];
int main()
{
//freopen("acm.acm","r",stdin);
int test;
int num;
int i;
int j;
int k;
int r;
cin>>test;
for(k = 0; k < test; ++ k)
{
cin>>num;
for(i = 0; i < num-1; ++ i)
{
cin>>a[i].s1;
cin>>a[i].s2;
}
for(i = 0; i < num-1; ++ i)
{
for(j = 0; j < num-1; ++ j)
{
if(a[i].s1 == a[j].s2)
{
break;
}
}
if(j == num-1)
{
cout<<"Scenario #"<<k+1<<":"<<endl;
cout<<a[i].s1<<endl;
cout<<a[i].s2<<endl;
for(j = 0; j < num-2; ++ j)
{
for(r = 0; r < num-1; ++ r)
{
if(a[i].s2 == a[r].s1)
{
cout<<a[r].s2<<endl;
a[i].s2 = a[r].s2;
break;
}
}
}
break;
}
}
cout<<endl;
}
}