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

题目大意:

n个点,m条边,求最多再加几条边,然后这个图不是强连通

分析:

这是一个单向图,如果强连通的话,他最多应该有n*(n-1)条边,假设有a个强连通块,任取其中一个强连通块,假设取出的这个强连通块里有x个点,剩下的(n-a)个点看成一个强连通块,如果让这两个强连通块之间不联通,肯定是这两个只有一个方向的边,最多就会有x*(n-x)条边  所以最多加n*(n-1)-x*x(n-x)-m边。所以当x最小是式子最大。

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
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector> using namespace std;
#define N 100005
#define INF 0x3f3f3f3f struct node
{
int to,next;
}edge[N*]; int low[N],dfn[N],Time,top,ans,Stack[N],belong[N],sum,head[N],aa[N],in[N],out[N],Is[N]; void Inn()
{
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(Stack,,sizeof(Stack));
memset(belong,,sizeof(belong));
memset(head,-,sizeof(head));
memset(aa,,sizeof(aa));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(Is,,sizeof(Is));
Time=top=ans=sum=;
} void add(int from,int to)
{
edge[ans].to=to;
edge[ans].next=head[from];
head[from]=ans++;
} void Tarjin(int u,int f)
{
low[u]=dfn[u]=++Time;
Stack[top++]=u;
Is[u]=;
int v;
for(int i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].to;
if(!dfn[v])
{
Tarjin(v,u);
low[u]=min(low[u],low[v]);
}
else if(Is[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
sum++;
do
{
v=Stack[--top];
belong[v]=sum;
aa[sum]++;
Is[v]=;
}while(v!=u);
}
} void solve(int n,int m)
{
for(int i=;i<=n;i++)
{
if(!dfn[i])
Tarjin(i,);
}
if(sum==)
{
printf("-1\n");
return ;
}
long long Max=;
for(int i=;i<=n;i++)
{
for(int j=head[i];j!=-;j=edge[j].next)
{
int u=belong[i];
int v=belong[edge[j].to];
if(u!=v)
{
in[v]++;
out[u]++;
}
}
}
long long c=n*(n-)-m;
for(int i=;i<=sum;i++)
{
if(!in[i] || !out[i])
Max=max(Max,c-(aa[i]*(n-aa[i])));
}
printf("%lld\n",Max);
}
int main()
{
int T,n,m,a,b,i,t=;
scanf("%d",&T);
while(T--)
{
Inn();
scanf("%d %d",&n,&m);
for(i=;i<m;i++)
{
scanf("%d %d",&a,&b);
add(a,b);
}
printf("Case %d: ",t++);
solve(n,m);
}
return ;
}

Strongly connected-HDU4635的更多相关文章

  1. Strongly connected(hdu4635(强连通分量))

    /* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...

  2. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  3. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  4. cf475B Strongly Connected City

    B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. 【CF913F】Strongly Connected Tournament 概率神题

    [CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...

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

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

  7. 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP

    [题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...

  8. HDU4625:Strongly connected(思维+强连通分量)

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

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

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

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

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

随机推荐

  1. Spark学习之Spark调优与调试(7)

    Spark学习之Spark调优与调试(7) 1. 对Spark进行调优与调试通常需要修改Spark应用运行时配置的选项. 当创建一个SparkContext时就会创建一个SparkConf实例. 2. ...

  2. linux下安装xampp

    Choose your flavor for your linux OS, the 32-bit or 64-bit version. Change the permissions to the in ...

  3. iOS Programming Touch Events and UIResponder

    iOS Programming Touch Events and UIResponder  1 Touch Events  As a subclass of UIResponder, a UIView ...

  4. table鼠标滑过变颜色

    table鼠标滑过变颜色 添加 table tr:hover{background-color: #eee;} 设置鼠标滑过行背景变色,重新刷新浏览器页面.  一般设置灰色,eee

  5. 洛谷 P2341 [HAOI2006]受欢迎的牛

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  6. CentOS 7 挂载ntfs磁盘格式的U盘

    因为CentOS 默认不识别NTFS的磁盘格式,所以我们要借助另外一个软件来挂载,那就是ntfs-3g了 自带的yum源没有这个软件,要用第三方的软件源,这里我用的是阿里的epel. 1. 切换到系统 ...

  7. fedora下yum安装gnome和kde桌面 (有问题 )

    转自:   http://linux.chinaunix.net/techdoc/system/2009/08/31/1133198.shtml 1.1  安装KDE桌面环境 yum groupins ...

  8. BEGIN - 开始一个事务块

    SYNOPSIS BEGIN [ WORK | TRANSACTION ] DESCRIPTION 描述 BEGIN 初始化一个事务块, 也就是说所有 BEGIN 命令后的用户语句都将在一个事务里面执 ...

  9. 01XML文档结构

    文档结构 2.1文档结构 2.1.1文档声明及字符编码 <?xml version=“1.0” encoding=“”gb2312 standalone=“yes”?> <?  告诉 ...

  10. genymotion 双击打开后 图标只显示在任务栏 无法在电脑上显示

    解决办法 删除 c:/users/user/AppData/local/Genymobile  例如:C:\Users\lenovo\AppData\Local\Genymobile 删除注册表:HK ...