POJ3177_Redundant Paths
给你一个无向图,求至少加入多少条边,使得整个图是双联通的。
通过枚举题意,发现重边是不算的,直接去掉。
首先把那些边是桥计算出来,把位于同一个连通分量里面的点缩成一个点(并查集),然后计算缩点后有多少个点的度数为1,只要处理这些点就好了。
每次处理连接任意两个度数为1的点,增加一个联通分量,这样总共只要连接(n+1)/2次即可。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#define maxn 50050
using namespace std; int f[maxn],g[maxn];
int d[maxn],low[maxn],first[maxn];
int to[maxn],next[maxn],edge=-;
bool c[maxn];
int n,m,dfs_clock,ans;
map<int, map<int,int> > ss; void _init()
{
dfs_clock=;
for (int i=; i<=n; i++) d[i]=first[i]=-,f[i]=i,g[i]=;
} void addedge(int U,int V)
{
c[++edge]=false;
to[edge]=V,next[edge]=first[U],first[U]=edge;
c[++edge]=false;
to[edge]=U,next[edge]=first[V],first[V]=edge;
} void dfs(int cur,int fa)
{
d[cur]=++dfs_clock,low[cur]=d[cur];
for (int i=first[cur]; i!=-; i=next[i])
{
if ((i^)==fa || i==fa) continue;
if (d[to[i]]==-) dfs(to[i],i);
low[cur]=min(low[cur],low[to[i]]);
}
if (fa!=- && low[cur]>=d[cur]) c[fa]=c[fa^]=true;
} int father(int x)
{
return f[x]==x?x:f[x]=father(f[x]);
} int main()
{
int U,V;
scanf("%d%d",&n,&m);
_init();
for (int i=; i<=m; i++)
{
scanf("%d%d",&U,&V);
if (U>V) swap(U,V);
if (ss[U][V]) continue;
addedge(U,V);
ss[U][V]=;
}
dfs(U,-); for (int i=; i<edge; i+=)
{
if (c[i]) continue;
int fx=father(to[i]),fy=father(to[i+]);
f[fx]=fy;
}
for (int i=; i<edge; i+=)
{
if (!c[i]) continue;
int fx=father(to[i]),fy=father(to[i+]);
g[fx]++,g[fy]++;
} for (int i=; i<=n; i++)
if (g[i]==) ans++;
printf("%d\n",(ans+)/);
return ;
}
POJ3177_Redundant Paths的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- LeetCode-62-Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
随机推荐
- Kubernetes中的网络
一.引子 既然Kubernetes中将容器的联网通过插件的方式来实现,那么该如何解决这个的联网问题呢? 如果你在本地单台机器上运行docker容器的话注意到所有容器都会处在docker0网桥自动分配的 ...
- 引用“kernel32”读写ini配置文件
引用"kernel32"读写ini配置文件 unity ini kernel32 配置文件 引用"kernel32"读写ini配置文件 OverView ke ...
- linux运维升级路线
运维工程师是从一个呆逼进化为苦逼再成长为牛逼的过程,前提在于你要能忍能干能拼,还要具有敏锐的嗅觉感知前方潮流变化.如:今年大数据,人工智能比较火……(相对表示就是 Python 比较火) 之前写过运维 ...
- VisionPro相机操作类
在网站上看到这个,保存下来,以后用到了,再看一下.谢谢原创的分享! #region 获得相机信息方法 /// <summary> /// 公有静态方法,查找单个相机.例如“Basler” ...
- bitcoin PoW原理及区块创建过程
bitcoin PoW原理及区块创建过程 PoW 为了在点对点的基础上实现一个分布式时间戳服务器,我们需要使用PoW(Proof of Work)系统来达成共识.PoW过程就是寻找一个目标值的过程,当 ...
- Java 内存模型_1
title: Java 内存模型_1 date: 2017-01-15 17:11:02 tags: [JMM] categories: [Programming,Java] --- 概述 本文记录 ...
- cal命令详解
基础命令学习目录首页 原文链接:https://www.yiibai.com/linux/cal.html cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又称格列历,通称阳历.“阳 ...
- Python函数式编程中map()、reduce()和filter()函数的用法
Python中map().reduce()和filter()三个函数均是应用于序列的内置函数,分别对序列进行遍历.递归计算以及过滤操作.这三个内置函数在实际使用过程中常常和“行内函数”lambda函数 ...
- Navicat Premium 连接Oracle 数据库
昨天开始工作的时候听同事说:Navicat可以连各种数据库,包括Oracle,头一次听说!!!很是尴尬.现在记录一下怎么用Navicat连接Oracle.最重要的是,Navicat只支持32的Orac ...
- 关于Keil C关键字xdata和data的问题
1.xdata表示这是一个外部RAM地址内的数据,数据最终将被保存至外部RAM的某个地址单元中:但是,外部RAM只能通过寄存器间接寻址来访问,也就是说,其地址需要保存在内部RAM中(其实或许是SFR中 ...