题目链接:https://vjudge.net/article/371?tdsourcetag=s_pcqq_aiomsg

题目:给定一个连通图,题目说,任意两个点至少有一条路线可以相互到达,

为保证任意两点有完全不同的路线(点可以相同,边不能相同)可以相互到达至少需要加几条边。

思路:tarjan缩点,之后重构图,找出度数为1的scc个数scc_cnt,这些点相互连接,答案可以得出是 (scc_cnt+1)/2。

两组样例:

n = 5 ,m = 4  |  (1 2)  (1 3) )(1 4) (1 5)

n = 8, m = 9  | (1 2) (1 4) (2 3) (3 4) (4 5) (5 6) (6 7) (5 8) (7 8 )

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. const int N = (int)5e3+;
  7. int head[N],dfn[N],low[N],scc_no[N],s[N],du[N];
  8. int n,m,tot,tim,scc,top;
  9. struct node{
  10. int to;
  11. int nxt;
  12. }e[N << ];
  13.  
  14. inline void add(int u,int v){
  15. e[tot].to = v;
  16. e[tot].nxt = head[u];
  17. head[u] = tot++;
  18. }
  19.  
  20. void tarjan(int now,int pre){
  21. dfn[now] = low[now] = ++tim;
  22. s[top++] = now;
  23.  
  24. //这里有情况,两点之间可能>1条路直接连接
  25. //所以,需要处理下边的重复问题
  26. int to,pre_cnt = ;
  27. for(int o = head[now]; ~o; o = e[o].nxt){
  28. to = e[o].to;
  29. //只是一条无向边,处理一下
  30. if(to == pre && pre_cnt == ){
  31. pre_cnt = ;
  32. continue;
  33. }
  34. if(!dfn[to]){
  35. tarjan(to,now);
  36. low[now] = min(low[now],low[to]);
  37. }
  38. else low[now] = min(low[now],dfn[to]);
  39. }
  40.  
  41. if(dfn[now] == low[now]){
  42. ++scc;
  43. int x;
  44. do{
  45. x = s[--top];
  46. scc_no[x] = scc;
  47. }while(now != x);
  48. }
  49. }
  50.  
  51. void rebuild(){
  52. int to;
  53. for(int now = ; now <= n; ++now){
  54. for(int o = head[now]; ~o; o = e[o].nxt){
  55. to = e[o].to;
  56. if(scc_no[now] != scc_no[to]){
  57. ++du[scc_no[now]];
  58. ++du[scc_no[to]];
  59. }
  60. }
  61. }
  62. }
  63.  
  64. void solve(){
  65. int p = ;
  66. // cout << scc << endl;
  67.  
  68. //度数为什么是2的,因为是无向图,其实除2就是度数为1了
  69. for(int i = ; i <= scc; ++i)
  70. if(du[i] == ) ++p;
  71. // cout << p << endl;
  72.  
  73. printf("%d\n",(p+)/);
  74. }
  75.  
  76. int main(){
  77.  
  78. int u,v;
  79. scanf("%d%d",&n,&m);
  80. for(int i = ;i <= n; ++i) head[i] = -;
  81. for(int i = ; i < m; ++i){
  82. scanf("%d%d",&u,&v);
  83. add(u,v); add(v,u);
  84. }
  85. tarjan(,);
  86. rebuild();
  87. solve();
  88.  
  89. return ;
  90. }

kuangbin专题 专题九 连通图 POJ 3177 Redundant Paths的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  3. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  4. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  5. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  6. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  7. POJ - 3177 Redundant Paths(边双连通分支)(模板)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...

  8. poj 3177 Redundant Paths

    题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...

  9. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

随机推荐

  1. hadoop_2.6.5集群安装

    安装hadoop2.6.5集群: 1.规划设计: JacK6:NameNode,jobtracker JacK7:secondnode,datenode,tasktracker JacK8:datan ...

  2. 利用geojson实现模型轨迹运动

    直接上代码 var viewer = new Cesium.Viewer('cesiumContainer'); //Set the random number seed for consistent ...

  3. Jenkins | 搭建你第一个Jenkins应用

    搭建你第一个Jenkins应用 1.准备工作 第一次使用 Jenkins,您需要: 机器要求: 256 MB 内存,建议大于 512 MB 10 GB 的硬盘空间(用于 Jenkins 和 Docke ...

  4. python接口自动化测试 - 数据驱动DDT模块的简单使用

    DDT简单介绍 名称:Data-Driven Tests,数据驱动测试 作用:由外部数据集合来驱动测试用例的执行 核心的思想:数据和测试代码分离 应用场景:一组外部数据来执行相同的操作 优点:当测试数 ...

  5. apace访问403错误的解决方法汇总

    作为一个努力学习的实习生,遇到问题还是靠记录才能更好的学习. 首先附上故障图 翻译过来就是啥呢? 于是天真的我去百度了一下大神们的解决方法,目录没权限嘛,来个777就完事了.一开始还觉得挺合乎情理的, ...

  6. Fedora 安装及配置

    引言 最近学习课程要用到Linux,之前装的Ubuntu双系统被我删掉了(因为后来发现那个WSL,win子系统还挺好用的),所以上午用虚拟机再装了一下老师给的Ubuntu16,也不知道怎么回事特别卡, ...

  7. 非常NB的一款快捷启动软件--Merry

    Merry 被设计为了能将日常重复性操作简化为一个快捷键或者命令.Merry 采用完全开放的体系, 可以使用 Lua 或者外部程序来扩展 Merry 的功能. 另附一个自己扩展的LUA脚本: --启动 ...

  8. js小练习---实现红绿灯

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. java设计模式5——适配器模式

    java设计模式5--适配器模式 1.结构型模式介绍 1.1.作用 从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题. 分类: 适配器模式 代理模式 桥接模式 装饰模式 组合模式 ...

  10. Java并发读书笔记:Lock与ReentrantLock

    Lock位于java.util.concurrent.locks包下,是一种线程同步机制,就像synchronized块一样.但是,Lock比synchronized块更灵活.更复杂. 话不多说,我们 ...