hdu4635

B - Strongly connected
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can add that the graph is still a simple directed graph. Also, after you add these edges, this graph must NOT be strongly connected. 
A simple directed graph is a directed graph having no multiple edges or graph loops.
A strongly connected digraph is a directed graph in which it is possible to reach any node starting from any other node by traversing edges in the direction(s) in which they point. 
 

Input

The first line of date is an integer T, which is the number of the text cases. 
Then T cases follow, each case starts of two numbers N and M, 1<=N<=100000, 1<=M<=100000, representing the number of nodes and the number of edges, then M lines follow. Each line contains two integers x and y, means that there is a edge from x to y.
 

Output

For each case, you should output the maximum number of the edges you can add. 
If the original graph is strongly connected, just output -1.
 

Sample Input

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

Sample Output

 
      
Case 1: -1 Case 2: 1 Case 3: 15
 题意:给定一些点和边(有向边),问最多可以添加多少条边使使该图不是强连通图;
解:正向难思考,需要逆向方法,一个有n个点的有向图,最多有sum=(n-1)*n条边,这时它肯定是强连通图,减去多少条边可以使其变成非相强连通图, 已经给了m条边,sum-=m 这时把已经强连通的部分进行缩点,对于缩好的点我们把他们分成两部分,保证其中一部分到另一部分没有边(这两部分不强连通),再把sum减去两部分能构成所有的边数,取最大值即为答案;

具体做时枚举每个小强连通块,找到num[i]*(n-num[i])最小的情况(num[i]为小强连通块点数),其中必须出度或入度为0的连通块才可以被选择

ac代码

#include 
#include 
#include 
#include 

using namespace std;
const int maxn=100010,maxm=100010;
struct Edge
{
    int to,next;
    bool cut;///标记是否为桥
}edge[maxm];
int head[maxn],tot;
int low[maxn],dfn[maxn],Stack[maxn];
int Index,top;
bool Instack[maxn],cut[maxn];
int add_block[maxn];///删除一个点以后正价的连通块
int bridge;
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    edge[tot].cut=false;
    head[u]=tot++;
}
void Tarjan(int u,int pre)
{
    int v;
    low[u]=dfn[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;
    int son=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(v==pre)continue;
        if(!dfn[v])
        {
            son++;
            Tarjan(v,u);
            if(low[u]>low[v])low[u]=low[v];
            ///桥,
            ///一条边(u,v)是桥,当且仅当(u,v)为树枝边,且满足dfn[u]dfn[u])
            {
                bridge++;
                edge[i].cut=true;
                edge[i^1].cut=true;
            }
            ///割点
            ///一个顶点u是割点,当且仅当满足(1)或(2),(1)为树根,且u有多于一个子树
            ///(2)u不为树根,且满足存在(u,v)为树枝边
            ///(或称父子边)即u为v在搜索数中的父亲使得dfn[u]<=low[v]
            if(u!=pre&&low[v]>=dfn[u])
            {
                cut[u]=true;
                add_block[u]++;
            }

        }
        else if(low[u]>dfn[v])
        low[u]=dfn[v];
    }
    if(u==pre&&son>1)cut[u]=true;
    if(u==pre)add_block[u]=son-1;
    Instack[u]=false;
    top--;
}
int solve(int N)
{
    int ans=0;
    memset(dfn,0,sizeof(dfn));
    memset(Instack,0,sizeof(Instack));
    memset(add_block,0,sizeof(add_block));
    memset(cut,false,sizeof(cut));
    Index=0;
    bridge=0;
    for(int i=1;i<=N;i++)
        if(!dfn[i])
        Tarjan(i,i);
    for(int i=1;i<=N;i++)
        if(cut[i])
        ans++;
    return ans;
}
int main()
{
    int n;
    while(~scanf("%d",&n),n)
    {
        init();
        int a,b;
        char c;
        while(~scanf("%d",&a),a)
        {
            while(~scanf("%d%c",&b,&c))
            {
                add(a,b);
                add(b,a);
               if(c=='\n')break;
            }
        }
        printf("%d\n",solve(n));
    }
    return 0;
}



你可能感兴趣的:(-------连通图)