问题 B: 确定比赛名次 codeup拓扑排序问题

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 510;
int inDegree[maxn] = { 0 }; //记录每个节点的入度
int n; //节点个数
vector<int> Adj[maxn]; //邻接表
bool topologicalSort() {
	priority_queue<int, vector<int>, greater<int> > q;
	for (int i = 1; i <= n; i++) {
		if (inDegree[i] == 0)
			q.push(i); //把入度为0的节点压入
	}
	int num = 0; //记录已经位于拓扑排序序列中的节点个数
	while (!q.empty()) {
		int t = q.top();
		//输出结果
		printf("%d", t);
		if (num < n)
			printf(" ");
		else
			printf("\n");
		q.pop();
		num++;
		for (int i = 0; i < Adj[t].size(); i++) {
			int tmp = Adj[t][i];
			inDegree[tmp]--;
			if (inDegree[tmp] == 0) {
				q.push(tmp);
			}
		}
	}
	if (num == n) {
		return true;
	}
	return false;
}
int main() {
	int m;
	while (scanf("%d %d", &n, &m) , n!=0||m!=0) {
		memset(inDegree, 0, sizeof(inDegree));
		for (int i = 0; i < m; i++) {
			int a, b;
			scanf("%d %d", &a, &b);
			inDegree[b]++;
			Adj[a].push_back(b);
		}
		topologicalSort();
		//邻接表清空
		for (int i = 0; i < maxn; i++)
			Adj[i].clear();
	}

}

问题 B: 确定比赛名次 - Codeup新家 (hustoj.com)icon-default.png?t=N7T8http://codeup.hustoj.com/problem.php?cid=100000623&pid=1#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值