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
 
根据题意这个半连通子图应该是个环。。。然后用tarjan缩点,然后找最长链就是答案,然后DP。。。
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 using namespace std;
  5 int read()
  6 {
  7     int x=0,f=1;char ch=getchar();
  8     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  9     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 10     return x*f;
 11 }
 12 int mx,ans;
 13 int ind,cnt,scc,top;
 14 int n,m,X;
 15 int last[100005],last2[100005];
 16 int dfn[100005],low[100005],hav[100005],belong[100005],q[100005];
 17 int r[100005],f[100005],g[100005],vis[100005];
 18 bool inq[100005];
 19 struct edge{int to,next;}e[2000005],ed[2000005];
 20 void insert(int u,int v)
 21 {
 22     e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
 23 }
 24 void ins(int u,int v)
 25 {
 26     ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;
 27     r[v]++;
 28 }
 29 void tarjan(int x)
 30 {
 31     dfn[x]=low[x]=++ind;
 32     q[++top]=x;inq[x]=1;
 33     for(int i=last[x];i;i=e[i].next)
 34         if(!dfn[e[i].to])
 35             tarjan(e[i].to),low[x]=min(low[x],low[e[i].to]);
 36         else if(inq[e[i].to])low[x]=min(low[x],dfn[e[i].to]);
 37     int now=0;
 38     if(low[x]==dfn[x])
 39     {
 40         scc++;
 41         while(now!=x)
 42         {
 43             now=q[top];top--;
 44             inq[now]=0;
 45             hav[scc]++;
 46             belong[now]=scc;
 47         }
 48     }
 49 }
 50 void rebuild()
 51 {
 52     cnt=0;
 53     for(int x=1;x<=n;x++)
 54     {
 55         for(int i=last[x];i;i=e[i].next)
 56             if(belong[x]!=belong[e[i].to])
 57                 ins(belong[x],belong[e[i].to]);
 58     }
 59 }
 60 void dp()
 61 {
 62     int head=0,tail=0;
 63     for(int i=1;i<=scc;i++)
 64     {
 65         if(!r[i])q[tail++]=i;
 66         f[i]=hav[i];g[i]=1;
 67     }
 68     while(head!=tail)
 69     {
 70         int now=q[head];head++;
 71         for(int i=last2[now];i;i=ed[i].next)
 72         {
 73             r[ed[i].to]--;
 74             if(!r[ed[i].to])q[tail++]=ed[i].to;
 75             if(vis[ed[i].to]==now)continue;
 76             if(f[now]+hav[ed[i].to]>f[ed[i].to])
 77             {
 78                 f[ed[i].to]=f[now]+hav[ed[i].to];
 79                 g[ed[i].to]=g[now];
 80             }
 81             else if(f[now]+hav[ed[i].to]==f[ed[i].to])
 82                 g[ed[i].to]=(g[ed[i].to]+g[now])%X;
 83             vis[ed[i].to]=now;
 84         }
 85     }
 86 }
 87 int main()
 88 {
 89     n=read();m=read();X=read();
 90     for(int i=1;i<=m;i++)
 91     {
 92         int u=read(),v=read();
 93         insert(u,v);
 94     }
 95     for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
 96     rebuild();
 97     dp();
 98     for(int i=1;i<=scc;i++)
 99     {
100         if(f[i]>mx)mx=f[i],ans=g[i];
101         else if(f[i]==mx)ans=(ans+g[i])%X;
102     }
103     printf("%d\n%d\n",mx,ans);
104     return 0;
105 }
View Code

 

posted @ 2016-05-27 10:12  HTWX  阅读(159)  评论(0编辑  收藏  举报