题目链接:https://www.luogu.com.cn/problem/P4017
题意:给定n,m(n<=5000,m<=500000),然后m行每行两个整数a,b(b吃a),最后要求输出总的最长食物链(最左边为最高消费者,最右边为生产者)条数取mod
题解:拓扑排序不难想,但是这里面坑还是不少的(反正我被坑了)。。。
dp[i]为%mod之后的值,所以不能单纯的求mod的最大值。事实上只需要将所有的out[i]即出度为0的点的dp值加上取模就好了(唉,难道这就是我无脑取最大值太多的报应??)。
代码:
#include <bits/stdc++.h>
#define ll int
#define pi acos(-1)
#define pb push_back
#define mst(a, i) memset(a, i, sizeof(a))
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define dbg(x) cout << #x << "===" << x << endl
using namespace std;
template <class T> void read(T &x) {
T res = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
res = (res << 3) + (res << 1) + c - '0';
c = getchar();
}
x = res * f;
}
void print(ll x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
const ll maxn = 5e5 + 10;
const ll mod = 80112002;
ll n,m,a,b,du[maxn],dp[maxn],out[maxn];
vector<ll> g[maxn];
ll bfs(){
queue<ll> q;
for(ll i=1;i<=n;i++){
if(du[i]==0) q.push(i),dp[i]=1;
}
ll mx=0;
while(!q.empty()){
ll now=q.front();q.pop();
if(out[now]==0) mx=(mx+dp[now])%mod;
//dbg(now);
for(auto i:g[now]){
dp[i]=(dp[i]+dp[now])%mod;
du[i]--;
if(du[i]==0) q.push(i);
}
}
return mx;
}
// ll gcd(ll a,ll b){return (b==0)?a:gcd(b,a%b);}
// ll qpow(ll a,ll p,ll mod){ll
// ans=1;a=a%mod;while(p){if(p&1)ans=(ans*a)%mod;p>>=1;a=(a*a)%mod;}return ans;}
int main() {
ll _s = 1;
// read(_s);
for (ll _ = 1; _ <= _s; _++) {
read(n),read(m);
for(ll i=1;i<=m;i++){
read(a),read(b);
g[b].pb(a);
du[a]++;
out[b]++;
}
//for(ll i=1;i<=n;i++) cout<<du[i]<<" ";cout<<endl;
ll ans=bfs();
//for(ll i=1;i<=n;i++) cout<<dp[i]<<' ';cout<<endl;
cout<<ans<<endl;
}
return 0;
}
/*
input:::
?z至少简单检查一下(简单到,看一看是不是自己想要提交的代码,不要自己调试的没注释,或者随便造的一个样例都过不了)。
output:::
*/