原题http://acm.hdu.edu.cn/showproblem.php?pid=1285
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12093 Accepted Submission(s): 4817
4 3 1 2 2 3 4 3
1 2 4 3
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
//#include <map>
#include <set>
using namespace std;
#define N 500 + 10
bool map[N][N];
vector<int>a[N];
int n,m;
int degree[N],res[N];
void Topo(){
int i,j,p;
for(i=1;i<=n;i++){
p = -1;
for(j=1;j<=n;j++){
if(degree[j] == 0){
degree[j]--;
res[i] = j;
p = j;
break;
}
}
for(j=1;j<=n;j++){
if(map[p][j] == true)
{
map[p][j] = false;
degree[j]--;
}
}
}
}
void output(){
int i;
printf("%d",res[1]);
for(i=2;i<=n;i++){
printf(" %d",res[i]);
}
printf("\n");
}
int main(){
int i,x,y;
while(~scanf("%d%d",&n,&m)){
memset(map,false,sizeof(map));
memset(degree,0,sizeof(degree));
memset(res,0,sizeof(res));
for(i=1;i<=n;i++){
a[i].clear();
}
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
if(map[x][y] == false){
map[x][y] = true;
degree[y]++;
}
}
Topo();
output();
}
return 0;
}
原文:http://blog.csdn.net/zcr_7/article/details/38355835