Strongly connected (强连通分量 Tarjan+缩点)

该博客讨论了一种简单的有向图问题,要求在保持图简单的情况下,找出最多能添加多少条边而不形成强连通图。给出了输入输出格式及样例,并提出了思路:首先计算最大可能的边数,然后考虑如何确保添加边后不形成强连通图,即通过强连通分量的性质减少边数。最后提到了利用 Tarjan 算法和缩点来解决这个问题。

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

Strongly connected

 

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

 

题目大意:给出n点m边的有向图,求最多可以加多少条边使得图不是强连通图,如果本来就是强连通图就输出-1;

思路:n 个点之间最多有n*(n-1)条边,减去图中已经连上的m条边,可以加的边最多为ans=n*(n-1)-m,;

其中还要保证加边后的图不是强连通图,所以还要从中减去一定数量D,要使D最小;

要保证不是强连通图,即缩点后要保证不构成环,即保证一个强连通分量入度或者出度为0,所以 D=k*(n-k) (强连通分量的点数与剩余点数的乘积)

最大可加边数 ans=n*(n-1)-m-D(min);

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int N=200000+20;
const int M=200000+20;
const long long inf=1e12+20;
int first[N],dfn[N],low[N];
int in[N],out[N],color[N],vis[N],poi[N];//poi记录连通分量的点数
int tot,cnt,time;
stack<int>s;
struct node
{
    int v,next;
}e[M];
void init() //初始化
{
    mem(first,-1);
    mem(dfn,0);
    mem(low,0);
    mem(vis,0);
    mem(color,0);
    mem(poi,0);
    mem(in,0);
    mem(out,0);
    tot=time=cnt=0;
    while(!s.empty()) s.pop();
}
void adde(int u,int v) //建边
{
    e[tot].v=v;
    e[tot].next=first[u];
    first[u]=tot++;
}
void tarjan(int u)
{
    vis[u]=1;
    s.push(u);
    dfn[u]=low[u]=++time;
    for(int i=first[u];~i;i=e[i].next)
    {
        int v=e[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        cnt++;
        while(1)  
        {
            int now=s.top();
            s.pop();
            vis[now]=0;
            color[now]=cnt; 
            poi[cnt]++;
            if(now==u) break;
        }
    }
}
int main()
{
    int t,k=1;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n,m,x,y;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            adde(x,y);
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i])
            tarjan(i);
        if(cnt==1)
        {
            printf("Case %d: -1\n",k++);
            continue;
        }
        long long ans=n*(n-1)-m;
        for(int i=1;i<=n;i++)
            for(int j=first[i];~j;j=e[j].next)
        {
            int v=e[j].v;
            if(color[i]!=color[v])
            {
                in[color[v]]++;
                out[color[i]]++;
            }
        }
        long long d=inf;
        for(int i=1;i<=cnt;i++)
            if(in[i]==0||out[i]==0)
            {
                long long pos=poi[i]*(n-poi[i]);
                if(pos<d) d=pos;
            }
        printf("Case %d: %lld\n",k++,ans-d);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值