algorithm_2024/P4017/P4017.cpp

71 lines
1.6 KiB
C++
Raw Normal View History

2024-10-03 02:44:17 +00:00
#include <iostream>
2024-10-03 03:09:28 +00:00
#include <ranges>
#include <queue>
2024-10-03 04:44:29 +00:00
#include <stdio.h>
2024-10-03 02:44:17 +00:00
2024-10-03 05:19:21 +00:00
using std::cout,std::iostream;
2024-10-03 03:09:28 +00:00
static const constexpr auto range {std::ranges::views::iota};
2024-10-03 04:44:29 +00:00
static const constexpr int MOD {80112002}, MAX_TYPES {(int)5e3+5} /*, MAX_REL_SHIPS {(int)5e5+5}*/;
static int types, rel_ships, a, b, in_degree[MAX_TYPES], out_degree[MAX_TYPES], link_nums[MAX_TYPES], ans;
static std::queue<int> q;
static std::vector<int> next[MAX_TYPES];
2024-10-03 02:44:17 +00:00
2024-10-03 04:44:29 +00:00
class ReadInt{
char c;
int w,n;
public:
ReadInt &operator>>(int &num){
c=0,w=1,n=0;
while(!isdigit(c)){
if(c=='-')w=-1;
c = (char)getchar();
}
while(isdigit(c)){
n=n*10+(c-'0');
c = (char)getchar();
}
num = n*w;
return *this;
}
};
static ReadInt readint;
#define cin readint
2024-10-03 02:44:17 +00:00
2024-10-03 04:44:29 +00:00
int main(){
2024-10-03 03:09:28 +00:00
cin>>types>>rel_ships;
2024-10-03 04:44:29 +00:00
for([[maybe_unused]] const int _:range(1,rel_ships+1)){
2024-10-03 03:09:28 +00:00
cin>>a>>b;
next[a].push_back(b);
in_degree[b]++;
out_degree[a]++;
}
2024-10-03 04:44:29 +00:00
for(const int i:range(1, types+1)){
2024-10-03 03:09:28 +00:00
if(in_degree[i]==0){
q.push(i);
2024-10-03 04:44:29 +00:00
link_nums[i] = 1;
2024-10-03 03:09:28 +00:00
}
}
2024-10-03 04:44:29 +00:00
while(!q.empty()){
2024-10-03 03:09:28 +00:00
auto front {q.front()};
q.pop();
for(auto i:next[front]){
2024-10-03 04:44:29 +00:00
link_nums[i] = (link_nums[i] + link_nums[front])%MOD;
2024-10-03 03:09:28 +00:00
if(--in_degree[i]==0){
q.push(i);
}
}
}
2024-10-03 04:44:29 +00:00
for(auto i:range(1,types+1)){
if(out_degree[i]==0){
ans = (ans + link_nums[i]) % MOD;
}
}
cout<<ans<<"\n";
2024-10-03 02:44:17 +00:00
}