hdu 4635 强连通分量+缩点

本文详细阐述了如何通过强连通分量与缩点算法解决图论问题,具体包括输入格式、输出要求及示例输入输出解析。

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

http://acm.hdu.edu.cn/showproblem.php?pid=4635

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

/**
hdu 4635  强连通分量+缩点
题目大意:给定一个图,问能最多加多少边使其还不是连通图
解题思路:http://www.xuebuyuan.com/1606580.html
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn=100005;

struct note
{
    int v,next;
}edge[maxn*10];

int head[maxn],ip;
int st[maxn],ins[maxn],dfn[maxn],low[maxn],cnt_tar,index,top;
int in[maxn],out[maxn],num[maxn],belong[maxn];
int m,n;

void addedge(int u,int v)
{
    edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

void tarjan(int u)
{
    dfn[u]=low[u]=++index;
    st[++top]=u;
    ins[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        cnt_tar++;
        int j;
        do
        {
            j=st[top--];
            ins[j]=0;
            belong[j]=cnt_tar;
            num[cnt_tar]++;
        }while(j!=u);
    }
}

void solve()
{
    top=0,index=0,cnt_tar=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
            tarjan(i);
    }
}

int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        memset(num,0,sizeof(num));
        solve();
        if(cnt_tar==1)
        {
            printf("Case %d: -1\n",++tt);
            continue;
        }
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        for(int u=1;u<=n;u++)
        {
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].v;
                if(belong[u]==belong[v])continue;
                out[belong[u]]++;
                in[belong[v]]++;
            }
        }
        LL all=(LL)n*(n-1)-m;
        LL ans=0;
        for(int i=1;i<=cnt_tar;i++)
        {
            if(in[i]==0||out[i]==0)
            {
                ans=max(ans,all-(LL)num[i]*(n-num[i]));
            }
        }
        printf("Case %d: %I64d\n",++tt,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值