HDU 4635 Strongly connected(构造最大非强连通图)

87 篇文章 0 订阅

Strongly connected

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2887    Accepted Submission(s): 1193


Problem 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
 

Source
 

Recommend
zhuyuanchen520

题目大意:

    给你一个简单图,问最多能加多少条边,构成一个非强连通的简单图。


解题思路:

    考虑加边不好下手,那么我们考虑从完全图删边。那么要让总边数最多就一定是只有两个强连通分量,只有一个联通分量到另一个有满边,而且这两个联通分量的点数差距还要尽可能的大。

    那么我们就可先强连通分量分解,然后找到到其它强连通分量只有入边或出边的最小强连通分量,按照刚才的方式构造边,再减去原图中的边即为答案。


AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const LL MAXV=100000+3;
vector<LL> G[MAXV];
LL vis[MAXV],dfn[MAXV],low[MAXV],belong[MAXV],tmpdfn,cnt,num[MAXV];
LL V,E,in[MAXV],out[MAXV];
stack<LL> st;

void init()//初始化
{
    for(LL i=0;i<V;++i)
    {
        G[i].clear();
        vis[i]=0;
        num[i]=0;
        in[i]=out[i]=0;
    }
    tmpdfn=0;
    cnt=0;
}

void tarjan(LL u)//tarjan求强连通分量
{
    vis[u]=1;
    dfn[u]=low[u]=tmpdfn++;
    st.push(u);
    for(LL i=0;i<G[u].size();++i)
    {
        LL v=G[u][i];
        if(vis[v]==0)
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v]==1)
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        LL v;
        do{
            v=st.top(); st.pop();
            belong[v]=cnt;
            ++num[cnt];
            vis[v]=2;
        }while(v!=u);
        ++cnt;
    }
}

int main()
{
    LL T_T;
    scanf("%lld",&T_T);
    for(LL cas=1;cas<=T_T;++cas)
    {
        scanf("%lld%lld",&V,&E);
        init();
        for(LL i=0;i<E;++i)
        {
            LL u,v;
            scanf("%lld%lld",&u,&v);
            --u;
            --v;
            G[u].push_back(v);
        }
        for(LL i=0;i<V;++i)
            if(vis[i]==0)
                tarjan(i);
        if(cnt==1)//已经强连通
        {
            printf("Case %lld: -1\n",cas);
            continue;
        }
        for(int u=0;u<V;++u)//统计缩点之后的入度与出度
            for(int i=0;i<G[u].size();++i)
            {
                LL v=G[u][i];
                if(belong[u]!=belong[v])
                {
                    ++in[belong[v]];
                    ++out[belong[u]];
                }
            }
        LL the_min=INF;
        for(LL i=0;i<cnt;++i)//找到满足要求的最小强连通分量
            if(in[i]==0||out[i]==0)
                the_min=min(the_min,num[i]);
        LL other_v=V-the_min;
        LL other_e=(other_v)*(other_v-1);
        printf("Case %lld: %lld\n",cas,the_min*(the_min-1)+other_e+the_min*other_v-E);
    }
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值