题目链接: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 )

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = (int)5e3+;
int head[N],dfn[N],low[N],scc_no[N],s[N],du[N];
int n,m,tot,tim,scc,top;
struct node{
int to;
int nxt;
}e[N << ]; inline void add(int u,int v){
e[tot].to = v;
e[tot].nxt = head[u];
head[u] = tot++;
} void tarjan(int now,int pre){
dfn[now] = low[now] = ++tim;
s[top++] = now; //这里有情况,两点之间可能>1条路直接连接
//所以,需要处理下边的重复问题
int to,pre_cnt = ;
for(int o = head[now]; ~o; o = e[o].nxt){
to = e[o].to;
//只是一条无向边,处理一下
if(to == pre && pre_cnt == ){
pre_cnt = ;
continue;
}
if(!dfn[to]){
tarjan(to,now);
low[now] = min(low[now],low[to]);
}
else low[now] = min(low[now],dfn[to]);
} if(dfn[now] == low[now]){
++scc;
int x;
do{
x = s[--top];
scc_no[x] = scc;
}while(now != x);
}
} void rebuild(){
int to;
for(int now = ; now <= n; ++now){
for(int o = head[now]; ~o; o = e[o].nxt){
to = e[o].to;
if(scc_no[now] != scc_no[to]){
++du[scc_no[now]];
++du[scc_no[to]];
}
}
}
} void solve(){
int p = ;
// cout << scc << endl; //度数为什么是2的,因为是无向图,其实除2就是度数为1了
for(int i = ; i <= scc; ++i)
if(du[i] == ) ++p;
// cout << p << endl; printf("%d\n",(p+)/);
} int main(){ int u,v;
scanf("%d%d",&n,&m);
for(int i = ;i <= n; ++i) head[i] = -;
for(int i = ; i < m; ++i){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
tarjan(,);
rebuild();
solve(); return ;
}

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. Dynamics 365 CRM 在 Connected Field Service 中部署 IoT Central (二)- 匹配设备

    上个blog中介绍了我们怎么去部署IoT central和 connected field service做连接. 我们这次介绍怎么把IoT设备在CRM中怎么去注册. 首先我们打开devices,再选 ...

  2. 文艺平衡树(区间splay)

    文艺平衡树(luogu) Description 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列. 其中需要提供以下操作:翻转一个区间,例如原有序序列是 5\ 4\ 3\ 2\ ...

  3. JavaWeb高级编程(上)

    好久没更新了,发一篇以前记录学习的笔记. 面向读者:已经具有丰富的Java语言和Java SE平台知识的软件开发者和软件工程师. 预掌握知识: Internet.TCP.HTTP协议 HTML(5) ...

  4. php--->使用callable强制指定回调类型

    php 使用callable强制指定回调类型 如果一个方法需要接受一个回调方法作为参数,我们可以这样写 <?php function dosth($callback){ call_user_fu ...

  5. Distance Dependent Infinite Latent Feature Model 阅读笔记1

    阅读文献:Distance Dependent Infinite Latent Feature Model 作者:Samuel J.Gershman ,Peter I.Frazier ,and Dav ...

  6. Linux驱动管理

    一.驱动更新 本示例为更新网卡驱动,把新的驱动文件放到/root/目录下,然后执行下面的命令 备份已有的文件,将新的文件复制的相应位置 mv /lib/modules/`uname -r`/kerne ...

  7. AcWing 786.第k个数

    AcWing 786.第k个数 题目描述 给定一个长度为n的整数数列,以及一个整数k,请用快速选择算法求出数列的第k小的数是多少. 输入格式 第一行包含两个整数 n 和 k. 第二行包含 n 个整数( ...

  8. Boyer-Moore 算法 Leetcode169

    Boyer-Moore 算法 Leetcode169 一.题目 169. 多数元素 给定一个大小为 n 的数组,找到其中的多数元素.多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假 ...

  9. Hibernate(六)

    ================================缓存============================定义:介于应用程序和永久性数据存储源之间,可以复制数据存储源中的数据. 工作 ...

  10. 手把手实操教程!使用k3s运行轻量级VM

    前 言 k3s作为轻量级的Kubernetes发行版,运行容器是基本功能.VM的管理原本是IaaS平台的基本能力,随着Kubernetes的不断发展,VM也可以纳入其管理体系.结合Container和 ...