解决拓扑排序分;
找到入度为0的点----->>删除与此点有关的边----->>>输出该点---------->> 重复操作----->>>>判断点的个数 = ? 总点数 yes : no
#include <stdio.h>
#include<string.h>
#include <queue>
using namespace std;
int m,n;
int into[100];
int map[27][27];
void topusort()
{
queue <int> Q;
queue <char> S;
while(!S.empty()) S.pop();
int count = 0,cn=0,flog = 0;
for(int i=1;i<=m;i++)
{
if(!into[i]) // 首先寻找一个入度为0的点
{
Q.push(i);
cn++;
}
}
if(cn>1) flog = 1;
while(!Q.empty())
{
cn = 0;
int k = Q.front();
Q.pop();
count++;
S.push(k+'A'-1);
for(int i=1;i<=m;i++)
{
if(map[k][i])
{
into[i]--; //此处为去边
if(!into[i])
{
cn++;
Q.push(i);
}
}
if(cn>1) flog = 1 ;
}
}
if(count<m||flog)
printf("No Answer\n");
else
{
while(!S.empty())
{
printf("%c",S.front());
S.pop();
}
printf("\n");
}
}
int main()
{
int N;
char A,B;
scanf("%d",&N);
while(N--)
{
memset(into,0,sizeof(into));
memset(map,0,sizeof(map));
scanf("%d%d",&m,&n);
getchar();
for(int i=1;i<=n;i++)
{
scanf("%c %c",&A,&B);getchar();
int a = A-'A'+1;
int b = B-'A'+1;
map[a][b] = 1; // 转化成图
into[b]++; //统计入度
}
topusort();
}
}