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

Strongly connected

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3821    Accepted Submission(s): 1510

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
题目大意:给一个简单有向图,在图中加边,问在保证图不强连通的情况下最多能加多少边。
题目分析:仔细考虑可知如果不连通,至少有两个强连通分量。而一个强连通分量最多有 N*(N-1)条边,所以这道题的最后答案应该是成为两个强连通分量。
总边数为 sum=(n1-1)*n1+(n2-1)*n2+(n2*n1),(其中n1+n2=n)由于已经存在了m条边,所以最后答案是ans=sum-m;
所以对于上述sum 的最大值求最大值可知应该使其中一个强连通分量的顶点数n1尽可能小,前提是这两个强连通分量之间没有强连通,即这两个强连通分量的入度或者出度为0。则在Tarjan求出各个强连通分量之后,再缩点统计各个强连通分量的入度和出度,最后找出顶点数最少的入度或者出度为0的强连通分量代入即可。
【PS:一定要记得使用边数m时,不要用while(m--),可能会因为下面需要使用m而无限wa。】
 #include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn=;//边的最大值
const int maxn1=;//顶点最大值
struct edge{
int from;
int to;
int next;
}EDGE[maxn];
vector<int>vc[maxn1];
int head[maxn1],dfn[maxn1],vis[maxn1],low[maxn1],col[maxn1],out[maxn1],in[maxn1],en[maxn1],stk[maxn1];//各个变量的意义可参照上篇博客
int edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
int n,m;
void add(int x,int y)
{
EDGE[edge_cnt].from=x;
EDGE[edge_cnt].to=y;
EDGE[edge_cnt].next=head[x];
head[x]=edge_cnt++;
}
void Tarjan(int u)
{
low[u]=dfn[u]=++tot1;//注意tot1的初值必须是1【因为dfn必须为正数】,所以这里使用++tot1而不用tot1++;
vis[u]=;
stk[++tot2]=u;
for(int i = head[u]; i != - ; i = EDGE[i].next)
{
if(!dfn[EDGE[i].to]){
Tarjan(EDGE[i].to);
low[u]=min(low[u],low[EDGE[i].to]);
}
else if(vis[EDGE[i].to]){
low[u]=min(low[u],low[EDGE[i].to]);
}
}
if(low[u]==dfn[u]){
int xx;
scc_cnt++;//注意scc_cnt也是从1开始的,因为要染色,区别于为染色的0
do{
xx=stk[tot2--];
vc[scc_cnt].push_back(xx);
col[xx]=scc_cnt;
vis[xx]=;
}while(xx!=u);
}
}
void INIT()
{
for(int i = ; i < maxn1 ; i++)
vc[i].clear();
edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
memset(head,-,sizeof(head));
memset(stk,,sizeof(stk));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(col,,sizeof(col));
}
void suodian()//缩点
{
for(int i = ; i < edge_cnt ; i++)
{
if(col[EDGE[i].from]!=col[EDGE[i].to])
{
in[col[EDGE[i].to]]++;//缩点
out[col[EDGE[i].from]]++;
}
}
}
int main()
{
int t;
scanf("%d",&t);
int case1=;
while(t--)
{
INIT();
scanf("%d%d",&n,&m);
int M=m;
while(M--)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
for(int i = ; i <= n ; i++)
{
if(!dfn[i]){
Tarjan(i);
}
} printf("Case %d: ",case1++);
if(scc_cnt==)
{
printf("-1\n");
}
else
{
suodian();
int minn=-;
for(int i = ; i <= scc_cnt ; i++)
{
if(in[i]==||out[i]==)
{
int asd=vc[i].size();
if(minn==-||minn>asd)
{
minn=asd;
}
}
}
cout << (n-minn)*(n-minn-)+minn*(minn-)+minn*(n-minn)-m<<endl;
}
}
return ;
}
 
 

【HDOJ4635】【Tarjan缩点+思维】【经典】的更多相关文章

  1. POJ1236:Network of Schools (思维+Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24880   Accepted: 99 ...

  2. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  3. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  4. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  5. 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率

    2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1638  Solved: 433[Submit][Statu ...

  6. 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP

    1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 318[Submit][Stat ...

  7. 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit] ...

  8. BZOJ 1051 受欢迎的牛(Tarjan缩点)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4573  Solved: 2428 [Submit][S ...

  9. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

随机推荐

  1. webpack分离打包css和less

    github仓库:https://github.com/llcMite/webpack.git 为什么要分离打包?       答:刚开始学webpack的时候就很郁闷,明明没几个文件,打包出来体积特 ...

  2. linux:ssh远程调用tomcat脚本时候出错

    我们都知道,使用ssh在另一台机子执行一个ssh文件的语句是酱紫的 ssh root@1.9.7.56 "chmod 777 /opt/script/tomcatStop.sh ; sh / ...

  3. maven项目配置findbugs插件 使用git钩子控制代码的提交

    maven项目配置findbugs插件对代码进行静态检测 当发现代码有bug时,就不让用户commit代码到远程仓库里 没有bug时才可以commit到远程仓库中 (1)新建maven项目 ,配置fi ...

  4. OPENWRT安装配置指南之 17.01.4 LEDE

    简介 这个东西,需要看简介的就不要看下去了. 下面已刚刷进去,路由IP地址为192.168.1.1为例开始配置. 浏览器访问192.168.1.1,无密码. 一:配置上网 不管你是什么方式上网,请根据 ...

  5. PC/FORTH 编辑程序

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  6. core net 实现post 跟get

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...

  7. Java 利用poi生成excel表格

    所需jar包,如下所示 写一个excel工具类 ExcelUtils .java import java.lang.reflect.Field; import java.util.Iterator; ...

  8. ASP.NET使用文件上传控件上传图片

    ASPX代码 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default. ...

  9. Alpha冲刺1

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9948330.html 作业博客:https://edu.cnblogs.com/campus/ ...

  10. NSHashTable NSPointerArray

    NSHashTable和NSMapTable能够对持有的对象做strong或weak存储,弱持有weak引用对象,当weak对象释放后会自动从表中移除     http://blog.csdn.net ...