Strongly connected-HDU4635
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最小是式子最大。
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.
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.
If the original graph is strongly connected, just output -1.
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
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的更多相关文章
- Strongly connected(hdu4635(强连通分量))
/* http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/ ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- cf475B Strongly Connected City
B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 【CF913F】Strongly Connected Tournament 概率神题
[CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...
- HDU 4635 Strongly connected (Tarjan+一点数学分析)
Strongly connected Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- 【CodeForces】913 F. Strongly Connected Tournament 概率和期望DP
[题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.每对游戏者i和j(i<j)进行一场游戏,有p的概率i赢j(反之 ...
- HDU4625:Strongly connected(思维+强连通分量)
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 4635 Strongly connected (2013多校4 1004 有向图的强连通分量)
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】
Strongly connected Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- 调用wsdl接口,参数是xml格式
1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...
- 移动设备访问使用百度js跳转
以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.bai ...
- contact用法解析
经典用法: mysql> select concat('11','22','33'); +------------------------+ | concat('11','22','33') | ...
- EasyUI edatagrid插件使用小计
html片段 <table id="menuview" style="width:100%"> <thead> <tr> & ...
- windows保存tomcat的控制台日志到文件
startup.bat修改:call "%EXECUTABLE%" start %CMD_LINE_ARGS%改为:call "%EXECUTABLE%" ru ...
- 从0开始搭建SQL Server 2012 AlwaysOn 第三篇(安装数据,配置AlwaysOn)
这一篇是从0开始搭建SQL Server 2012 AlwaysOn 的第三篇,这一篇才真正开始搭建AlwaysOn,前两篇是为搭建AlwaysOn 做准备的 操作步骤: 1.安装SQL server ...
- 基于jQuery的用户界面插件集合---EasyUI
easyui是一种基于jQuery的用户界面插件集合.为创建现代化,互动,JavaScript应用程序,提供必要的功能.使用easyui你不需要写很多代码,你只需要通过编写一些简单HTML标记,就可以 ...
- 【C语言】控制台窗口图形界面编程(七):鼠标事件
目录 00. 目录 01. INPUT_RECORD结构 02. MOUSE_EVENT_RECORD结构 03. ReadConsoleInput函数 04. 示例程序 00. 目录 01. INP ...
- 指针函数(Pointer Function)和函数指针(Pointer to Function或Function Pointer)
一.指针函数 1.解释:指针函数很好理解:简单来说,就是一个返回指针的函数,本质是一个函数.如: int fun(int x,int y); //这是一个普通函数的声明,返回值是一个int类型, ...
- js 小练习
js 学习之路代码记录 js 加载时间线 1.创建Document对象,开始解析web页面.解析HTML元素和他们的文本内容后添加Element对象和Text节点到文档中.这个阶段document.r ...