小可爱求助qwq

P2272 [ZJOI2007] 最大半连通子图

Arcturus1350 @ 2018-07-31 19:48:20

5wa

一脸懵逼.jpg

要是帮忙解决的话,就把小可爱抱走吧qwq

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,x;
int ans;int sum;int scc;
int u,v;
struct data{
    int v;int next;
}edge[1000010],node[1000100];
int alist[100010];
int head[1000010];
int cnt;
void add(int u,int v)
{
    edge[++cnt].v=v;
    edge[cnt].next=alist[u];
    alist[u]=cnt;
    return ;
}
int d[1000010];
void ins(int u,int v)
{
    node[++cnt].v=v;
    node[cnt].next=head[u];
    head[u]=cnt;
    d[v]++;
    return ;
}
int dfn[1000010];int dfu;
int stack[1000010];int top;
int low[10000010];bool book[1000010];
int poi[1000010];int bel[10000010];
void tarjan(int x)
{
    low[x]=dfn[x]=++dfu;
    stack[++top]=x;
    book[x]=true;
    int next=alist[x];
    while(next)
    {
//      printf("%d %d\n",x,next);
        int v=edge[next].v;
        if(!dfn[v]) tarjan(v),low[x]=min(low[x],low[v]);
        else if(book[v]) low[x]=min(low[x],dfn[v]);
        next=edge[next].next;
    }
    int now=0;
    if(low[x]==dfn[x])
    {
        scc++;
        while(now!=x)
        {
            now=stack[top],top--;
            book[now]=false;
            poi[scc]++;
            bel[now]=scc;
        }
    }
    return ;
}
void rebuilt()
{
    cnt=0;
    for(int i=1;i<=n;i++){
        int next=alist[i];
        while(next)
        {
            if(bel[i]!=bel[edge[next].v])
              ins(bel[i],bel[edge[next].v]);
//            printf("%d %d\n",bel[i],bel[edge[next].v]);
            next=edge[next].next;
        }
    }
    return ;
}

queue<int> q;
int f[100010];int g[100010];int dp[100010];
void dynamic()
{
    for(int i=1;i<=scc;i++){
        if(!d[i]) q.push(i);
        f[i]=poi[i],g[i]=1;
    }
    while(!q.empty())
    {
        int x=q.front();
        q.pop();int next=head[x];
        while(next)
        {
            d[node[next].v]--;
            if(!d[node[next].v]) q.push(node[next].v);
            if(dp[node[next].v==x])continue;
            if(f[x]+poi[node[next].v]>f[node[next].v]){
              f[node[next].v]=f[x]+poi[node[next].v];
              g[node[next].v]=g[x];
            }
            else if(f[x]+poi[node[next].v]==f[node[next].v]) 
              g[node[next].v]=(g[node[next].v]+g[x])%x;
            dp[node[next].v]=x;
            next=node[next].next;
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&x);
    for(int i=1;i<=m;i++) scanf("%d%d",&u,&v),add(u,v);
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
    rebuilt();dynamic();
    for(int i=1;i<=scc;i++)
      if(f[i]>ans) ans=f[i],sum=g[i];
      else if(f[i]==ans) sum=(sum+g[i])%x;
//  for(int i=1;i<=cnt;i++) printf("%d %d\n",node[i].v,node[i].next);
    printf("%d\n%d",ans,sum%x);
    return 0;
}

by shadowice1984 @ 2018-07-31 20:27:42

@2017zc 他是纯爷们不要误会


by shadowice1984 @ 2018-07-31 20:28:02

@小粉兔 他是纯爷们不要误会了


by 奶酥奶酥QwQ @ 2018-07-31 20:28:18

@cn:苏卿念 没办法抱你走啊,我也不会qwq。 或者说我没做过这题qaq


by Arcturus1350 @ 2018-07-31 21:13:41

@待捕捉蒟蒻QwQ 那……本宝宝就把你抱走吧qwq


by Niko @ 2018-08-12 19:29:42

挖....挖坟


by Starrydream @ 2018-10-12 19:52:40

前来挖坟qwq


by presucc @ 2020-08-12 20:44:34

wf


上一页 |