HUD 4635 Strongly connected(tarjan缩点,构造完全图,更新最小值)

题目链接:https://vjudge.net/contest/396078#problem/O

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。
简单有向图是指没有多条边或图环的有向图。

分析:
tarjan缩点对原图进行处理,若强联通分量为1,直接输出-1。
若该图为完全图,则总的边数为 n * (n-1)。
找到强联通入度或出度为0的点
假设这一部分点的个数为x,则把其余n-x个点归在一起。
可以把x个点都和n-x个点连一条边。

维护:所有的边 - 起初的m条边 - x * (n-x)

完整代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
vector<int>G[N];
int low[N],dfn[N],dfstime;
int st[N],top;
int co[N],col;
int in[N],out[N];
int fir[N],net[N],to[N],cnt;
int si[N];///记录每一个强联通分量内有几个点
void init()
{
    memset(fir,0,sizeof(fir));
    cnt=0;
    memset(net,0,sizeof(net));
    memset(st,0,sizeof(st));
    top=0;
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    dfstime=0;
    memset(co,0,sizeof(co));
    col=0;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(si,0,sizeof(si));
}
void Ins(int x,int y)
{
    to[++cnt]=y;
    net[cnt]=fir[x];
    fir[x]=cnt;
}
void tarjan(int u)
{
    low[u]=dfn[u]=++dfstime;
    st[++top]=u;
    for(int i=fir[u]; i; i=net[i])
    {
        int v=to[i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!co[v])///结点v还在栈内:v不属于任何强联通分量
            low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        co[u]=++col;//标记属于哪一个强联通分量
        ++si[col];
        while(st[top]!=u)
        {
            co[st[top]]=col;
            ++si[col];
            --top;
        }
        --top;///u已经标记
    }
}
int main()
{
    int t,n,m;
    int flag=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0; i<m; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            Ins(x,y);
        }
        for(int i=1; i<=n; i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
        printf("Case %d: ",flag++);
        if(col==1)///只有一个强联通分量,该图一定是连通的
        {
            printf("-1\n");
            continue;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=fir[i]; j; j=net[j])
            {
                if(co[i]!=co[to[j]])
                    in[co[to[j]]]++,out[co[i]]++;
            }
        }
        int point=INF;
        for(int i=1; i<=col; i++)
        {
            if(!in[i]||!out[i])
                point=min(point,si[i]);
        }
        LL sum=n*(n-1)-m-point*(n-point);
        printf("%lld\n",sum);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值