整理的算法模板合集: ACM模板


HDU 4635 Strongly connected

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.

给定一个有向图,求最大可以增加多少条边使得这个图仍然不是强连通图。

图片来源

  • 最大可以增加多少条边使得这个图仍然不是强连通图

结论:n∗(n−1)−m−min∗(n−minv)n * (n - 1) - m - min * (n - minv)n∗(n−1)−m−min∗(n−minv)(其中n为点数、m为边数、minv为缩点以后的点中入度和出度至少有一个为0的包含节点个数最少的点)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;const int N = 500007, M = 5000007, INF = 0x3f3f3f3f;int n, m;
int head[N], ver[M], nex[M], tot;
int dfn[N], low[N], ind;
int stk[N], top;
int scc_id[N];
int scc_cnt;
bool ins[N];
int scc_num[N];
vector<int>scc[N];
int t;void add(int x, int y){ver[tot] = y;nex[tot] = head[x];head[x] = tot ++ ;
}void tarjan(int x)
{dfn[x] = low[x] = ++ ind;stk[++ top] = x, ins[x] = true;for(int i = head[x]; ~i ; i = nex[i]){int y = ver[i];if(!dfn[y]){tarjan(y);low[x] = min(low[x], low[y]);}else if(ins[y])low[x] = min(low[x], dfn[y]);}if(dfn[x] == low[x]){int y;scc_cnt ++;do{y = stk[top -- ];ins[y] = false;scc_id[y] = scc_cnt;scc_num[scc_cnt] ++ ;scc[scc_cnt].push_back(y);}while(x != y);}
}int in[N], out[N];
int ans;
int cnt;
int main()
{scanf("%d", &t);while(t -- ){cnt ++ ;tot = 0, ind = 0, scc_cnt = 0;memset(dfn, 0, sizeof dfn);memset(low, 0, sizeof low);memset(head, -1, sizeof head);memset(in, 0, sizeof in);memset(out, 0, sizeof out);memset(scc_id, 0,sizeof scc_id);memset(scc_num, 0, sizeof scc_num);scanf("%d%d", &n, &m);for(int i = 1; i <= m; ++ i){int x, y;scanf("%d%d", &x, &y);add(x, y);}for(int i = 1; i <= n; ++ i)if(!dfn[i])tarjan(i);for(int i = 1; i <= n; ++ i){for(int j = head[i]; ~j; j = nex[j]){int k = ver[j];if(scc_id[i] != scc_id[k]){out[scc_id[i]] ++ ;in[scc_id[k]] ++ ;}}}int minv = INF;for(int i = 1; i <= scc_cnt; ++ i){if(!in[i] || !out[i]){minv = min(minv, scc_num[i]);//找到包含点最少并且出度和入度至少有一个为0的点}}if(scc_cnt == 1){printf("Case %d: -1\n", cnt);}else {printf("Case %d: %lld\n", cnt, 1ll * n * (n - 1) - m - minv * (1ll * n - minv));}}return 0;
}

HDU 4635 Strongly connected(缩点、最多可加边数使得仍然非强连通)相关推荐

  1. HDU - 4635 Strongly connected(强连通缩点+数学+思维)

    题目链接:点击查看 题目大意:给出一个由n个点和m条边构成的无向图,现在问最多能添加几条边,能使得原图仍然不是强连通图,若原图初始时就是强连通图,直接输出-1 题目分析:首先对于原图来说,肯定会有一些 ...

  2. HDU 4635 Strongly connected

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4635 解题思路: 题目大意是你能最多能添加多少边,使的这个图不是强连通图.其临界条件是差一条边成强连通图 ...

  3. 强连通分量(strongly connected components)

    强连通分量(strongly connected components)    徐不可说        2018/8/4                                        ...

  4. 【HDU - 4635】Strongly connected(缩点,新图性质,建图,Tarjan求强连通分量)

    题干: Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the ...

  5. Strongly connected HDU - 4635(tarjan+强连通分量)

    题意: 给一个简单有向图,让你加最多的边,使他还是一个简单有向图. 题目: Give a simple directed graph with N nodes and M edges. Please ...

  6. 【CF913F】Strongly Connected Tournament 概率神题

    [CF913F]Strongly Connected Tournament 题意:有n个人进行如下锦标赛: 1.所有人都和所有其他的人进行一场比赛,其中标号为i的人打赢标号为j的人(i<j)的概 ...

  7. 强连通分量(SCC, Strongly Connected Components)

    强连通分量(SCC, Strongly Connected Component) 强连通分量的概念 强连通分量的应用 强连通分量的算法--Tarjan算法 强连通分量的概念 在有向图中,任意两个顶点 ...

  8. Tarjan's strongly connected components algorithm的一些想法

    Tarjan的极大强连通子图(strongly connected components,SCC)算法基于深度优先遍历(DFS)实现.本文就尝试从深度优先遍历的角度思考一下Tarjan的方法是如何找出 ...

  9. JavaScript实现strongly Connected Components 强连通分量算法(附完整源码)

    JavaScript实现strongly Connected Components 强连通分量算法(附完整源码) Comparator.js完整源代码 LinkedListNode.js完整源代码 L ...

最新文章

  1. Go 学习笔记(22)— 并发(01)[进程、线程、协程、并发和并行、goroutine 启动、goroutine 特点,runtime 包函数]
  2. ubuntu 命令整合1
  3. 你需要知道的12个Git高级命令
  4. 干货,别再浪费时间到处找了,各大面试题和答案都在这里
  5. 探索ASP.NET Core中的IStartupFilter
  6. 服务器性能瓶颈分析方法
  7. matlab 检测gpu,康奈尔大学使用MATLAB进行GPU性能测试
  8. 【kafka】 windows平台搭建及使用
  9. 如何把局域网内不同数据库的两个表的数据进行传输?
  10. 软件开发需要学好数学吗?
  11. 两种解决Qt5显示中文乱码的方法(使用QStringLiteral和#pragma execution_character_set(utf-8)两种方法)
  12. ArcGIS API for Silverlight 使用GeometryService进行河流网格划分(三)
  13. php获取当前文件夹下所有图片大小,PHP获取文件夹大小函数用法实例
  14. LCD12864驱动显示程序
  15. java kindeditor 上传图片_使用Kindeditor上传图片
  16. 播放器显示服务器失败是什么意思,播放器没有办法播放
  17. 新元宇宙每周连载《地球人奇游天球记》第十八回冥王遇鬼
  18. Sping AOP 源码解析(一、动态 AOP 自定义标签 aop:aspectj-autoproxy)
  19. kubernetes资源控制器【一】- ReplicaSet控制器
  20. java.sql.SQLException: Incorrect Integer value:‘****‘ for column ‘id‘ at row 1 解决方案

热门文章

  1. 基于GAN的自动驾驶汽车语义分割
  2. 基于OpenCV创建视频会议虚拟背景
  3. Primary VLAN
  4. redis3.2集群搭建
  5. 侧边栏qq客服对话显示
  6. 使用Java HttpURLConnection抓取网页内容(一)限制返回的网页大小
  7. 【leetcode】148. Sort List
  8. Hobby开挂!加速web编码
  9. c++成员函数的调用
  10. C#文件和文件夹输入输出流代码