拓扑排序及其模板

拓扑排序是一种针对于有向无环图中顶点的排序算法,以入度为基准(一个顶点被多少条边指向的边的数值即为入度),从小到大一一挑选并排列成线性结果。

模板:(利用优先队列默认输出字典序最小结果)

#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include<queue>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#pragma warning(disable:4996)
#define lldin(a) scanf("%lld", &a)
#define println(a) printf("%lld\n", a)
#define print(a) printf("%lld ", a)
#define reset(a, b) memset(a, b, sizeof(a))
#define debug cout<<"procedures above are available"<<endl;
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 33;
inline ll nextLong()
{
	ll tmp = 0, si = 1;
	char c;
	c = getchar();
	while (c > '9' || c < '0')
	{
		if (c == '-')
			si = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		tmp = tmp * 10 + c - '0';
		c = getchar();
	}
	return si * tmp;
}
/**Maintain your determination.Nobody knows the magnificent landscape
at his destination before the arrival with stumble.**/
/**Last Remote**/
struct node
{
	int next, to;
}nodes[150000];
int heads[110000],indegrees[110000];
vector<ll>ans;
int cnt = 0;
void construction(int from, int to)
{
	nodes[++cnt] = node{ heads[from],to };
	heads[from] = cnt;
}
void topological(int limit)
{
	priority_queue<int, vector<int>, greater<int>>q;//小根堆
	for (int i = 1; i <= limit; i++)
		if (indegrees[i] == 0)//把入度为0的点塞入队列
			q.push(i);
	while (!q.empty())
	{
		int current = q.top();
		q.pop();
		ans.push_back(current);
		for (int i = heads[current]; i != -1; i = nodes[i].next)
		{
			ll to = nodes[i].to;
			indegrees[to]--;//减少相关点入度
			if (indegrees[to] == 0)
				q.push(to);
		}
	}
}
int DETERMINATION()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	ll n, m;
	while (cin>>n>>m)
	{
		cnt = 0;
		reset(heads, -1);
		ans.clear();
		reset(indegrees, 0);
		for (int i = 1; i <= m; i++)
		{
			ll tmp1, tmp2;
			cin >> tmp1 >> tmp2;
			construction(tmp1, tmp2);
			indegrees[tmp2]++;
		}
		topological(n);
		for (int i = 0; i < ans.size(); i++)
		{
			if (i < ans.size() - 1)
				cout << ans[i] << " ";
			else
				cout << ans[i] << endl;
		}
	}
	return 0;
}

 

posted @ 2019-08-21 00:35  完全墨染的樱花  阅读(146)  评论(0编辑  收藏  举报