HDU 4635 Strongly connected (Tarjan缩点)

来源:互联网 发布:吉林数据造假 编辑:程序博客网 时间:2024/05/29 10:16

Strongly connected

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


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
33 31 22 33 13 31 22 31 36 61 22 33 14 55 66 4
 

Sample Output
Case 1: -1Case 2: 1Case 3: 15
 

Source
2013 Multi-University Training Contest 4
 

Recommend
zhuyuanchen520
 

Statistic | Submit | Discuss | Note

题目大意:

给你N个顶点,M条边的有向图,问最多加入多少条边之后,这个图仍旧是一个简单图(简单图:无重边,无自环),
并且不是强联通的。如果原始的图就是强联通的话就输出 -1.
思路:
1.最终添加完边的图,肯定可以分成两个部XY,其中只有XY的边没有YX的边,那么要使得边数尽可能的多,则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的点中,包含节点数最少的那个点,令它为一个部,其它所有点加起来做另一个部,就可以得到最多边数的图了

2.

要加边最多那么加边后的图连通分量越少越好,那么连通分量最少也就是2个。先用n个点构造一个完全图(有向图有:n*(n-1)条边,无向图有:n*(n-1)/2条边),再用构造的边 减去原来有的m条边=ans。再用强连通算法缩点,记录每个新点包含点的个数,从入度为0或出度为0的新点中找出包含点数最小的minnum,再用上面剩余的边ans - minnum*(n-minnum)就是所要的答案。因为如果不减入度为0或出度为0相关的边,那么该点本身包含有入边和出边,加的边永远都是强连通图。所以只能去掉与入度为0或出度为0点的相关边,只减掉一个方向的边,要么全是(n-minnum)点数到minnum点数的入边,那么是minnum点数到(n-minnum)点数的出边。

总结:学会逆着想,倒着想,想反面,这样就是用理想/目标状态减去 现有 状态

#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <stack>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1e5 + 5;int n, m, low[maxn], dfn[maxn], id[maxn], scc_cnt, dfs_cnt, cnt[maxn];int in[maxn], out[maxn];vector<int> v[maxn];stack<int> s;void init(){    memset(low, 0, sizeof(low));    memset(id, 0, sizeof(id));    memset(dfn, 0, sizeof(dfn));    memset(in, 0, sizeof(in));    memset(out, 0, sizeof(out));    memset(cnt, 0, sizeof(cnt));    scc_cnt = dfs_cnt = 0;    for(int i = 0; i < maxn; i++)        v[i].clear();    while(!s.empty())        s.pop();}void tarjan(int x){    dfn[x] = low[x] = ++dfs_cnt;    s.push(x);    for(int i = 0; i < v[x].size(); i++)    {        int to = v[x][i];        if(!dfn[to])        {            tarjan(to);            low[x] = min(low[x], low[to]);        }        else if(!id[to])            low[x] = min(low[x], dfn[to]);    }    if(low[x] == dfn[x])    {        scc_cnt++;        while(1)        {            int u = s.top();            s.pop();            id[u] = scc_cnt;            cnt[scc_cnt]++;            if(x == u) break;        }    }}void scc(){    for(int i = 1; i <= n ; i++)        if(!dfn[i])            tarjan(i);}int main(){    int _, ca = 1;    cin >> _;    while(_--)    {        scanf("%d%d", &n, &m);        init();        for(int i = 1; i <= m; i++)        {            int x, y;            scanf("%d%d", &x, &y);            v[x].push_back(y);        }        scc();        if(scc_cnt == 1)        {            printf("Case %d: -1\n", ca++);            continue;        }        for(int i = 1; i <= n; i++)        {            for(int j = 0; j < v[i].size(); j++)            {                int to = v[i][j];                if(id[i] != id[to])                {                    in[id[to]]++;                    out[id[i]]++;                }            }        }        int num = maxn;        for(int i = 1; i <= scc_cnt; i++)        {            if(!in[i]) num = min(cnt[i], num);            if(!out[i]) num = min(cnt[i], num);        }        ll x = num, y = n-num;        printf("Case %d: %lld\n",ca++, x*(x-1)+y*(y-1)+x*y-m);    }    return 0;}


原创粉丝点击