Tarjan+SPFA(Topsort)求助

P2272 [ZJOI2007] 最大半连通子图

黑影洞人 @ 2022-04-07 17:27:16

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define N 1919810
#define int long long
using namespace std;
int n,m,p,tot,head[N],to[N],nxt[N],st[N],top,co[N],col,dfn[N],low[N],cnt;
int in[N],sz[N],dis[N],ans,tim[N],anss,ax[N],ay[N],aid[N];
void add(int u,int v){
    to[++tot]=v;
    nxt[tot]=head[u];
    head[u]=tot;
}
bool cmp(int aa,int b){
    if(ax[aa]!=ax[b])return ax[aa]<ax[b];
    return ay[aa]<ay[b];
}
void tarjan(int x){
    dfn[x]=low[x]=++cnt;
    st[++top]=x;
    for(int i=head[x];i;i=nxt[i]){
        int y=to[i];
        if(!dfn[y])tarjan(y),low[x]=min(low[x],low[y]);
        else if(!co[y])low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x]){
        co[x]=++col;
        sz[col]=1;
        while(st[top]!=x)co[st[top--]]=col,sz[col]++;
        top--;
    }
}
void rebuild(){
    for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
    for(int i=1;i<=m;i++)aid[i]=i,ax[i]=co[ax[i]],ay[i]=co[ay[i]];
    sort(aid+1,aid+m+1,cmp);
    memset(head,0,sizeof(head));
    memset(to,0,sizeof(to));
    memset(nxt,0,sizeof(nxt));
    tot=0;
    for(int i=1;i<=m;i++){
        int z=aid[i];
        if((ax[z]!=ay[z])&&(ax[z]!=ax[aid[i-1]]||ay[z]!=ay[aid[i-1]]))
        add(ax[z],ay[z]);
        in[ay[z]]++;
    }
}
void spfa(){
    queue<int >q;
    for(int i=1;i<=col;i++){
        if(!in[i]){
            q.push(i),dis[i]=sz[i],tim[i]=1;
            if(dis[ans]<dis[i])ans=i;
        }
    }
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=head[x];i;i=nxt[i]){
            int y=to[i];
            in[y]--;
            if(dis[y]<dis[x]+sz[y]){
                dis[y]=dis[x]+sz[y];
                tim[x]=0;
                if(dis[ans]<dis[y])ans=y;
            }
            if(dis[y]==dis[x]+sz[y])tim[y]=(tim[x]+tim[y])%p;
            if(!in[y])q.push(y);
        }
    }
}
signed main(){
    scanf("%lld%lld%lld",&n,&m,&p);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%lld%lld",&u,&v);
        add(u,v);
        ax[i]=u,ay[i]=v;
    } 
    rebuild();
    spfa();
    for(int i=1;i<=n;i++)if(dis[i]==dis[ans])anss=(anss+tim[i])%p;
    printf("%lld\n%lld",dis[ans],anss);
    return 0;
}

|