HDU1285(拓扑排序+优先队列)

 坑点:重边情况下入度不++,因为即使比赛排名重复出现对节点入度也是没有影响的

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int MAX_N = 505;
vector<int> ans;
priority_queue<int, vector<int>, greater<int> > q;
int indegree[MAX_N];
int n, m;
int map[MAX_N][MAX_N];
void InitTop()
{
	for (int i = 1; i <= n; i++)
		if (indegree[i] == 0)
			q.push(i);
}
void TopSort()
{
	int temp;
	int c = 1;
	while(!q.empty())
	{

		temp = q.top();
		q.pop();
		if (c != n)
			printf("%d ", temp), c++;
		else
			printf("%d\n", temp);
		for (int i = 1; i <= n; i++)
		{
			if (map[temp][i])
			{
				indegree[i]--;
				if(indegree[i] == 0)
					q.push(i);
			}
		}
	}
}
int main()
{
	while(~scanf("%d %d", &n, &m))
	{
		memset(map, 0, sizeof(map));
		memset(indegree, 0, sizeof(indegree));
		while(m--)
		{
			int x, y;
			scanf("%d %d", &x, &y);
			if (map[x][y])
				continue;
			map[x][y] = 1;
			indegree[y]++; //重边情况下入度不++
		}
		InitTop();
		TopSort();
	}
	return 0;
}

    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/zzycsx/article/details/50330875
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞