#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
#define SIZE 100010
/*自定义比较函数,这里实现的是最小优先级队列*/
struct less_cmp
{
bool operator()(const int &a, const int &b)
{
return a > b;
}
};
vector<int> Vec[SIZE];
/* 初始化方式 */
priority_queue < int, vector<int>, less_cmp > Que;
int n, k, degree[SIZE];
void clear()
{
memset(degree, 0, sizeof(degree));
}
int main(int argc, char *argv[])
{
int i, beg, end;
while (1)
{
scanf("%d %d", &n, &k);
if (0 == n && 0 == k)
break;
for (i=0 ; i<k ; i++)
{
scanf("%d %d", &beg, &end);
Vec[beg].push_back(end);
degree[end]++;
}
for (i=1 ; i<=n ; i++)
{
if (0 == degree[i])
Que.push(i);
}
int node;
vector<int>::iterator iter_beg;
vector<int>::iterator iter_end;
printf("ORDER:");
while (!Que.empty())
{
node = Que.top();
Que.pop();
printf(" %d", node);
iter_end = Vec[node].end();
for (iter_beg = Vec[node].begin() ; iter_beg != iter_end ; iter_beg++)
{
degree[*iter_beg]--;
if (0 == degree[*iter_beg])
Que.push(*iter_beg);
}
Vec[node].clear();
}
printf("\n");
clear();
}
}
|