I did a Topollogical Sort as follows and got WA:

Code: Select all

```
#include <stdio.h>
#include <map>
#include <string.h>
using namespace std;
char list[101][60];
int n,m;
int mat[101][101];
int pos(char a[]){
int i;
for(i=0;i<n;i++){
if(strcmp(a,list[i])==0)
return i;
}
}
int num[101];
int ts(char a[]){
int i;
int p=pos(a);
if(num[p]!=-1)
return num[p];
int max=0;
for(i=0;i<n;i++){
int t=0;
char tt[100];
if(mat[i][p]==1){
strcpy(tt,list[i]);
t=ts(tt);
}
if(t>max)
max=t;
}
if(max+1>num[p])
num[p]=max+1;
return num[p];
}
int main(){
int c=0;
while(scanf("%d", &n)!=EOF){
c++;
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
mat[i][j]=0;
for(i=0;i<n;i++){
scanf("%s", list[i]);
num[i]=-1;
}
scanf("%d", &m);
for(i=0;i<m;i++){
char t1[102],t2[102];
scanf("%s%s", t1,t2);
mat[pos(t1)][pos(t2)]=1;
}
for(i=0;i<n;i++)
if(num[i]==-1){
char ini[60];
strcpy(ini,list[i]);
ts(ini);
}
printf("Case #%d: Dilbert should drink beverages in this order:",c);
while(1){
int min=20000000;
int ii;
for(i=0;i<n;i++){
if(num[i]<min){
min=num[i]; ii=i;
}
}
if(min==20000000)
break;
num[ii]=20000000;
printf(" %s",list[ii]);
}
printf(".\n\n");
}
return 0;
}
```

Surelly, i'm understanding bad the problem. Can you help me?

(my 3rd sample output is: apple-juice wine vodka beer martini rum whiskey cachaca gin ).