#include<stdio.h>
#include<string.h>
int m,n;
int map[20][20],degree[20];
char s[20],list[20];
bool visit[20];
void dfs(int depth)
{
int i,j;
if(depth>=m)
{
list[depth]=0;
printf("%s/n",list);
return;
}
for(i=1;i<=m;i++)
if(degree[i]==0&&!visit[i])
{
visit[i]=true;
for(j=1;j<=map[i][0];j++)
degree[map[i][j]]--;
list[depth]=i+'A'-1;
dfs(depth+1);
for(j=1;j<=map[i][0];j++)
degree[map[i][j]]++;
visit[i]=false;
}
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(map,0,sizeof(map));
memset(visit,false,sizeof(visit));
memset(degree,0,sizeof(degree));
for(i=0;i<n;i++)
{
scanf("%s",s);
map[s[0]-'A'+1][0]++;
map[s[0]-'A'+1][map[s[0]-'A'+1][0]]=s[2]-'A'+1;
}
for(i=1;i<=m;i++)
for(j=1;j<=m;j++)
if(map[i][j]!=0)
degree[map[i][j]]++;
dfs(0);
printf("/n");
}
return 0;
}
再探拓扑排序JOJ 2237 Hero Ranklist
最新推荐文章于 2024-08-16 22:54:40 发布