不太清楚是不是tarjan后去重边出问题了,54pts

P2272 [ZJOI2007] 最大半连通子图

Jack_1218 @ 2024-09-29 21:02:38

RT。哈哈哪位dalao帮帮

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, x;
vector<int> adj[N], adjs[N];
int ind[N], dp[N], num[N];

int scc[N], sz[N], scccnt = 0;
int dfn[N], low[N], tt = 0;
stack<int> st;
bool inst[N];

void tarjan(int u) {
    dfn[u] =  low[u] = ++tt;
    st.push(u); inst[u] = true;
    for (int i = 0; i < adj[u].size(); i++) {
        int v = adj[u][i];
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if (inst[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }

    if (dfn[u] == low[u]) {
        scccnt++;
        while (true) {
            int t = st.top();
            scc[t] = scccnt;
            sz[scccnt]++;
            inst[t] = false;
            st.pop();
            if (u == t) break;
        }
    }
}

int main() {
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
    scanf("%d%d%d", &n, &m, &x);
    for (int i = 1; i <= m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        adj[u].push_back(v);
    }

    for (int i = 1; i <= n; i++) {
        if (!dfn[i]) {
            tarjan(i);
        }
    }

    map<int, bool> mp;
    for (int u = 1; u <= n; u++) {
        mp.clear();
        for (int i = 0; i < adj[u].size(); i++) {
            int v = adj[u][i];
            if (scc[u] != scc[v] && !mp.count(scc[v])) {
                adjs[scc[u]].push_back(scc[v]);
                mp.insert(make_pair(scc[v], true));
                ind[scc[v]]++;
            }
        }
    }

    queue<int> Q;
    for (int i = 1; i <= scccnt; i++) {
        if (!ind[i]) {
            Q.push(i);
            dp[i] = sz[i];
            num[i] = 1 % x;
        }
    }

    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        for (int i = 0; i < adjs[u].size(); i++) {
            int v = adjs[u][i];
            if (dp[u] + sz[v] > dp[v]) {
                dp[v] = dp[u] + sz[v];
                num[v] = num[u];
            } else if (dp[u] + sz[v] == dp[v]) {
                num[v] += num[u];
                num[v] %= x;
            }
            --ind[v];
            if (ind[v] == 0) Q.push(v);
        }
    }

    int K = 0;
    for (int i = 1; i <= scccnt; i++) {
        K = max(K, dp[i]);
    }
    printf("%d\n", K);

    int cnt = 0;
    for (int i = 1; i <= scccnt; i++) {
        if (dp[i] == K) {
            cnt = (cnt + num[i]) % x;
        }
    }
    printf("%d\n", cnt);
    return 0;
}

|