poj2491

简单题

View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

#define maxn 400
#define maxl 100

char st[maxn][maxl];
int n, m;
int from[maxn];
int to[maxn];

int getid(char *a)
{
for (int i = 0; i < m; i++)
if (strcmp(st[i], a) == 0)
return i;
strcpy(st[m++], a);
return m - 1;
}

void input()
{
char a[maxl], b[maxl];
scanf("%d", &n);
n--;
m = 0;
memset(from, -1, sizeof(from));
memset(to, -1, sizeof(to));
for (int i = 0; i < n; i++)
{
scanf("%s%s", a, b);
int x = getid(a);
int y = getid(b);
from[y] = x;
to[x] = y;
}
}

void work()
{
int start;
for (int i = 0; i < m; i++)
if (from[i] == -1)
{
start = i;
break;
}
while (start != -1)
{
printf("%s\n", st[start]);
start = to[start];
}
}

int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++)
{
printf("Scenario #%d:\n", i + 1);
input();
work();
putchar('\n');
}
return 0;
}

你可能感兴趣的:(poj)