题意:在一张有向图中最少添加几条边,能使它强连通。

思路:自己想的时候差了一点。。最后还是看了一眼白书秒过了。首先第一步很简单当然是缩点。变成DAG接下来问题就是DAG上最少添几条边使他强连通。其实只要求出max(出度为零的节点数,入度为零的结点数)。注意当原图已经强联通时特判一下。

代码如下:

 1 /**************************************************
 2  * Author     : xiaohao Z
 3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
 4  * Last modified : 2014-01-31 22:26
 5  * Filename     : uva_12167.cpp
 6  * Description     :
 7  * ************************************************/
 8
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <cmath>
14 #include <algorithm>
15 #include <queue>
16 #include <stack>
17 #include <vector>
18 #include <set>
19 #include <map>
20 #define MP(a, b) make_pair(a, b)
21 #define PB(a) push_back(a)
22
23 using namespace std;
24 typedef long long ll;
25 typedef pair<int, int> pii;
26 typedef pair<unsigned int,unsigned int> puu;
27 typedef pair<int, double> pid;
28 typedef pair<ll, int> pli;
29 typedef pair<int, ll> pil;
30
31 const int INF = 0x3f3f3f3f;
32 const double eps = 1E-6;
33 const int LEN = 100000+10;
34 vector<int> Map[LEN];
35 int n, m, dclock, scc_cnt, dfn[LEN], low[LEN], sccn[LEN];
36 stack<int> s;
37
38 void sccinit(){
39     for(int i=0; i<LEN; i++) Map[i].clear();
40     while(!s.empty()) s.pop();
41     memset(dfn, 0, sizeof dfn);
42     memset(sccn, 0, sizeof sccn);
43     dclock = scc_cnt = 0;
44 }
45
46 void dfs(int u){
47     low[u] = dfn[u] = ++dclock;
48     s.push(u);
49     for(int i=0; i<Map[u].size(); i++){
50         int v = Map[u][i];
51         if(!dfn[v]){
52             dfs(v);
53             low[u] = min(low[u], low[v]);
54         }else if(!sccn[v]) low[u] = min(low[u], dfn[v]);
55     }
56     if(dfn[u] == low[u]){
57          scc_cnt ++;
58          while(1){
59              int x = s.top();s.pop();
60              sccn[x] = scc_cnt;
61              if(u == x) break;
62          }
63     }
64 }
65
66 int main()
67 {
68 //    freopen("in.txt", "r", stdin);
69
70     int T, a, b;
71     scanf("%d", &T);
72     while(T--){
73         sccinit();
74         scanf("%d%d", &n, &m);
75         for(int i=0; i<m; i++){
76             scanf("%d%d", &a, &b);
77             Map[a].PB(b);
78         }
79         int od[LEN] = {0}, id[LEN] = {0};
80         for(int i=1; i<=n; i++)if(!dfn[i])dfs(i);
81         for(int i=1; i<=n; i++){
82              for(int j=0; j<Map[i].size(); j++){
83                 if(sccn[i] == sccn[Map[i][j]]) continue;
84                 od[sccn[i]] = 1;
85                 id[sccn[Map[i][j]]] = 1;
86              }
87         }
88         a = b = 0;
89         for(int i=1; i<=scc_cnt; i++) {
90             if(!od[i]) a++;
91             if(!id[i]) b++;
92         }
93         if(scc_cnt!=1)printf("%d\n", max(a, b));
94         else printf("0\n");
95     }
96     return 0;
97 }

View Code

转载于:https://www.cnblogs.com/shu-xiaohao/p/3536904.html

uva 12167(强连通分支)相关推荐

  1. UVa 12167 HDU 2767 强连通分量 Proving Equivalences

    题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. 1 ...

  2. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  3. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  5. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  6. UVa 11174 - Stand in a Line

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. UVa 10112 - Myacm Triangles

    UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...

  8. UVa 10180 - Rope Crisis in Ropeland!

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=41&pa ...

  9. Uva 10074【递推dp】

    UVa 10074 题意:求01矩阵的最大子0矩阵. http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2 这里说的很清楚.先求Larg ...

最新文章

  1. 世界人工智能大会圆桌实录:AI与产业融合创新的挑战与机遇
  2. PostgreSQL中表名、字段名大小写问题
  3. python excel数据分析实战_一次完整的数据分析实战!仅用4步,效率吊打Excel和Python...
  4. 【CodeForces - 570A】Elections(模拟,水题)
  5. 全连接条件随机场_深圳机场在国内机场中率先推出全流程“行李门到门”服务...
  6. 图片验证码+输入立即校验
  7. Nand Flash驱动程序分析
  8. vue使用高德地图API,定位,搜索,拖拽选址
  9. 计算机应用基础第四版答案周南岳,计算机应用基础周南岳答案.docx
  10. baidumap vue 判断范围_vue-baidu-map 行政区域划分
  11. 过滤微信特殊字符名称
  12. Apple watch ,小米微信通知
  13. 全球某工商云战役自动打卡系统
  14. sparse_to_dense()和sparse_tensor_to_dense()的用法
  15. Kesci“魔镜杯”风控算法大赛铜奖解决方案
  16. Searchcode: 源代码搜索利器
  17. Eslint +Vue配置
  18. ORA-00020: maximum number of processes (xxxx) exceeded 报错解决方法
  19. (Adventure项目)自行车业务数据分析报告(二)
  20. 基于Windows Embedded部署物流配货系统加快物流速度

热门文章

  1. mysql数据库语句q_mysql数据库命令大全,mysql基本命令大全
  2. mysql修改时间精度_Mysql时间精度丢失问题
  3. Springboot实战:3种 Springboot 全局时间格式化方式
  4. iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
  5. TCP和UDP 粘包 消息保护边界
  6. plsql连接oracle无响应,求教 pl/sql连接本机数据库是未响应问题
  7. mysql通过视图查看_MySQL教程92-MySQL查看视图
  8. 【PHPExcel】生成Excel2007文件并下载
  9. cpu飙升 死循环_记一次CPU飙升BUG
  10. 5教程 watchout_Unit 5单元复习学案设计