kuangbin专题 专题九 连通图 POJ 3177 Redundant Paths
题目链接: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的更多相关文章
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction
这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ - 3177 Redundant Paths(边双连通分支)(模板)
1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...
- poj 3177 Redundant Paths
题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
随机推荐
- ios启动流程
1.创建UIApplication (1.打开网页,发短信,打电话 . 2.设置应用程序提醒数字 . 3.设置联网状态 . 4.设置状态栏) 2.创建AppDelegate代理对象,并且成为UIApp ...
- String字符串性能优化的几种方案
String字符串是系统里最常用的类型之一,在系统中占据了很大的内存,因此,高效地使用字符串,对系统的性能有较好的提升. 针对字符串的优化,我在工作与学习过程总结了以下三种方案作分享: 一.优化构建的 ...
- WSL初始配置+图形界面
安装WSL 换源 参考另一篇文章https://www.cnblogs.com/bosslv/p/11006680.html 修改$PATH,默认会把windows的PATH也加入WSL中,不需要的话 ...
- Leetcode 题目整理
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- SystemVerilog搭建APB_I2C IP 层次化验证平台
一.前言 近期疫情严重,身为社畜的我只能在家中继续钻研技术了.之前写过一篇关于搭建FIFO验证平台的博文,利用SV的OOP特性对FIFO进行初步验证,但有很多不足之处,比如结构不够规范.验证组件类不独 ...
- Gloang Swagger
功能 自动化生产接口文档 安装 # 安装swaggo get -u github.com/swaggo/swag/cmd/swag # 安装 gin-swagger go get -u github. ...
- 五、Django学习之基于对象的跨表查询
五.Django学习之基于对象的跨表查询 正向与反向查询 关键在于ForeignKey字段写的位置.例如下面这段代码, 关系属性(字段)写在哪个类(表)里面,从当前类(表)的数据去查询它关联类(表)的 ...
- ceph问题
问题1: [root@admin-node my-cluster]# ceph -s cluster 4ca35731-2ccf-47fb-9f06-41fae858626d health HEALT ...
- Java中的代码点与代码单元
在Java中,什么是代码点与代码单元? 代码点(Code Point):在 Unicode 代码空间中的一个值,取值 U+0000 至 U+10FFFF,代表一个字符. 其中U+0000到U+FFFF ...
- HDU_1166_树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=1166 树状数组入门题. #include<iostream> #include<cstring ...