首先,什么是拓扑排序
只有有向无环图(Directed Acyclic Graph,简称DAG )才有拓扑排序。
DAG必至少有一个入度为零的点和一个出度为零的点。
在拓扑排序中,对于任意一个有向边的起点和终点,在排序后起点总是在终点前。
————————————————
HDUProblem - 1285拓扑排序
思路:
将数据存为有向图,每个顶点的入度即为被打败的次数。首先,第一名没有被打败过,入度为0,弹出,并且剩下的所有被他打败的人的排名都提升了一位,再找出当前情况下的第二个“第一”重复此操作,采用拓扑排序实现
//#include<bits/std c++.h>
#include <iostream>
#include<ctime>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#define int ll
#pragma warning (disable:4996);
using namespace std;
const int N = 1e3+10;
int T;
int gra[N][N];//有向无权图邻接矩阵
int deg[N];//每个顶点的入度
int ans[N];//记录排名
int n, m;
void topo() {
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= n; i++) {
if (deg[i] == 0) {
ans[j]=i;
deg[i]--;
for (int k = 1; k <= n; k++) {
if (gra[i][k] == 1) {
gra[i][k] = 0;//弹出前一个排名后删边拓扑
deg[k]--;//入度减一
}
}
break;
}
}
}
}
signed main()
{ ios::sync_with_stdio(false); cout.tie(NULL);
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
while (cin >> n >> m) {
memset(gra, 0, sizeof(gra));
memset(deg, 0, sizeof(deg));
memset(ans, 0, sizeof(ans));
for (int j = 1; j <= m; j++) {
int a, b;
cin >> a >> b;
if (gra[a][b] == 0)
{
gra[a][b] = 1;//a win b
deg[b]++;
}
}
topo();
for (int j = 1; j <= n; j++) {
cout << ans[j];
if (j != n)cout << " ";
}
cout << endl;
}
return 0;
}
稀疏图改邻接表存储(vector实现)
//#include<bits/std c++.h>
#include <iostream>
#include<ctime>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#define int ll
#pragma warning (disable:4996);
using namespace std;
const int N = 1e3+10;
int T;
vector<int> gra[N];//有向无权图邻接矩阵
int deg[N];//每个顶点的入度
int ans[N];//记录排名
int n, m;
void topo() {
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= n; i++) {
if (deg[i] == 0) {
ans[j]=i;
deg[i]--;
for (int x : gra[i]) {//邻接表每个顶点对应的元素就是链接点
deg[x]--;
}
break;
}
}
}
}
signed main()
{ ios::sync_with_stdio(false); cout.tie(NULL);
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
while (cin >> n >> m) {
memset(gra, 0, sizeof(gra));
memset(deg, 0, sizeof(deg));
memset(ans, 0, sizeof(ans));
for (int j = 1; j <= m; j++) {
int a, b;
cin >> a >> b;
gra[a].push_back(b);//改用邻接表存储
deg[b]++;
}
topo();
for (int j = 1; j <= n; j++) {
cout << ans[j];
if (j != n)cout << " ";
}
cout << endl;
}
return 0;
}
HDUProblem - 4324拓扑排序(队列模板)
思路:题意简单概括就是判断一个图有没有环,只需要拓扑排序,然后记录拓扑的节点数,如果等于总节点数,也就是全部的节点都被pop了就没有环,反之则存在环
//#include<bits/std c++.h>
#include <iostream>
#include<ctime>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define ll long long
#define int ll
#pragma warning (disable:4996);
const int N = 4e3+10;
int T;
int n;
vector<int>gra[N];//邻接表
int deg[N];//入度记录
int topo() {
queue<int>q;
for (int j = 0; j < n; j++) {
if (deg[j] == 0)
q.push(j);
}
int sum = 0;//记录拓扑出去的顶点个数与总顶点比较
while (!q.empty()) {
int now = q.front();
q.pop();
sum++;
for (int x : gra[now]) {//删边,对应入度减一
deg[x]--;
if (deg[x] == 0)
q.push(x);
}
}
return sum == n;
}
signed main()
{ ios::sync_with_stdio(false); cout.tie(NULL);
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // !ONLINE_JUDGE
cin >> T;
int cnt = 0;
while (T--) {
cin >> n;
memset(deg, 0, sizeof(deg));
for (int j = 0; j < n; j++) {
gra[j].clear();
char s[N];
scanf("%s", &s);
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
gra[j].push_back(i);
deg[i]++;
}
}
}
if(!topo())
cout << "Case #" << ++cnt << ":" << " Yes" ;
else
cout << "Case #" << ++cnt << ":" << " No" ;
cout << endl;
}
return 0;
}
这里有个细节就是为什么使用了队列而上一题没有。上一题的话确定了拓扑的次数,也就是排名,并且确定每次只拓扑一个顶点,找下一个排名的时候就会重新拓扑,所以产生的状态都会被处理,而这题我们要一次拓扑完,处理当前节点的时候没办法回去处理产生的子状态,所以选择用队列存储图的状态,然后一起处理