大佬们,帮查错,一道很水的题

P2272 [ZJOI2007] 最大半连通子图

_sry @ 2018-10-07 13:24:42

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
inline long long read()
{
    long long f=1,ans=0;char c;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
    return f*ans;
}
struct node{
    long long u,v,nex;
}x[1000001];
long long n,m,mod;
long long low[100001],top,cnt,head[100001],dfn[100001],st[100001],col,co[100001],si[100001],num;
void add(long long u,long long v)
{
    x[cnt].u=u,x[cnt].v=v,x[cnt].nex=head[u],head[u]=cnt++; 
}
void tarjan(long long u)
{
    low[u]=dfn[u]=++num;
    st[++top]=u;
    for(long long i=head[u];i!=-1;i=x[i].nex)
    {
        long long v=x[i].v;
        if(!dfn[v]){tarjan(v),low[u]=min(low[u],low[v]);}
        else if(!co[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        co[u]=++col;
        si[col]++;
        while(st[top]!=u)
        {
            ++si[col];
            co[st[top]]=col;
            top--;
        }top--;
    }return;
}
long long uu[1000001],vv[1000001];
struct node1{
    long long uu,vv;
}xx[1000001];
bool cmp(node1 x1,node1 x2)
{
    if(x1.uu==x2.uu) return x1.vv<x2.vv;
    return x1.uu<x2.uu;
}
long long dis[1000001],du[1000001],ans,e[1000001];
queue<long long> que;
void build()
{
    for(long long i=1;i<=m;i++)
        xx[i].uu=co[uu[i]],xx[i].vv=co[vv[i]];
    sort(xx+1,xx+m+1,cmp);
    cnt=0;
    memset(head,-1,sizeof(head));
    for(long long i=1;i<=m;i++)
    {
        if(xx[i].uu!=xx[i].vv&&(xx[i].uu!=xx[i-1].uu||xx[i].uu!=xx[i-1].vv))
        {
            add(xx[i].uu,xx[i].vv);
            du[xx[i].vv]++;
        }
    }
    for(long long i=1;i<=col;i++)
    {
        if(du[i]==0)
        {
            dis[i]=si[i];
            que.push(i);
            e[i]=1;
            if(dis[i]>dis[ans]) ans=i;
        }
    }
}
long long anss;
void topu()
{
    while(!que.empty())
    {
        long long xx=que.front();que.pop();
        for(long long i=head[xx];i!=-1;i=x[i].nex)
        {
            du[x[i].v]--;
            if(dis[x[i].v]<dis[xx]+si[x[i].v])
            {
                dis[x[i].v]=dis[xx]+si[x[i].v];
                if(dis[ans]<dis[x[i].v]) ans=x[i].v;
            }
            if(dis[x[i].v]==dis[xx]+si[x[i].v])
            {
                e[x[i].v]=(e[x[i].v]%mod+e[xx]%mod)%mod;
                if(!du[x[i].v]) que.push(x[i].v);
            }
        }
    }
    for(long long i=1;i<=col;i++)
        if(dis[i]==dis[ans]) anss=(anss%mod+e[i]%mod)%mod;
    return;
}
int main()
{
    memset(head,-1,sizeof(head));
    n=read(),m=read(),mod=read();
    for(long long i=1;i<=m;i++)
    {
        uu[i]=read(),vv[i]=read();
        add(uu[i],vv[i]);
    }
    for(long long i=1;i<=n;i++)
        if(!dfn[i]) tarjan(i);
    build();
    topu();
    cout<<dis[ans]<<endl<<anss;
}

by 哔哩哔哩 @ 2018-10-07 13:26:23

很水的题……Orz


by 塔罗兰 @ 2018-10-07 13:30:01

orz


by C201914 @ 2018-10-07 16:34:07

%%%巨佬认为紫题水


by Tommy_clas @ 2018-12-23 10:56:37

认为题很水还需要查错算什么鬼......


by Y15BeTa @ 2019-10-14 19:08:47

@siruiyang_sry %%%巨佬认为紫题水


|