Strongly connected

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

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
 
 
题目大意:给n个结点,m条有向边。问你最多加多少条边,让原图仍然不是强连通的。如果图本身就是强连通的,输出-1
 
解题思路:如果要让图不是强连通图,那么必须会将分成两部分。我们设左部分为X,右部分为Y。X这边的结点个数设为x,X这边的结点个数设为y,想要加边最多,那么可以让左边X成为完全图,右边Y成为完全图。让X与Y之间全部都是一个方向的边,即如果是X--->Y的,那么所有都是从X->Y的;如果是从Y--->X的,那么所有都是Y->X的。那么我们可以得到一个公式计算这样能形成的所有边数  F = x*y + x*(x-1) + y*(y-1)表示X与Y之间形成的边数+X形成完全图的边数+Y形成完全图的边数。x+y = n。公式可以进一步化简得:F = n*n - n - (xy)。这是总的边数。那么如果想让F越大,那么只要x*y值越小即可。当x+y 为定值时,x*y的值越小,则需要x与y的差值越大。那么我们通过缩点以后,让缩点出度或者入度为0的单独放在X或者Y,让剩余的放到另一侧。然后通过枚举这样的缩点,即可找到结果。
 
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<string.h>
#include<vector>
using namespace std;
typedef long long INT;
const int maxn = 100100;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn];
int tot, head[maxn];
void init(){
tot = 0;
memset(head,-1,sizeof(head));
}
void AddEdge(int _u,int _v){
edges[tot] = Edge(_v,head[_u]);
head[_u] = tot++;
}
int dfs_clock, scc_cnt;
int sccno[maxn], dfn[maxn], lowlink[maxn];
stack<int>S;
void dfs(int u){
lowlink[u] = dfn[u] = ++dfs_clock;
S.push(u);
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}else if(!sccno[v]){
lowlink[u] = min(lowlink[u],dfn[v]);
}
}
if(lowlink[u] == dfn[u]){
scc_cnt++;
for(;;){
int x = S.top();
S.pop();
sccno[x] = scc_cnt;
if(x == u){
break;
}
}
}
}
void find_scc(int n){
dfs_clock = scc_cnt = 0;
memset(sccno,0,sizeof(sccno));
memset(dfn,0,sizeof(dfn));
for(int i = 1; i <= n; i++){
if(!dfn[i]){
dfs(i);
}
}
}
int outdeg[maxn], indeg[maxn];
INT sccsz[maxn];
int main(){
int T, m, cas = 0;
INT n;
scanf("%d",&T);
while(T--){
scanf("%lld%d",&n,&m);
init();
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
}
find_scc(n);
printf("Case %d: ",++cas);
if(scc_cnt == 1){
puts("-1");continue;
}
memset(indeg,0,sizeof(indeg));
memset(outdeg,0,sizeof(outdeg));
memset(sccsz,0,sizeof(sccsz));
for(int i = 1; i <= n; i++){
sccsz[sccno[i]]++;
for(int j = head[i]; j != -1; j = edges[j].next){
int v = edges[j].to;
if(sccno[i] == sccno[v]){
continue;
}
indeg[sccno[v]]++;
outdeg[sccno[i]]++;
}
}
INT ans = 0;
for(int i = 1; i <= scc_cnt; i++){
if(indeg[i] == 0 ||outdeg[i] == 0){
ans = max( ans, (n*n - n - sccsz[i]*(n - sccsz[i]))-m );
}
}
printf("%lld\n",ans);
}
return 0;
} /*
555
7 9
1 5
1 2
5 6
6 7
7 5
6 4
2 4
4 3
3 2 */

  

 

HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】的更多相关文章

  1. HDU 4635 Strongly connected(强连通)经典

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. hdu 4635 Strongly connected 强连通缩点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...

  3. hdu 4635 Strongly connected(强连通)

    考强连通缩点,算模板题吧,比赛的时候又想多了,大概是不自信吧,才开始认真搞图论,把题目想复杂了. 题意就是给你任意图,保证是simple directed graph,问最多加多少条边能使图仍然是si ...

  4. hdu 4635 Strongly connected 强连通

    题目链接 给一个有向图, 问你最多可以加多少条边, 使得加完边后的图不是一个强连通图. 只做过加多少条边变成强连通的, 一下子就懵逼了 我们可以反过来想. 最后的图不是强连通, 那么我们一定可以将它分 ...

  5. HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 4635 Strongly connected(强连通分量缩点+数学思想)

    题意:给出一个图,如果这个图一开始就不是强连通图,求出最多加多少条边使这个图还能保持非强连通图的性质. 思路:不难想到缩点转化为完全图,然后找把它变成非强连通图需要去掉多少条边,但是应该怎么处理呢…… ...

  7. HDU 4635 Strongly connected (Tarjan+一点数学分析)

    Strongly connected Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  8. HDU 4635 Strongly connected (强连通分量)

    题意 给定一个N个点M条边的简单图,求最多能加几条边,使得这个图仍然不是一个强连通图. 思路 2013多校第四场1004题.和官方题解思路一样,就直接贴了~ 最终添加完边的图,肯定可以分成两个部X和Y ...

  9. HDU 4635 - Strongly connected(2013MUTC4-1004)(强连通分量)

    t这道题在我们队属于我的范畴,最终因为最后一个环节想错了,也没搞出来 题解是这么说的: 最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数尽可能的多,则X部肯 ...

随机推荐

  1. MVVM模式WPF的ComboBox数据绑定,使用Dictionary作为数据源

    ViewModel//属性定义 Dictionary<int, string> _selGroupList; /// <summary> /// 分组下拉列表 /// < ...

  2. c# ftp创建文件(非上传文件)

    c#  ftp创建文件(非上传文件) 一.奇葩的故事: 今天项目中遇到这么个奇葩的问题,ftp文件传输完成后要在ftp目录下另一个文件夹下创建对应的空文件,听说是为了文件的完整性,既然这么说,那么就必 ...

  3. redis-淘汰策略

    将redis用作缓存时,如果内存空间用满,就会自动驱逐老的数据.默认情况下,memcached就是这种方式. LRU是Redis唯一支持的回收算法. maxmemory配置指令 maxmemory用于 ...

  4. 关于jquery.extend()的坑:我的数组变成相同元素了?

    首先呢我有一个数组,存放了多个json对象.这些json对象的属性有缺失,我设置了一个对象模板来存放默认值 先来看一段代码 var source = [ { name: 'dapianzi', bor ...

  5. 【Linux】-Ubuntu常用命令吐血整理

    前言 刚刚接触Linux操作系统,真的是各种艰难啊,用个什么东西都得从头开始配置,这个时候才明白从头再来是什么滋味了.自己装了数个数十几次的Centos版本的Linux系统,好不容易争气了一次,跑了起 ...

  6. server2008 IIS7配置全过程(包括发布ASP网站)

    一.安装IIS 1.打开服务器管理器->角色->添加角色,选中WEB服务器(IIS),记得要把包括之后出现的ASP相关的东西都选中并安装,安装成功后,打开http://localhost/ ...

  7. uoj#453. 【集训队作业2018】围绕着我们的圆环(线性代数+递推)

    题面 传送门 题解 我对线代一无所知 如果下面有啥说错的地方请说出来省的我一辈子都搞不明白 如果你没看懂以下在讲什么不要紧,因为我也没看懂 首先,关于\(A\times B \equiv C \pmo ...

  8. P3749 [六省联考2017]寿司餐厅 最小割

    \(\color{#0066ff}{ 题目描述 }\) Kiana 最近喜欢到一家非常美味的寿司餐厅用餐. 每天晚上,这家餐厅都会按顺序提供 \(n\) 种寿司,第 \(i\) 种寿司有一个代号 \( ...

  9. 小程序渲染问题:ios显示安卓不显示

    问题描述: 测试库转到正式库后添加数据,小程序数据渲染不出来,但是测试库没问题,ios数据能显示,没问题,但是安卓显示没数据. 排除是服务器https证书问题,如果是证书问题,小程序会直接调不了接口. ...

  10. rhcs红帽插件及 轮循

    server1:yum install luci ricci -yecho westos | passwd -stdin  ricci/etc/init.d/ricci startchkconfig ...