76分求助

P2272 [ZJOI2007] 最大半连通子图

FJ_OIer @ 2023-07-16 15:22:34

WA on 5#&7#,重边也过滤了……

#include <bits/stdc++.h>
#define MAXN 100001
using namespace std;
int n,m,x,u,v,tot,top,cnt,ans;
int a[MAXN];
int dfn[MAXN],low[MAXN],sta[MAXN];
int val[MAXN],in[MAXN],id[MAXN],vis[MAXN];
int dp[MAXN],t[MAXN];
bool ins[MAXN];
vector<int> e[MAXN],E[MAXN];
queue<int> q;
void tarjan(int u){
    dfn[u]=low[u]=++tot;
    sta[top++]=u;
    ins[u]=1;
    for (int i=0;i<e[u].size();i++){
        int v=e[u][i];
        if (!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }else if (ins[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if (dfn[u]==low[u]){
        cnt++;
        int x=0;
        while (u!=x){
            x=sta[--top];
            ins[x]=0;
            id[x]=cnt;
            val[cnt]++;
        }
    }
}
void topo(){
    for (int i=1;i<=cnt;i++){
        if (!in[i]){
            dp[i]=val[i];
            t[i]=1;
            q.push(i);
        }
    }
    while (!q.empty()){
        int u=q.front();
        q.pop();
        for (int i=0;i<E[u].size();i++){
            int v=E[u][i];
            in[v]--;
            if (dp[u]+val[v]>dp[v]){
                dp[v]=dp[u]+val[v];
                t[v]=t[u];
            }else if (dp[u]+val[v]==dp[v]){
                t[v]=(t[v]+t[u])%x;
            }
            if (!in[v]){
                q.push(v);
            }
        }
    }
}
int main(){
    cin>>n>>m>>x;
    while (m--){
        cin>>u>>v;
        e[u].push_back(v);
    }
    for (int i=1;i<=n;i++){
        if (!dfn[i]){
            tarjan(i);
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=0;j<e[i].size();j++){
            int v=e[i][j];
            if (id[i]!=id[v]&&vis[id[v]]!=id[i]){
                in[id[v]]++;
                vis[id[v]]=id[i];
                E[id[i]].push_back(id[v]);
            }
        }
    }
    topo();
    for (int i=1;i<=cnt;i++){
        ans=max(ans,dp[i]);
    }
    tot=0;
    for (int i=1;i<=cnt;i++){
        if (dp[i]==ans){
            tot=(tot%x+t[i]%x)%x;
        }
    }
    cout<<ans<<endl<<tot;
    return 0;
}

|