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. Machine Learning in Action -- Logistic regression

    这个系列,重点关注如何实现,至于算法基础,参考Andrew的公开课 相较于线性回归,logistic回归更适合用于分类 因为他使用Sigmoid函数,因为分类的取值是0,1 对于分类,最完美和自然的函 ...

  2. 用Delphi“遥控”按钮

    很多情况下,我们需要在程序中实现这样的功能:在自编写的程序里控制另外一软件中的某个按钮被按下.比如,有一天你在聊QQ时觉得烦了,那么就想写程序来帮你按下“发送”按钮,省得你自己一次次动手了.那么,这个 ...

  3. ServletContextDemo

    1.servlet 之间共享数据 package xw.servlet; import javax.servlet.ServletContext; import javax.servlet.http. ...

  4. Groovy 在eclipse中的使用

    今天在写Groovy脚本的时候发现一个问题.如下图,element的elementIterator()方法不能智能显示,只能手动输入.而在eclipse中如果类a没有方法a,那么你是用"cl ...

  5. 【转】Servlet与web.xml的配置

    Web.xml常用元素<web-app><display-name></display-name>定义了WEB应用的名字<description>< ...

  6. .NET对象与Windows句柄(三):句柄泄露实例分析

    在上篇文章.NET对象与Windows句柄(二):句柄分类和.NET句柄泄露的例子中,我们有一个句柄泄露的例子.例子中多次创建和Dispose了DataReceiver和DataAnalyzer对象, ...

  7. C#程序读取数据库中包含null的列的值

    private void btn2_Click(object sender, RoutedEventArgs e)         {             using (SqlConnection ...

  8. SQL Server 取日期时只要年月或年月日

    select CONVERT(varchar(7) ,getdate(), 120) as 'Date' from 表名;--只取年月且日期格式为 xxxx-xx select CONVERT(var ...

  9. JS-JQ实现TAB选项卡

    原理:       有两种实现方法,       方法一利用css的display:none 和display:block:交替实现:       方法二利用css的z-index:     

  10. dede如何实现二级栏目导航的仿制

    {dede:channelartlist row='2' typeid='1,2' }<h3><a href='{dede:field name='typeurl'/}'>{d ...