Classic Topological Sorting Template

  Topological sorting is a typical algorithm in graph theory. The hierarchical structure of the graph can be sorted through topological sorting. Graph theory tasks such as the completion of a period of time are typical applications. The second application is to determine whether there is a loop problem in the graph.
  The way to build a graph in the program is in the form of adjacency list, the code is as follows:

vector<vector<int> > graph(n, vector<int>{
    
    });

  The following is a template for topological sorting:

	vector<vector<int> > graph(n, vector<int>{
    
    });
	
	根据所给有向图的指向关系,完成graph图的建立
	
	// 统计初始入度为0的
	vector<int> indegree(n, 0);
	for (int i=0; i<n; i++) {
    
    
		for (int j=0; j<graph[i].size(); j++) {
    
    
			indegree[graph[i][j]]++;
		}
	} 
	queue<int> que; // 队列存储,其实如果对于顺序没有要求栈也行
	for (int i=0; i<n; i++) {
    
    
		if (indegree[i] == 0)
			que.push(i);
	} 
	while (!que.empty()) {
    
    
		int a = que.front();
		cout << a + 1 << " ";
		que.pop();
		for (int i=0; i<graph[a].size(); i++) {
    
    
			indegree[graph[a][i]]--;
			if (indegree[graph[a][i]] == 0)
				que.push(graph[a][i]);
		}
	}
/*
	如果要判断是否存在环路,可以在最后判断一下indegree数组,如果全为0说明无环,否则说明有环。
*/

  Let's practice with two questions! (The topic was found by someone else’s blog, not from the website. I didn’t go to OJ to verify the AC, but just passed the sample test. The output format is not adjusted, just barely pass it!)

Topic 1: Determine the ranking of the competition

code show as below:

#include<bits/stdc++.h>
using namespace std;

int main(void) {
    
    
	int n, m;
	cin >> n >> m;
	vector<vector<int>> graph(n, vector<int>{
    
    });
	for (int i=0; i<m; i++) {
    
    
		int u, v;
		cin >> u >> v;
		graph[u-1].push_back(v-1);
	}
	vector<int> indegree(n, 0);
	for (int i=0; i<graph.size(); i++) {
    
    
		for (int j=0; j<graph[i].size(); j++) {
    
    
			indegree[graph[i][j]]++;
		}
	}
	priority_queue<int> que; // 题目对顺序有要求,这里用了优先队列
	for (int i=0; i<n; i++) {
    
    
		if (indegree[i] == 0)
			que.push(-i);
	}
	while (!que.empty()) {
    
    
		int num = -que.top();
		cout << num + 1 << " ";
		que.pop();
		for (int i=0; i<graph[num].size(); i++) {
    
    
			indegree[graph[num][i]]--;
			if (indegree[graph[num][i]] == 0)
				que.push(-graph[num][i]);
		}
	}
	return 0;
}

Topic 2: POJ 2367: Genealogical tree

#include<bits/stdc++.h>
using namespace std;

int main(void) {
    
    
	int n;
	cin >> n;
	vector<vector<int> > graph(n, vector<int>{
    
    });
	int i;
	for (int j=0; j<n; j++) {
    
    
		while (1) {
    
    
			cin >> i;
			if (i != 0) 
				graph[j].push_back(i-1);
			else
				break;
		}
	}
	vector<int> indegree(n, 0);
	for (int i=0; i<n; i++) {
    
    
		for (int j=0; j<graph[i].size(); j++) {
    
    
			indegree[graph[i][j]]++;
		}
	} 
	queue<int> que;
	for (int i=0; i<n; i++) {
    
    
		if (indegree[i] == 0)
			que.push(i);
	} 
	while (!que.empty()) {
    
    
		int a = que.front();
		cout << a + 1 << " ";
		que.pop();
		for (int i=0; i<graph[a].size(); i++) {
    
    
			indegree[graph[a][i]]--;
			if (indegree[graph[a][i]] == 0)
				que.push(graph[a][i]);
		}
	}
	return 0;
}

Reference materials: https://blog.csdn.net/wang_123_zy/article/details/81411683

Guess you like

Origin blog.csdn.net/gls_nuaa/article/details/115102635
Recommended