溢出+re求助
查看原帖
溢出+re求助
1139280
Eryi117楼主2024/5/25 15:05

为啥我用链星和拓扑排序数据会溢出,把N改小之后也会超时

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
#define ll long long
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define mm(a, b) memset(a, b, sizeof(a))
#define mp make_pair
#define MOD 80112002

const int N = 1e6 + 10;
int n, m;
int a, b;
int dp[N];
int h[N], e[N], ne[N], idx;

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int main() {
    ios;
    mm(h, -1);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> a >> b;
        add(a, b);
    }

    int in[N] = { 0 };
    for (int i = 1; i <= n; i++)
        for (int j = h[i]; ~j; j = ne[j])
            in[e[j]]++;

    queue<int> q;
    for (int i = 1; i <= n; i++) {
        if (in[i] == 0) {
            q.push(i);
            dp[i] = 1;
        }
    }

    while (!q.empty()) {
        int t = q.front();
        q.pop();
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            dp[j] = (dp[j] + dp[t]) % MOD;
            in[j]--;
            if (in[j] == 0) q.push(j);
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ans = (ans + dp[i]) % MOD;
    }
    cout << ans;
    return 0;
}

2024/5/25 15:05
加载中...