• bzoj 1093: [ZJOI2007]最大半连通子图


    Description

      一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意
    两点u,v,存在一条u到v的有向路径或者从v到u的有向路径。若G'=(V',E')满足V'?V,E'是E中所有跟V'有关的边,
    则称G'是G的一个导出子图。若G'是G的导出子图,且G'半连通,则称G'为G的半连通子图。若G'是G所有半连通子图
    中包含节点数最多的,则称G'是G的最大半连通子图。给定一个有向图G,请求出G的最大半连通子图拥有的节点数K
    ,以及不同的最大半连通子图的数目C。由于C可能比较大,仅要求输出C对X的余数。

    Input

      第一行包含两个整数N,M,X。N,M分别表示图G的点数与边数,X的意义如上文所述接下来M行,每行两个正整
    数a, b,表示一条有向边(a, b)。图中的每个点将编号为1,2,3…N,保证输入中同一个(a,b)不会出现两次。N ≤1
    00000, M ≤1000000;对于100%的数据, X ≤10^8

    Output

      应包含两行,第一行包含一个整数K。第二行包含整数C Mod X.

    Sample Input

    6 6 20070603
    1 2
    2 1
    1 3
    2 4
    5 6
    6 4

    Sample Output

    3
    3

    HINT

     

    Source

    对于这种有向图的问题一般的解法就是tarjan缩点后转为DAG上的问题,然后用拓扑序dp。

    这个题缩完点后就是求DAG上的最长路及最长路的方案数,可以用拓扑序dp很好的解决。

    至于方案数可以直接在转移的时候由加法原理直接统计(注意去掉重边!!!)

    问题很经典就不再赘述

    // MADE BY QT666
    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    typedef long long ll;
    const int Inf=20010411;
    const int M=1500000;
    const int N=150000;
    int gi()
    {
      int x=0;
      char ch=getchar();
      while(ch<'0'||ch>'9') ch=getchar();
      while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
      return x;
    }
    int head[N],to[M],nxt[M],sum,dfn[N],low[N],cnt=1,tot,vis[N],ans,tt,zhan[N],fr[N],size[N];
    int n,m,mod,ru[N],f[N],num[N],mark[N],ans1,ans2;
    queue<int>q;
    vector<int>p[N];
    void tarjan(int x) {
      dfn[x]=low[x]=++tt;int y;
      vis[x]=1,zhan[++sum]=x;
      for(int i=head[x];i;i=nxt[i]) {
        y=to[i];
        if(!dfn[y]) {
          tarjan(y);low[x]=min(low[x],low[y]);
        }
        else if(vis[y]) low[x]=min(low[x],dfn[y]);
      }
      if(dfn[x]==low[x]) {
        tot++;
        do {
          y=zhan[sum--];
          vis[y]=0;fr[y]=tot;size[tot]++;
        } while(y!=x);
      }
    }
    void pre(){
        for(int i=1;i<=n;i++)
        for(int j=head[i];j;j=nxt[j])
            if(fr[to[j]]!=fr[i]){
            p[fr[i]].push_back(fr[to[j]]);
            ru[fr[to[j]]]++;
            }
        for(int i=1;i<=tot;i++){
        f[i]=size[i],num[i]=1;ans1=max(f[i],ans1);
        if(!ru[i]) q.push(i);
        }
    }
    void lnk(int x,int y){
     to[++cnt]=y,nxt[cnt]=head[x],head[x]=cnt;
    }
    void top_dp(){
        while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<p[x].size();i++){
            int y=p[x][i];ru[y]--;
            if(!ru[y]) q.push(y);
            if(mark[y]==x) continue;
            if(f[x]+size[y]>f[y]) f[y]=f[x]+size[y],num[y]=0;
            if(f[x]+size[y]==f[y]) num[y]=(num[y]+num[x])%mod;
            ans1=max(ans1,f[y]);
            mark[y]=x;
        }
        }
    }
    int main()
    {
        n=gi(),m=gi(),mod=gi();
        for(int i=1;i<=m;i++){
        int x=gi(),y=gi();lnk(x,y);
        }
        for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
        pre();top_dp();
        for(int i=1;i<=tot;i++) if(f[i]==ans1) ans2=(ans2+num[i])%mod;
        printf("%d
    %d",ans1,ans2);
        return 0;
    }
    

      

  • 相关阅读:
    eos合约案例导读
    eos TODO EOS区块链上EOSJS和scatter开发dApp
    电脑提示‘您需要来自Administration的权限才能对此文件夹进行更改’怎么删除文件
    ubuntu 设置全局代理
    eos开发实践
    eos博客
    如何在Ubuntu 18.04上安装Go
    parity密码
    Nodejs基础之redis
    完全搞懂事件
  • 原文地址:https://www.cnblogs.com/qt666/p/6847469.html
Copyright © 2020-2023  润新知