当前位置:网站首页>大雪菜味题解--P4017 最大食物链计数
大雪菜味题解--P4017 最大食物链计数
2022-07-16 22:07:00 【LiSymbol】
标签:图论、拓扑排序
思路:首先我们要理解题目,我因为看错题浪费了我好几个小时,我本来以为求的是这个食物链网里面那一条食物链最长,长度是多少,然后我就直接按照–进阶指南-可达性统计(链接)这题的思路来做了,直接浪费了很长时间。这道题的意思是求这个食物链网中有多少条食物链,一条完整的食物链的开头入度为0,结尾肯定是出度为0,这样就认为是一条食物链,对了,同时注意!!!,这里边的数量有500000条所以这里不能用邻接表来存,要用邻接矩阵来存。基本步骤是:
- 建立邻接矩阵存储图、边,记录下来每一个点的出度和入度
- 求这张有向无环图的拓扑序列
- 依次遍历拓扑序列的每一个点并累加点的食物链数
- 枚举出度为0的点,并ans+=此点的食物链数
这里具体讲一下为什么要用拓扑排序(思维过程):(摘自洛谷题解区@_Watcher )
1、这是一道图论题;
2、不是求最短路;
3、根据提示“最左端是不会捕食其他生物的生产者”可以想到,我们要入度为零的点开始查找;
4、再看一遍题目,就是求路径数,当且仅当一个点的入度变为零时才需要入队,并不是数据更新一次就要入队;
5、出度为零的点的路径总数和就是答案。
思路已经呼之欲出了:拓扑排序
拓扑排序的精髓就在于每个点只会入队一次,每条边只会通过一次,所以时间复杂度就有很好的保证
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
const int N=5010,mod=80112002;
vector<int> g[N]; //用邻接矩阵来存图
int d[N],p[N],num[N]; //d表示入度,p表示出度,num[i]记录到这个点的食物链的数量
int n,m;
LL ans=0;
void topsort(){
queue<int> q;
for(int i=1;i<=n;i++){
if(d[i]==0){
// cout<<"入度为0:"<<i<<endl;
num[i]=1;
q.push(i);
}
}
int k=0;
while(!q.empty()){
auto t=q.front();
q.pop();
for(auto j:g[t]){
d[j]--;
num[j]=(num[j]+num[t])%mod; //更新j点的食物链数
if(d[j]==0){
q.push(j);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
g[a].push_back(b);
d[b]++;
p[a]++;
}
topsort();
for(int i=1;i<=n;i++){
if(p[i]==0){
ans=(ans+num[i])%mod;
}
}
cout<<ans;
return 0;
}
@本人不太会表达,算法也只是初窥门径,如果有什么看不懂,写的不好的地方,欢迎在评论区留言,我会吸取教训,慢慢完善自己,谢谢!
边栏推荐
- Envoy生命周期管理
- 每日一题:最近请求次数(剑指offer042)
- Account creation + login + contact form code
- 此主机支持Intel VT-x ,但Intel VT-x处于禁用状态
- Pytest interface automation testing framework | pytest combines secondary packaging to realize interface automation
- Glide 源码分析(4.13.2)
- [STL] simulation implementation vector
- 从零复现PyTorch版(2)
- go语言实现登录注册收藏相关工具和教程链接
- 10.10:VectorDraw C# VS VectorDraw WEB/Crack-VectorDraw
猜你喜欢

VS使用WebDeploy发布网站

Install Apple CMS and its resource collection and page code from 0

Redis分布式缓存-Redis主从

canvas无数个三角形动画js特效

响应式表单样式透明设计

Compose uses coil to load network pictures

用cmd命令进行磁盘清理(主要是系统盘)

nodeJS编译环境下使用yarn工具的安装与使用方法

Canadian deployer Canadian adapter image construction, deployment

AtCoder Beginner Contest 259 D Circumferences
随机推荐
Programming examples of stm32f1 and stm32cubeide-w25q-spi-flash and FatFs migration
Une seule ligne de texte dépasse l'ellipse partielle, plusieurs lignes de texte dépassent l'ellipse partielle, spécifiez plusieurs lignes
In depth explanation of MySQL index
MySQL安装常见报错怎么处理
星巴克不使用两阶段提交
Applet: the picker view selector scrolls quickly. When confirming, the "value displays an error."“
liunx中FileDescriptor与打开文件之间的关系
STM32F1与STM32CubeIDE编程实例-W25Q-SPI-Flash与SPIFFS移植
微信小程序_15,纯数据字段
每日一题:回文链表(剑指off027)
Install Apple CMS and its resource collection and page code from 0
Wechat applet_ 16. Component life cycle
Compose gradient
Clean the disk with CMD command (mainly the system disk)
乐观锁和悲观锁在kubernetes中的应用
MySQL storage model
Eat chicken, throw away some supplies, and the Jedi survive
Pytest interface automation test framework | @pytest Fixture () decorator
绝地求生 吃鸡 98k 不自动关镜子
【百度飞桨】手写数字识别模型部署Paddle Inference