这道题很简单,就不细说了
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1285
代码:
#include<stdio.h>
#define max_n 505
int in_deg[max_n];
bool live[max_n];
int si[max_n], NO;
struct e{
int to, next;
}edge[max_n * max_n];
int head[max_n], index;
void add_e(int u, int v){
index++;
edge[index].to = v;
edge[index].next = head[u];
head[u] = index;
}
void init(int N){
NO = 0;
index=0;
int i;
for(i=0; i<=N; i++){
live[i] = true;
head[i] = 0;
in_deg[i] = 0;
}
}
void topology(int N){//拓扑排序
int i, j;
while(NO != N){
for(i=1; i<=N; i++){
if(in_deg[i] == 0 && live[i]){
si[++NO] = i;
live[i] = false;
int u = i;
for(int e = head[u]; e; e=edge[e].next){
int v = edge[e].to;
in_deg[v]--;
}
break;
}
}
}
}
int main(){
int N, M;
while(~scanf("%d %d",&N, &M)){
init(N);
int i, u, v;
for(i=1; i<=M; i++){
scanf("%d %d",&u, &v);
add_e(u, v);
in_deg[v]++;
}
topology(N);
for(i=1; i<=N; i++){
printf("%d",si[i]);
if(i != N)
printf(" ");
}
printf("\n");
}
return 0;
}