HDU 4635 Strongly connected (Tarjan+一点数学分析)

Strongly connected

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

2013 Multi-University Training Contest 4
 
题目大意:
给你n个点,m条边,给这个图添加最多的边,但不能让它变成强连通,输出边数(如果原始图已经是强连通图了,就输出-1)
题解:
最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部中每个点到Y部的每个点都有一条边,假设X部有x个点,Y部有y个点,有x+y=n,同时边数F=x*y+x*(x-1)+y*(y-1),整理得:F=N*N-N-x*y,(这还没去掉已经有了的边m,就是答案),当x+y为定值时,二者越接近,x*y越大,所以要使得边数最多,那么X部和Y部的点数的个数差距就要越大,所以首先对于给定的有向图缩点,对于缩点后的每个点,如果它的出度或者入度为0,那么它才有可能成为X部或者Y部,所以只要求缩点之后的出度或者入度为0的点中,包含节点数最少的那个点,令它为一个部,其它所有点加起来做另一个部,就可以得到最多边数的图了
来源:http://www.cnblogs.com/jackge/p/3231767.html
看了题解,豁然开朗。。。强啊!!!
#include <bits/stdc++.h>
using namespace std;
const int N=100000+5;
int dfn[N],low[N],team[N],num[N],in[N],out[N];
bool instack[N];
int n,T,m,index,team_num;
vector<int> mp[N];
stack<int> S;
void Tarjan(int u)
{
    low[u]=dfn[u]=++index;
    S.push(u);
    instack[u]=1;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if (!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if (instack[v]) low[u]=min(low[u],dfn[v]);
    }
    if (dfn[u]==low[u])
    {
        team_num++;
        while(1)
        {
            int v=S.top(); S.pop();
            instack[v]=0;
            team[v]=team_num;  // v点是第几组
            num[team_num]++;  //第i组的点个数
            if (v==u) break;
        }
    }
}
void dfs()
{
  memset(low,0,sizeof(low));
  memset(dfn,0,sizeof(dfn));
  memset(instack,0,sizeof(instack));
  memset(team,0,sizeof(team));
  memset(num,0,sizeof(num));
  team_num=0;
  index=0;
  for(int i=1;i<=n;i++)
    if (!dfn[i]) Tarjan(i);
}
int main()
{
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) mp[i].clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
        }
        dfs();  //缩点,求出各个组的点数
        printf("Case %d: ",cas);
        for(int i=1;i<=team_num;i++) in[i]=out[i]=0;
        for(int i=1;i<=n;i++)
            for(int j=0;j<mp[i].size();j++)
        {
            if (team[i]!=team[mp[i][j]])
            {
                in[team[mp[i][j]]]++;
                out[team[i]]++;
            }
        }
        //统计入度数和出度数
        int minn=100000;
        for(int i=1;i<=team_num;i++)
            if (in[i]==0 || out[i]==0) minn=min(minn,num[i]);
        //求出入度=0或者出度=0的点数最小的组
        if (team_num==1) printf("-1\n");
            else printf("%lld\n",(long long)n*n-n-(long long)minn*(n-minn)-m);
    }
    return 0;
}

 

posted on 2017-10-14 19:53  Yxter  阅读(224)  评论(0编辑  收藏  举报

导航