Strongly connected

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1802    Accepted Submission(s): 746

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
 
 分析:首先进行强连通对原图进行缩点,然后对于缩点后的图形,只考虑出度为0和入度为0的联通块,一出度为0说明,该联通块里的所有点都不能和该联通块外面的所有点连单向边,总共有sum[i]*(n-sum[i])条边不能连接,一个连通图两两建边,共有n*(n-1)条边,但是题目已有m条边已经连接,所以现在可以连接的有n*(n-1)-m-sum[i]*(n-sum[i]);入度为0的联通块同理,所有度为0的块计算后取较大值即为答案。
程序:
#include"cstdio"
#include"cstring"
#include"cstdlib"
#include"cmath"
#include"string"
#include"map"
#include"cstring"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#define inf 0x3f3f3f3f
#define M 200009
#define eps 1e-8
#define INT int
#define LL __int64
using namespace std;
struct node
{
int u,v,next;
}edge[M*];
int head[M];
int dfn[M];
int belong[M];
int low[M];
int use[M];
int sum[M];
int in[M];
int out[M];
int t,indx,num,cnt,m,n;
stack<int>q;
void init()
{
t=;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
void tarjan(int u)
{
dfn[u]=low[u]=++indx;
q.push(u);
use[u]=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(use[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
int p;
num++;
do
{
p=q.top();
q.pop();
use[p]=;
belong[p]=num;
sum[num]++; }while(p!=u);
}
}
void slove()
{
num=indx=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(use,,sizeof(use));
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
if(num==)
{
printf("-1\n");
return;
}
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(int i=;i<t;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(belong[u]!=belong[v])
{
out[belong[u]]++;
in[belong[v]]++;
}
}
LL ans=;
for(int i=;i<=num;i++)
{
if(!out[i]||!in[i])
{
LL s=(LL)n*(n-)-m-(LL)sum[i]*(n-sum[i]);
if(ans<s)
ans=s;
}
}
printf("%I64d\n",ans);
}
int main()
{
int T,a,b,kk=;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
printf("Case %d: ",kk++);
slove();
}
return ;
}

强连通(hdu4635)最多增加几条单向边后满足最终的图形不是强连通的更多相关文章

  1. 2017 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。

    /** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...

  2. HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】

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

  3. history统计命令最多的20条

    1.1.1 统计使用命令最多的20条 [root@ob1 ~]# history|awk '{ml[$2]++}END{for (i in ml) print i,ml[i]}'|sort -nrk ...

  4. 震惊,当我运行了这条Linux命令后,服务器竟然... (Linux中的删除命令)

    震惊,当我运行了这条Linux命令后,服务器竟然... 0X00 写在前面 大家都听说过删库命令rm -rf /*,但是谁又真正实践过呢?但作为一个程序员,不看看这条命令执行后会发生什么,怎么能甘心呢 ...

  5. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. 增加一条新记录,同时返回其自增id

    方法一.是在Insert或Update触发器中用select来返回需要的字段值.默认情况下,当insert时,触发其insert触发器,它的默认返回值是影响到的行数,语句是:select @@rowc ...

  7. 强连通图(最多加入几条边使得图仍为非强连通图)G - Strongly connected HDU - 4635

    题目链接:https://cn.vjudge.net/contest/67418#problem/G 具体思路:首先用tarjan缩点,这个时候就会有很多个缩点,然后再选取一个含有点数最少,并且当前这 ...

  8. 笔试算法题(19):判断两条单向链表的公共节点 & 字符集删除函数

    出题:给定两个单向链表的头结点,判断其是否有公共节点并确定第一个公共节点的索引: 分析: 由于是单向链表,所以每个节点有且仅有一个后续节点,所以只可能是Y型交叉(每条链表中的某个节点同时指向一个公共节 ...

  9. Strongly connected HDU - 4635 原图中在保证它不是强连通图最多添加几条边

    1 //题意: 2 //给你一个有向图,如果这个图是一个强连通图那就直接输出-1 3 //否则,你就要找出来你最多能添加多少条边,在保证添加边之后的图依然不是一个强连通图的前提下 4 //然后输出你最 ...

随机推荐

  1. [转自小兵的工具箱]C++ Builder 基础

    1. C++ Builder 网上资源 C++ Builder 研究    http://www.ccrun.com/ C++ Builder 程序员    http://mybcb.diy.myri ...

  2. The Producer-Consumer Relationship Version 2

    Listing -. The Producer-Consumer Relationship Version public class PC { public static void main(Stri ...

  3. Day09

    Servlet概述 生命周期方法: void init(ServletConfig):出生之后(1次): void service(ServletRequest request, ServletRes ...

  4. Jquery手册

    1.jQuery 选择器: 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" ...

  5. linux下线程调用sleep,进程挂起

    http://blog.csdn.net/horstlinux/article/details/7911457 http://blog.csdn.net/eroswang/article/detail ...

  6. SecureCRT SSH VMware Ubuntu

    Ubuntu 10.4 装完后网络OK. NAT模式下, 可以上网. 宿主机和客户机可以彼此ping通. 主要是检查SSH服务, 和防火墙是否关闭(UBUNTU 默认没有安装SSH, 默认启动了防火墙 ...

  7. C#引用类型与值类型的比较

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using SimpleDe ...

  8. X-UA-Compatible是神马

    X-UA-Compatible是神马 X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中使 ...

  9. C++ 安全字符串拼接

    #include <stdio.h> #include <stdint.h> #include <stdarg.h> #if defined(__GNUC__) # ...

  10. 3. 如何封装查询条件与查询结果到map中

    public Map<String, Object> queryOrderStatus(String orderNo) { // 查询到的结果与查询的条件一一对应,封装到map中! Str ...