HDU 4635 Strongly connected

G - Strongly connected
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

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

题意: 给你一个简单有向图,问你在不使这个图成为强联通图的情况下所能添加的边的最大数目
思路:1)如果这个图本身就是个强联通分量,那么肯定答案是-1
2)如果这个图不是强联通分量,那么我们就可以求出结果,我们试着逆向思考,N个节点边数最多的有向强联通是完全图,那么我们只要求出最少去掉几条边才可以使得这个完全图不是强联通,那么肯定是去除所有强联通分量中含有点数最少的点的所有进边,并且用这个数减去与已经有的边数,最大值就是我们的答案,
很明显,最终添加完边的图,肯定可以分成两个部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),

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
#include <stack>

using namespace std;
#define MAXN 200010
#define MAXM 400010
#define LL long long

struct Edge
{
    int u, v, next;
}e[MAXM];
int head[MAXM], cnt, dep, blo;
int dfn[MAXN], low[MAXN], vis[MAXN], num[MAXN], belong[MAXN], in[MAXN], out[MAXN];
stack<int> sta;
void init()
{
    memset(head, -1, sizeof(head));
    cnt = dep = blo = 0;
    memset(dfn, 0, sizeof(dfn));
    memset(vis, 0, sizeof(vis));
    memset(num, 0, sizeof(num));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    while(!sta.empty()) sta.pop();
}

void Addedge( int uu, int vv)
{
    e[cnt].u = uu, e[cnt].v = vv;
    e[cnt].next = head[uu], head[uu] = cnt++;
}

void dfs( int u )
{
    vis[u] = 1;
    low[u] = dfn[u] = ++dep;
    sta.push(u);
    for(int i = head[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        if(!dfn[v])
        {
            dfs(v);
            low[u] = min(low[u], low[v]);
        }
        else if(vis[v])
         low[u] = min(low[u],dfn[v]);
    }
    if(low[u] == dfn[u])
    {
        int v;
        ++ blo;
        do
        {
            v = sta.top();
            sta.pop();
            vis[v] = 0;
            num[blo]++;
            belong[v] = blo;
        }while(v != u);
    }
}

void solve(int n, int m)
{
    int LL ans = 0;
    if (n == 1) {printf("-1\n");  return ;}
    for(int i = 1; i <= n; i++)
         if(!dfn[i])
          dfs(i);
    if(blo == 1) {printf("-1\n");  return ;}
    for( int i = 0; i < cnt; i++)
    {
        int u = belong[e[i].u];
        int v = belong[e[i].v];
        if( u != v)
            out[u]++, in[v]++;
    }

    for( int i = 1; i <= blo; i++)
    {
        if(in[i] == 0 || out[i] == 0)
        {
            int tem = num[i];
            LL res  = tem * (tem - 1) + (n - tem)*(n - tem - 1) + tem*(n - tem) - m;
            ans = max(ans, res);
        }
    }
    printf("%lld\n",ans);
}

int main()
{
    int f, r, u, v, ca ,t ;
    ca = 1;
    scanf("%d",&t);
    while(t--)
    {

        scanf("%d %d",&f, &r);
        init();
        for(int i = 0; i < r; i++)
        {
            scanf("%d %d",&u, &v);
            if( u == v) continue;
            Addedge(u , v);
        } 
        printf("Case %d: ",ca++);
        solve(f, r);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值