HDU ~ 4635 ~ Strongly connected (强连通 + 缩点 + 贪心)

在这里插入图片描述

题意

T组测试数据,每组输入n,m表示n个点m条边,问最多增加多少条边可以使得图仍然不为强连通图。

思路

首先强连通缩点。
①找到入度为0或者出度为0包含点最少的缩点,把该缩点内部补成完全图,假设该缩点内部含有 x x x个点,则边数为 x ∗ ( x − 1 ) x*(x-1) x(x1)
②除了 x x x那个缩点包含的点以外其他点补成完全图,假设有 y y y个点,则边数为 y ∗ ( y − 1 ) y*(y-1) y(y1)
③现在可以理解为只有 x x x y y y 两个点。如果 x x x 入度为0,那么我们把所有 x x x 中的点向 y y y 中的点建一条边。如果 x x x 出度为0,那么我们把所有 y y y 中的点向 x x x 中的点建一条边,这样保证了 x x x只有出度或者入度, x x x y y y永远不可能强连通。则边数为 x ∗ y x*y xy
那么答案就是: x ∗ ( x − 1 ) + y ∗ ( y − 1 ) + x ∗ y − m x*(x-1) + y*(y-1) + x*y - m x(x1)+y(y1)+xym

另一种理解方法:

我们把原图先搞成一个完全图,然后我们把上面那个①条件中的点的所有出边或者所有入边删掉。
那么答案就是: n ∗ ( n − 1 ) − x ∗ y − m n*(n-1) - x*y - m n(n1)xym

注意答案开long long即可。

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+5;
const int INF = 0x3f3f3f3f;
struct Edge
{
    int from, to;
    Edge (int from, int to): from(from), to(to) {}
};
struct SCC
{
    int n, m;
    int DFN[MAXN], LOW[MAXN], sccno[MAXN], dfs_clock, scc_cnt;
    vector<Edge> edges;
    vector<int> G[MAXN];
    stack<int> S;
    void init(int n)
    {
        this->n = n, m = 0;
        edges.clear();
        for (int i = 0; i <= n; i++) G[i].clear();
    }
    void AddEdge (int from, int to)
    { 
        edges.push_back(Edge(from, to));
        m = edges.size();
        G[from].push_back(m-1);
    }
    void dfs(int u)
    {
        DFN[u] = LOW[u] = ++dfs_clock;
        S.push(u);
        for (int i = 0; i < G[u].size(); i++)
        {
            Edge e = edges[G[u][i]];
            int v = e.to;
            if (!DFN[v])
            {
                dfs(v);
                LOW[u] = min(LOW[u], LOW[v]);
            }
            else if (!sccno[v])
                LOW[u] = min(LOW[u], DFN[v]);
        }
        if (LOW[u] == DFN[u])
        {
            scc_cnt++;
            while (1)
            {
                int x = S.top(); S.pop();
                sccno[x] = scc_cnt;
                if (x == u) break;
            }
        }
    }
    void find_scc()
    {
        dfs_clock = scc_cnt  = 0;
        memset(DFN, 0, sizeof(DFN)), memset(sccno, 0, sizeof(sccno));
        for (int i = 0; i < n; i++)
            if (!DFN[i]) dfs(i);
    }

}gao;

int n, m, in[MAXN], out[MAXN], num[MAXN];
int main()
{
    int T, CASE = 1; scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        gao.init(n);
        for (int i = 0; i < m; i++)
        {
            int u, v; scanf("%d%d", &u, &v);
            u--, v--;
            gao.AddEdge(u, v);
        }
        gao.find_scc();
        if (gao.scc_cnt == 1) { printf("Case %d: -1\n", CASE++); continue; }
        memset(num, 0, sizeof(num));
        for (int i = 0; i < n; i++) num[gao.sccno[i]]++;
        memset(in, 0, sizeof(in)), memset(out, 0, sizeof(out));
        for (int i = 0; i < gao.m; i++)
        {
            int u = gao.edges[i].from, v = gao.edges[i].to;
            if (gao.sccno[u] != gao.sccno[v])
                out[gao.sccno[u]]++, in[gao.sccno[v]]++;
        }
        int x = INF;
        for (int i = 1; i <= gao.scc_cnt; i++) 
            if (in[i] == 0 || out[i] == 0) x = min(x, num[i]);
        int y = n-x;
        long long ans = 1LL*x*(x-1) + 1LL*y*(y-1) + 1LL*x*y - m;
        //long long ans = 1LL*n*(n-1) - 1LL*x*y - m;
        printf("Case %d: %lld\n", CASE++, ans);
    }
    return 0;
}
/*
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
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值