给你一个无向图,求至少加入多少条边,使得整个图是双联通的。

通过枚举题意,发现重边是不算的,直接去掉。

首先把那些边是桥计算出来,把位于同一个连通分量里面的点缩成一个点(并查集),然后计算缩点后有多少个点的度数为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的更多相关文章

  1. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [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 ...

  4. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. 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 ...

  7. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  9. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

随机推荐

  1. 总结hibernate框架的常用检索方式

    1.hibernate框架的检索方式有以下几种: OID检索:根据唯一标识OID检索数据 对象导航检索:根据某个对象导航查询与该对象关联的对象数据 HQL检索:通过query接口对象查询 QBC检索: ...

  2. Security Permissions Caching

    Security Permissions Caching Security permission caching is implemented in Security Adapters - class ...

  3. java中object数据怎么转换成json数据

    可以通过这个(json-lib-2.3-jdk15.jar)jar里的方法转换 JSONObject json = JSONObject.fromObject(Object); 如果对象数组 JSON ...

  4. Qt-网易云音乐界面实现-4 实现推荐列表和我的音乐列表,重要在QListWidget美化

    来标记下这次我么实现的部分 这次我们来是试下这部分功能,来对比一下,左边是原生,右面是我写的,按着模仿的海可以哈,就有有的资源不是一样了,因为我连抠图都懒得扣了了 好了,现在就是我的是先过程了,主要教 ...

  5. redis-4.0.2

    redis-4.0.2.tar.gz 链接:https://pan.baidu.com/s/1qj4bSgM1s2InLikugRNqKA 提取码:tozq 复制这段内容后打开百度网盘手机App,操作 ...

  6. Netty源码分析第5章(ByteBuf)---->第1节: AbstractByteBuf

    Netty源码分析第五章: ByteBuf 概述: 熟悉Nio的小伙伴应该对jdk底层byteBuffer不会陌生, 也就是字节缓冲区, 主要用于对网络底层io进行读写, 当channel中有数据时, ...

  7. 配置tensorflow环境(anaconda+jupyter notebook)

    很早之前,tensorflow环境之前我也曾装过,但是用的不是很舒服,很多问题都不明所以然.今天想要系统地学习一下tensorflow,于是又重新搭建了一遍,这次还是踩了不少坑.特此写下此文,供有兴趣 ...

  8. c++ getline()和get()的区别

    1.方法get(char &)和get(void)提供不跳过空白的单字符输入功能:2.函数get(char * , int , char)和getline(char * , int , cha ...

  9. 模块-Memcached、Redis

    目录 Mecache 安装 使用 Redis 安装 Python操作Redis 操作模式 连接池 操作 String Hash List Set sort set 其他常用操作 管道 发布订阅 sen ...

  10. vim 多个文件切换 :b 命令

    MiniBufExplorer插件的使用 博客分类: vim vimMiniBufExplorer 快速浏览和操作Buffer -- 插件: MiniBufExplorer 下载地址 [http:// ...