HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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
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
If the original graph is strongly connected, just output -1.
Sample Input
Sample Output
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<string.h>
#include<vector>
using namespace std;
typedef long long INT;
const int maxn = 100100;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn];
int tot, head[maxn];
void init(){
tot = 0;
memset(head,-1,sizeof(head));
}
void AddEdge(int _u,int _v){
edges[tot] = Edge(_v,head[_u]);
head[_u] = tot++;
}
int dfs_clock, scc_cnt;
int sccno[maxn], dfn[maxn], lowlink[maxn];
stack<int>S;
void dfs(int u){
lowlink[u] = dfn[u] = ++dfs_clock;
S.push(u);
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}else if(!sccno[v]){
lowlink[u] = min(lowlink[u],dfn[v]);
}
}
if(lowlink[u] == dfn[u]){
scc_cnt++;
for(;;){
int x = S.top();
S.pop();
sccno[x] = scc_cnt;
if(x == u){
break;
}
}
}
}
void find_scc(int n){
dfs_clock = scc_cnt = 0;
memset(sccno,0,sizeof(sccno));
memset(dfn,0,sizeof(dfn));
for(int i = 1; i <= n; i++){
if(!dfn[i]){
dfs(i);
}
}
}
int outdeg[maxn], indeg[maxn];
INT sccsz[maxn];
int main(){
int T, m, cas = 0;
INT n;
scanf("%d",&T);
while(T--){
scanf("%lld%d",&n,&m);
init();
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
}
find_scc(n);
printf("Case %d: ",++cas);
if(scc_cnt == 1){
puts("-1");continue;
}
memset(indeg,0,sizeof(indeg));
memset(outdeg,0,sizeof(outdeg));
memset(sccsz,0,sizeof(sccsz));
for(int i = 1; i <= n; i++){
sccsz[sccno[i]]++;
for(int j = head[i]; j != -1; j = edges[j].next){
int v = edges[j].to;
if(sccno[i] == sccno[v]){
continue;
}
indeg[sccno[v]]++;
outdeg[sccno[i]]++;
}
}
INT ans = 0;
for(int i = 1; i <= scc_cnt; i++){
if(indeg[i] == 0 ||outdeg[i] == 0){
ans = max( ans, (n*n - n - sccsz[i]*(n - sccsz[i]))-m );
}
}
printf("%lld\n",ans);
}
return 0;
} /*
555
7 9
1 5
1 2
5 6
6 7
7 5
6 4
2 4
4 3
3 2 */
HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】的更多相关文章
- HDU 4635 Strongly connected(强连通)经典
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 4635 Strongly connected 强连通缩点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个n个点m条边的图,问在图不是强连通图的情况下,最多可以向图中添多少条边,若图为原来 ...
- hdu 4635 Strongly connected(强连通)
考强连通缩点,算模板题吧,比赛的时候又想多了,大概是不自信吧,才开始认真搞图论,把题目想复杂了. 题意就是给你任意图,保证是simple directed graph,问最多加多少条边能使图仍然是si ...
- hdu 4635 Strongly connected 强连通
题目链接 给一个有向图, 问你最多可以加多少条边, 使得加完边后的图不是一个强连通图. 只做过加多少条边变成强连通的, 一下子就懵逼了 我们可以反过来想. 最后的图不是强连通, 那么我们一定可以将它分 ...
- 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(强连通分量缩点+数学思想)
题意:给出一个图,如果这个图一开始就不是强连通图,求出最多加多少条边使这个图还能保持非强连通图的性质. 思路:不难想到缩点转化为完全图,然后找把它变成非强连通图需要去掉多少条边,但是应该怎么处理呢…… ...
- HDU 4635 Strongly connected (Tarjan+一点数学分析)
Strongly connected Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- HDU 4635 Strongly connected (强连通分量)
题意 给定一个N个点M条边的简单图,求最多能加几条边,使得这个图仍然不是一个强连通图. 思路 2013多校第四场1004题.和官方题解思路一样,就直接贴了~ 最终添加完边的图,肯定可以分成两个部X和Y ...
- HDU 4635 - Strongly connected(2013MUTC4-1004)(强连通分量)
t这道题在我们队属于我的范畴,最终因为最后一个环节想错了,也没搞出来 题解是这么说的: 最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数尽可能的多,则X部肯 ...
随机推荐
- signalR之java client的websocket BUG处理
最近在用SignalR,服务端已经写好(老铁,没毛病,很稳),然后有坑的是我还得写App端,那就撸吧,java也不是什么很难的东西.奈何坑多(已经踩了一波android的控件bug),这次遇到了MS的 ...
- SQLServer数据库,表内存,实例名分析SQL语句
--数据库内存分析 USE master go DECLARE @insSize TABLE(dbName sysname,checkTime VARCHAR(19),dbSize VARCHAR(5 ...
- 自定义等高 Cell
1.介绍 1.1 代码自定义 cell(使用 frame) 创建一个继承自 UITableViewCell 的子类,比如 BookCell1. 在 initWithStyle:reuseIdentif ...
- Java开发环境配置(JDK+Tomcat+MyEclipsed)
前言 这个项目一开始,我只能说我把自己整的很无语,所以我只能在调整心态的基础上,重新把思路缕了一遍,好了,接下来就说java运行环境以及发布运行方法还有SSH环境配置. 内容 本次配置用到的安装包: ...
- soj 131 找题
soj 131 找题 给出两个长度为n,都含k个1的字符串A,B.现在令\(a_1,a_2,\dots,a_k\)是A中1的下标,\(b_1,b_2,\dots,b_k\)是B中1的下表,然后将a,b ...
- sap abap 流水号设置
1.TCODE:SNRO,进入如图所示界面 2. 短文本和长文本用来说明这个编号范围对象,输入任意描述即可. 子对象数据元素我们这里不填.这里需要说明一下,所谓子对象,多数指一个组织结构,比如公司代码 ...
- flink学习笔记-快速生成Flink项目
说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...
- pycharm加开头注释
选择 File and Code Templates -> Files -> Python Script #!/usr/bin/env python # encoding: utf-8 ' ...
- 「洛谷5017」「NOIP2018」摆渡车【DP,经典好题】
前言 在考场被这个题搞自闭了,那个时候自己是真的太菜了.qwq 现在水平稍微高了一点,就过来切一下这一道\(DP\)经典好题. 附加一个题目链接:[洛谷] 正文 虽然题目非常的简短,但是解法有很多. ...
- p标签text-align:justify以及CSS文字两端对齐
p标签样式添加text-align:justify; 那么就会左右对齐. 使用前: 使用后: CSS文字两端对齐 使用前: 代码: <style> div{ width:100px; he ...