HDU 4635 Strongly connected(构造最大非强连通图)
2017-07-09 21:06
302 查看
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2887 Accepted Submission(s): 1193
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
Source
2013 Multi-University Training Contest 4
Recommend
zhuyuanchen520
题目大意:
给你一个简单图,问最多能加多少条边,构成一个非强连通的简单图。
解题思路:
考虑加边不好下手,那么我们考虑从完全图删边。那么要让总边数最多就一定是只有两个强连通分量,只有一个联通分量到另一个有满边,而且这两个联通分量的点数差距还要尽可能的大。
那么我们就可先强连通分量分解,然后找到到其它强连通分量只有入边或出边的最小强连通分量,按照刚才的方式构造边,再减去原图中的边即为答案。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
const LL MAXV=100000+3;
vector<LL> G[MAXV];
LL vis[MAXV],dfn[MAXV],low[MAXV],belong[MAXV],tmpdfn,cnt,num[MAXV];
LL V,E,in[MAXV],out[MAXV];
stack<LL> st;
void init()//初始化
{
for(LL i=0;i<V;++i)
{
G[i].clear();
vis[i]=0;
num[i]=0;
in[i]=out[i]=0;
}
tmpdfn=0;
cnt=0;
}
void tarjan(LL u)//tarjan求强连通分量
{
vis[u]=1;
dfn[u]=low[u]=tmpdfn++;
st.push(u);
for(LL i=0;i<G[u].size();++i)
{
LL v=G[u][i];
if(vis[v]==0)
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==1)
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
LL v;
do{
v=st.top(); st.pop();
belong[v]=cnt;
++num[cnt];
vis[v]=2;
}while(v!=u);
++cnt;
}
}
int main()
{
LL T_T;
scanf("%lld",&T_T);
for(LL cas=1;cas<=T_T;++cas)
{
scanf("%lld%lld",&V,&E);
init();
for(LL i=0;i<E;++i)
{
LL u,v;
scanf("%lld%lld",&u,&v);
--u;
--v;
G[u].push_back(v);
}
for(LL i=0;i<V;++i)
if(vis[i]==0)
tarjan(i);
if(cnt==1)//已经强连通
{
printf("Case %lld: -1\n",cas);
continue;
}
for(int u=0;u<V;++u)//统计缩点之后的入度与出度
for(int i=0;i<G[u].size();++i)
{
LL v=G[u][i];
if(belong[u]!=belong[v])
{
++in[belong[v]];
++out[belong[u]];
}
}
LL the_min=INF;
for(LL i=0;i<cnt;++i)//找到满足要求的最小强连通分量
if(in[i]==0||out[i]==0)
the_min=min(the_min,num[i]);
LL other_v=V-the_min;
LL other_e=(other_v)*(other_v-1);
printf("Case %lld: %lld\n",cas,the_min*(the_min-1)+other_e+the_min*other_v-E);
}
return 0;
}
相关文章推荐
- hdu 4635 非强连通图最大边数
- HDU 4289 最小割=最大流 求去掉最少点权值使得 起末点不连通
- hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
- HDU-4635 Strongly connected 强连通,缩点
- hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
- 有向图强连通分量,练习1:hdu 3072 + hdu 4635 + poj 1236
- HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】
- HDU 1269 迷宫城堡 最大强连通图题解
- HDU 1269 迷宫城堡 最大强连通图题解
- HDU1856——最大连通分量的节点个数
- hdu 4635 Strongly connected【强连通Kosaraju+缩点染色+思维】
- 【强连通】 HDU 4635 Strongly connected
- 【HDU 2225】【数学题 构造】【求不大于sqrt(n)的分数, 分母最大为m】
- HDU 4888 神奇最大流行进列出构造矩阵
- HDU 4587 TWO NODES(割两个点的最大连通分支数)
- HDU 4635 Strongly connected 连通图
- hdu 1068 Girls and Boys(最大独立集,二分匹配)
- hdu 2276 Kiki & Little Kiki 2(矩阵构造乘法)
- http://acm.hdu.edu.cn/showproblem.php?pid=2795 更新节点构造线段数很关键,询问特殊
- hdu1131_数论_卡特兰_二叉树的构造