当前位置: 首页 > news >正文

图论·拓扑排序

拓扑排序

核心操作

#include<bits/stdc++.h>
#define MAX_VALUE 10009
using ll = long long;
using namespace std;
int n, m, s, t;
vector<list<int>>graph(100006,list<int>());
vector<int>indegrees(100006, 0);
vector<int>res;
void solve() {
	cin >> n >> m;
	while (m--) {
		cin >> s >> t;
		graph[s].push_back(t);
		indegrees[t]++;
	}
	queue<int>q;
	for (int i = 0; i <= n-1; i++) {
		if (!indegrees[i]) {
			q.push(i);
		}
	}
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		res.push_back(cur);

		for (auto item : graph[cur]) {
			if (--indegrees[item]==0) {
				q.push(item);
			}

		}
	}
	if (res.size() == n) {
		for (int i = 0; i < n - 1; i++) cout << res[i] << " ";
		cout << res[n - 1];
	}
	else cout << -1 << endl;

}
signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	solve();
}









以下为例题

P4017 最大食物链计数

题目背景

你知道食物链吗?Delia 生物考试的时候,数食物链条数的题目全都错了,因为她总是重复数了几条或漏掉了几条。于是她来就来求助你,然而你也不会啊!写一个程序来帮帮她吧。

题目描述

给你一个食物网,你要求出这个食物网中最大食物链的数量。

(这里的“最大食物链”,指的是生物学意义上的食物链,即最左端是不会捕食其他生物的生产者,最右端是不会被其他生物捕食的消费者。)

Delia 非常急,所以你只有 1 1 1 秒的时间。

由于这个结果可能过大,你只需要输出总数模上 80112002 80112002 80112002 的结果。

输入格式

第一行,两个正整数 n 、 m n、m nm,表示生物种类 n n n 和吃与被吃的关系数 m m m

接下来 m m m 行,每行两个正整数,表示被吃的生物A和吃A的生物B。

输出格式

一行一个整数,为最大食物链数量模上 80112002 80112002 80112002 的结果。

输入输出样例 #1

输入 #1

5 7
1 2
1 3
2 3
3 5
2 5
4 5
3 4

输出 #1

5

说明/提示

各测试点满足以下约定:

【补充说明】

数据中不会出现环

题解1:拓扑排序

  • 时间复杂度: O ( n + e ) O(n+e) O(n+e)

  • DFS理论上也可以使用,但是只能过前两个用例。

#include<bits/stdc++.h>
#define MAX_VALUE 10000009
#define mod 80112002
using ll = long long;
using namespace std;
int n, m,a,b,ans=0;
vector<int>indegrees(5009, 0);
vector<list<int>>graph(5009, list<int>());
vector<int>producers;
vector<int>res(5009, 0);
void solve() {
	cin >> n >> m;
	while (m--) {
		cin >> a >> b;
		graph[a].push_back(b);
		indegrees[b]++;
	}
	for (int i = 1; i <= n; i++) {
		if (!indegrees[i]) {
			producers.push_back(i);
			res[i] = 1;//1条食物链
		}
	}
	queue<int>q;
	for (auto producer : producers) {
		q.push(producer);
	}

	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		//cout << "cur:" << cur << endl;
		if (!graph[cur].size()) {
			ans = (ans + res[cur]) % mod;
		}
		for (auto item : graph[cur]) {
			if (!--indegrees[item]) {
				q.push(item);
			}
			res[item] = (res[item] + res[cur])% mod;
			//cout << "item:" << item << " res[item]:" << res[item] << endl;

		}
		//for (int i = 1; i <= n; i++) {
		//	cout << res[i] << " ";
		//}
		//cout << endl;
	}
	cout << ans;
}
signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	solve();
}

题解2:DFS

  • 对入度为0的进行DFS,探索所有路径,就是所有生物链

#include<bits/stdc++.h>
#define MAX_VALUE 10000009
#define mod 80112002
using ll = long long;
using namespace std;
int n, m,a,b,ans=0;
vector<int>indegrees(5009, 0);
vector<list<int>>graph(5009, list<int>());
vector<int>producers;
int visited[5009];
void dfs(int start,int * visited) {
	//cout << "current node:" << start << endl;
	if (!graph[start].size()) {
		ans=(ans+1)% mod;
		//cout << endl;
		return;
	}
	for (auto item : graph[start]) {
		if (!visited[item]) {
			visited[item] = 1;
			dfs(item,visited);
			visited[item] = 0;
		}
	}
}
void solve() {
	cin >> n >> m;
	while (m--) {
		cin >> a >> b;
		graph[a].push_back(b);
		indegrees[b]++;
	}
	for (int i = 1; i <= n; i++) {
		if (!indegrees[i]) {
			producers.push_back(i);
		}
	}
	for (auto producer : producers) {
		if (!graph[producer].size()) {// no consumer
			continue;
		}
		memset(visited, 0, sizeof(visited));
		visited[producer] = 1;
		dfs(producer, visited);
	}
	cout << ans;
}
signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	solve();
}

(过不了)
在这里插入图片描述

相关文章:

  • Arthas解决CPU飙高和定位死锁问题,JProfiler解决OOM问题
  • 支付宝小程序评论提升策略:打造高互动度的用户体验
  • TONGYI Lingma(通义灵码),GitHub Copilot和Cursor 对比
  • 树莓集团落子海南,如何重构数字产业生态体系​
  • 第27周JavaSpringboot电商进阶开发 3.常见问题解答
  • C语言实现队列数据结构:思路与代码详解
  • 谈谈List,Set,Map的区别
  • 瞭解安全防火牆術語(適合剛接觸Firepower的使用者)
  • 【蓝桥杯—单片机】第十五届省赛真题代码题解析 | 思路整理
  • vue-next-admin修改配置指南
  • WireShark自动抓包
  • 数据库第二次作业
  • 【Go语言圣经1.5】
  • ctfhub-web-SSRF通过攻略
  • 【lf中的git实战】
  • DeepSeek + Midjourney(MJ):创意设计 具体步骤
  • scrcpy pc机远程 无线 控制android app 查看调试log
  • 常见的Content-Type值
  • InternVL:论文阅读 -- 多模态大模型(视觉语言模型)
  • linux在 Ubuntu 系统中设置服务器时间
  • 北京发布今年第四轮拟供商品住宅用地清单,共计5宗22公顷
  • 幸福航空取消“五一”前航班,财务人员透露“没钱飞了”
  • 铁路上海站五一假期预计发送446万人次,同比增长8.4%
  • 梅花画与咏梅诗
  • 美国政府将暂时恢复部分受影响留学生的合法身份,并将制订新标准
  • 规范涉企案件审判执行工作,最高法今天发布通知