[USACO06JAN]Redundant Paths
OJ题号:
洛谷2860、POJ3177
题目大意:
给定一个无向图,试添加最少的边使得原图中没有桥。
思路:
Tarjan缩点,然后统计度为$1$的连通分量的个数(找出原图中所有的桥)。
考虑给它们每两个连通分量连一条边,这样一次性可以解决两个。
如果最后还有多的,就专门给它随便连一条边。
设度为$1$的连通分量的个数为$c$,则答案为$\lfloor{\frac{c+1}{2}}\rfloor$。
因为是无向图,所以用一般的Tarjan会来回走同一条边,这样就会把桥的两岸缩在同一个点中,不合题意。
考虑Tarjan中记录当前结点的父亲结点,往下递归时判断是否与其相等。这样看起来是正确的,但是交到洛谷上会WA一个点。
因为原图中不一定保证相同的两个点之间只有一条边,因此如果当某两点间同时存在两条边时,不能算桥。但是按照上面的算法不会将这两点缩在一起。
考虑记录每条边的编号,每次递归判断枚举到的出边是否与入边编号相等即可。
#include<stack>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int V=;
struct Edge {
int to,id;
};
std::vector<Edge> e[V];
inline void add_edge(const int u,const int v,const int id) {
e[u].push_back((Edge){v,id});
}
int dfn[V]={},low[V]={},scc[V]={},cnt=,id=;
bool ins[V]={};
std::stack<int> s;
void Tarjan(const int x,const int eid) {
dfn[x]=low[x]=++cnt;
s.push(x);
ins[x]=true;
for(unsigned i=;i<e[x].size();i++) {
if(e[x][i].id==eid) continue;
int &y=e[x][i].to;
if(!dfn[y]) {
Tarjan(y,e[x][i].id);
low[x]=std::min(low[x],low[y]);
}
else if(ins[y]) {
low[x]=std::min(low[x],dfn[y]);
}
}
if(low[x]==dfn[x]) {
int y;
id++;
do {
y=s.top();
s.pop();
ins[y]=false;
scc[y]=id;
} while(y!=x);
}
}
int deg[V]={};
int main() {
int n=getint();
for(int m=getint();m;m--) {
int u=getint(),v=getint();
add_edge(u,v,m);
add_edge(v,u,m);
}
for(int i=;i<=n;i++) {
if(!dfn[i]) Tarjan(i,);
}
for(int x=;x<=n;x++) {
for(unsigned i=;i<e[x].size();i++) {
int &y=e[x][i].to;
if(scc[x]!=scc[y]) deg[scc[x]]++,deg[scc[y]]++;
}
}
int cnt=;
for(int i=;i<=id;i++) {
if(deg[i]==) cnt++;
}
printf("%d\n",(cnt+)>>);
return ;
}
[USACO06JAN]Redundant Paths的更多相关文章
- 洛谷P2860 [USACO06JAN]Redundant Paths G (tarjan,边双缩点)
本题的大意就是加最少的边使得图成为边双. 多举例子,画图分析可得:最终答案就是叶子节点(度数为1的点)的个数加1在除以2. 那么我们的目的就转化为找叶子节点: 首先通过tarjan找到割边,再dfs将 ...
- Luogu2860 [USACO06JAN]冗余路径Redundant Paths
Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...
- 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告
P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...
- 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths
P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- [双连通分量] POJ 3177 Redundant Paths
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13712 Accepted: 5821 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- [POJ3177]Redundant Paths(双联通)
在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Tota ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
随机推荐
- Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally
hint: Updates were rejected because the remote contains work that you dohint: not have locally. This ...
- Javascript加速运动与减速运动
加速运动,即一个物体运动时速度越来越快:减速运动,即一个物体运动时速度越来越慢.现在用Javascript来模拟这两个效果,原理就是用setInterval或setTimeout动态改变一个元素与另外 ...
- the error about “no such file or directory”
CHENYILONG Blog the error about "no such file or directory" when you get the question like ...
- 记webpack下提取公共js代码的方法
环境: webpack4.6 + html-webpack-plugin 多页面多入口 经多次研究,稍微靠谱可用的配置 optimization: { splitChunks: { minSize: ...
- [转]避免头文件重复包含以及#ifndef 与 #program once 的区别
为了避免同一个文件被include多次,C/C++中有两种方式,一种是#ifndef方式,一种是#pragma once方式.在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一 ...
- list(列表)操作【五】
L表示从左边(头部)开始插与弹出,R表示从右边(尾部)开始插与弹出. 一.概述: 在Redis中,List类型是按照插入顺序排序的字符串链表.和数据结构中的普通链表一样,我们可以在其头部(l ...
- TensorFlow 从零到helloWorld
目录 1.git安装与使用 1.1 git安装 1.2 修改git bash默认路径 1.3 git常用操作 2.环境搭建 2.1 tensorflow安装 2.2 CUDA安装 2.3 ...
- 【黑客免杀攻防】读书笔记10 - switch-case分支
0x1 switch-case分支 switch-case其实就是if-else语句的另一种体现形式.但大于3之后的switchc-case.编译器会对代码进行优化. 1.1 简单switch-cas ...
- python之pip安装mysql-python失败
前言 由于公司使用的python版本是python2,并且连接mysql的包是mysql-python,但是mysql-python 使用pip安装报错,需要C++环境等依赖,于是使用wheel直接安 ...
- retrying模块的学习
retrying模块的学习 我们在写爬虫的过程中,经常遇到爬取失败的情况,这个时候我们一般会通过try块去进行重试,但是每次都写那么一堆try块,真的是太麻烦,所以今天就来说一个比较pythonic的 ...