uva 10305

原题

经典的拓扑排序, 

每次找入度为0的点, 输出, 并且把与这个点相连的点的入度-1

依次循环就行了

因为我这里是每输出完一次就对原数组排序, 

所以注意要把输出完毕的点入度设为一个较高的值

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>

using namespace std;
const int MAXN = 100+10;
const int BASE = 10;
typedef long long LL;
/*
uva 10305
*/

struct task{
	int num;
	int indegree;
} tasks[MAXN];
bool operator<(const task & t1, const task & t2){
	return t1.indegree < t2.indegree;
}

int Edge[MAXN][MAXN];
int m,n;

int main(){
	while( (cin >> n >> m) && m+n>0 ){
		int a, b, first = 0;
		memset(tasks,0,sizeof(tasks)); 
		memset(Edge,0,sizeof(Edge));
		for(int i=0; i<n; i++) tasks[i].num = i+1;
		for(int i=0; i<m; i++){
			cin >> a >> b;
			Edge[a][b] = 1;
			tasks[b-1].indegree++;
		}
		for(int i=0; i<n; i++){
			sort(tasks, tasks+n);
			cout << tasks[0].num << " ";
			for(int j=1; j<n-i; j++){
				if( Edge[tasks[0].num][tasks[j].num] ){
					tasks[j].indegree--;
				}
			}
			tasks[0].indegree = MAXN+1;
		}
		cout << endl;
	}
	
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值