拓扑排序 : hdu 1285 示例

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <utility>
#include <sstream>
#include <climits>
#include <cassert>
#define BUG puts("here!!!");

using namespace std;

const int N = 505;
int edge[N][N];
int degree[N];
int vis[N];
int n, m;
int main() {
	while(cin >> n >> m) {
		memset(edge, 0, sizeof(edge));
		memset(degree, 0, sizeof(degree));
		int a, b;
		while(m--) {
			cin >> a >> b;
			if(edge[a][b]) continue;
			edge[a][b] = 1;
			degree[b]++;
		}
		int k = 0;
		memset(vis, 0, sizeof(vis));
		int temp;
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				if(degree[j] == 0 && !vis[j]) {
					cout << j;
					k++;
					vis[j] = 1;
					temp = j;
					break;
				}
			}
			for(int j = 1; j <= n; j++) {
				if(edge[temp][j] == 1) {
					degree[j]--;
				}
			}
			if(k != n) cout << ' ';
			else {
				cout << endl;
				break;
			}
		}
	}
	return 0;
}			

posted @ 2013-01-18 12:51  小尼人00  阅读(133)  评论(0编辑  收藏  举报