HDU 4635 Strongly connected (Tarjan缩点)

探讨如何在有向图中增加最多边数,使其保持简单图且不形成强连通图的方法。通过缩点和寻找合适的两部分来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
  
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
 

Statistic |  Submit |  Discuss |  Note

题目大意:

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

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值