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 ...
随机推荐
- 19_08_26校内训练[Max]
题意 求$max_{l \leq r}{\{min{\{a_l,a_{l+1},...,a_r\}}*(r-l+1)\}}$ 思考 分治,考虑一个区间跨过某个点的贡献即可. 代码 #include&l ...
- Shell与进程
查看当前运行的进程 名称: ps 使用权限: 所有使用者 使用方式: ps [options] [--help] 说明: 显示瞬间行程 (process) 的动态 参数: ps的参数非常多, 在此仅列 ...
- Exception | java.security.NoSuchProviderException: no such provider: BC
背景 今天在用PGP key做JWT签名和验签的时候,转换报了如下错误: org.bouncycastle.openpgp.PGPException: exception on setup: java ...
- Spring注入bean的方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的se ...
- 测试用例设计:PICT的安装及使用
一.下载与安装 打开百度网页,搜索PICT,即可找到许多下载链接,点击这里,下载到桌面,点击安装.一直NEXT,安装路径保存在C盘: 二.PICT 使用 1.找到安装目录,即可看到以下内容 2.创建t ...
- css实现渐变字体和流光字体
这是段渐变文本 .text{ font-size: 30px; font-weight: bold; background-image: linear-gradient(#ed3f27, #9b099 ...
- 用pyinstaller打包时的图标问题
前言 因为昨天重新研究了下python的打包方法,今天一番准备把之前写的一个pdf合并软件重新整理一下,打包出来. 但在打包的过程中仍然遇到了一些问题,半年前一番做打包的时候也遇到了一些问题,现在来看 ...
- NR / 5G - MAC Overview
- 自定义属性的访问 - Customizing attribute access
自定义属性的访问 - Customizing attribute access 在 python 中, 下列方法可以实现类实例属性 instance.attribute 的 使用,设置,删除. obj ...
- qt creator源码全方面分析(2-5)
目录 Creating Wizards in Code 介绍 相关类 IWizardFactory的设置器和获取器 Creating Wizards in Code 介绍 如果基于模板的自定义向导提供 ...