HDU 4635 Strongly connected(强连通)经典
Strongly connected
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1828 Accepted Submission(s): 752
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 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 1: -1
Case 2: 1
Case 3: 15
假设一開始就是一个强连通图。则输出-1。
由于假设不减入度为0或出度为0相关的边,那么该点本身包括有入边和出边。加的边永远都是强连通图。所以仅仅能去掉与入度为0或出度为0点的相关边,仅仅减掉一个方向的边,要么全是(n-minnum)点数到minnum点数的入边,那么是minnum点数到(n-minnum)点数的出边。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll __int64
const int N = 100005;
struct EDG{
int to,next;
}edg[N];
int eid,head[N];
int low[N],dfn[N],vist[N],num[N],id[N],deep,stack1[N],tn,top;
int in[N],out[N]; void init(){
eid=tn=top=deep=0;
memset(head,-1,sizeof(head));
memset(vist,0,sizeof(vist));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(num,0,sizeof(num));
}
void addEdg(int u,int v){
edg[eid].to=v; edg[eid].next=head[u]; head[u]=eid++;
}
void tarjer(int u){
stack1[++top]=u;
vist[u]=1;
deep++;
low[u]=dfn[u]=deep;
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(vist[v]==0){
vist[v]=1;
tarjer(v);
low[u]=min(low[u],low[v]);
}
else if(vist[v]==1)
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
tn++;
do{
vist[stack1[top]]=2;
num[tn]++;
id[stack1[top]]=tn;
}while(stack1[top--]!=u); }
}
ll solve(int n,int m){
ll ans=n*(n-1)-m;
int minnum=N;
for(int i=1; i<=n; i++)
if(vist[i]==0)
tarjer(i);
if(tn==1) return -1;
for(int u=1; u<=n; u++)
for(int i=head[u]; i!=-1; i=edg[i].next){
int v=edg[i].to;
if(id[u]!=id[v])
in[id[v]]++,out[id[u]]++;
}
for(int i=1; i<=tn; i++)
if(in[i]==0||out[i]==0){
minnum=min(minnum,num[i]);
}
ans-=minnum*(n-minnum); return ans;
}
int main(){
int T,n,m,c=0,a,b;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=1; i<=m; i++)
{
scanf("%d%d",&a,&b);
addEdg(a,b);
}
printf("Case %d: %I64d\n",++c,solve(n,m));
}
}
HDU 4635 Strongly connected(强连通)经典的更多相关文章
- hdu 4635 Strongly connected 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- HDU 4635 Strongly connected (强连通分量)
题意 给定一个N个点M条边的简单图,求最多能加几条边,使得这个图仍然不是一个强连通图. 思路 2013多校第四场1004题.和官方题解思路一样,就直接贴了~ 最终添加完边的图,肯定可以分成两个部X和Y ...
- hdu 4635 Strongly connected 强连通
题目链接 给一个有向图, 问你最多可以加多少条边, 使得加完边后的图不是一个强连通图. 只做过加多少条边变成强连通的, 一下子就懵逼了 我们可以反过来想. 最后的图不是强连通, 那么我们一定可以将它分 ...
- HDU 4635 Strongly connected (强连通分量+缩点)
<题目链接> 题目大意: 给你一张有向图,问在保证该图不能成为强连通图的条件下,最多能够添加几条有向边. 解题分析: 我们从反面思考,在该图是一张有向完全图的情况下,最少删去几条边能够使其 ...
- HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】
Strongly connected Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 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 (Tarjan+一点数学分析)
Strongly connected Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- hdu 4635 Strongly connected(强连通)
考强连通缩点,算模板题吧,比赛的时候又想多了,大概是不自信吧,才开始认真搞图论,把题目想复杂了. 题意就是给你任意图,保证是simple directed graph,问最多加多少条边能使图仍然是si ...
- HDU 4635 Strongly connected(强连通分量,变形)
题意:给出一个有向图(不一定连通),问最多可添加多少条边而该图仍然没有强连通. 思路: 强连通分量必须先求出,每个强连通分量包含有几个点也需要知道,每个点只会属于1个强连通分量. 在使图不强连通的前提 ...
随机推荐
- 常见python快捷键
http://www.cnblogs.com/toutou/p/4778818.html Ctrl+/注释(取消注释)选择的行 Shift + Enter开始新行 Ctrl + Enter智能换行 T ...
- cobbler 安装centos7.3时GPT问题(五)
磁盘分区表MBR和GPT介绍: MBR(Master Boot Record):最大只支持2 TB的盘,最多只支持4个主分区,信息只存储在一个区域. GPT(GUID partition table) ...
- javascript学习笔记 - 引用类型 基本包装类型
六 基本包装类型 Boolean,Number,String 这三个引用类型亦称为基本包装类型,与基本的数据类型boolean,number,string相关联.为了方便操作这些基本类型的数据. 引用 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 【Luogu】P3389高斯消元模板(矩阵高斯消元)
题目链接 高斯消元其实是个大模拟qwq 所以就着代码食用 首先我们读入 ;i<=n;++i) ;j<=n+;++j) scanf("%lf",&s[i][j]) ...
- 浅谈Android保护技术__代码混淆
浅谈Android保护技术__代码混淆 代码混淆 代码混淆(Obfuscated code)亦称花指令,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理解的形式的行为.将代码中的各种元 ...
- mybatis学习(九)——动态sql
MyBatis 的强大特性之一便是它的动态 SQL.可以根据不同条件拼接 SQL 语句. 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似.主要由以下几种元素. if wh ...
- [转] Makefile 基础 (10) —— Makefile 后序
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作(转)
马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...
- idea部署项目到远程tomcat
之前做项目,一直都是把本地的源码上传到svn,服务器是通过ant或者maven脚本来编译的生成项目的.每次都要单独登录接服务器进行项目的部署和发布,感觉特别繁琐.(特别是在有几套服务器的情况下,简直就 ...