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

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

首先把那些边是桥计算出来,把位于同一个连通分量里面的点缩成一个点(并查集),然后计算缩点后有多少个点的度数为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. 初窥Linux之我最常用的20条命令

    1.cd命令   这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切 ...

  2. Python获取每一位的数字,并返回到列表

    通过计算 def calc(value): result = [] while value: result.append(value % 10) value = value // 10 #逆序,按正常 ...

  3. Redis源码阅读(四)集群-请求分配

    Redis源码阅读(四)集群-请求分配 集群搭建好之后,用户发送的命令请求可以被分配到不同的节点去处理.那Redis对命令请求分配的依据是什么?如果节点数量有变动,命令又是如何重新分配的,重分配的过程 ...

  4. SmartRaiden 和 Lighting Network 进行去中心化跨链原子资产交换

    作者介绍 虫洞社区·签约作者 steven bai 前言 如果能够进行以太坊和比特币跨链原子资产交换,是不是一件很酷的事情? 目前链下的扩容方式有很多,最广为人知的就是比特币的闪电网络和以太坊的雷电网 ...

  5. Windows 8.1 "计算机" 中文件夹清理

    计算机 win8.1 也叫这台电脑 清理文件夹 保留磁盘分区图标 注册表清理 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ ...

  6. 第39次Scrum会议(12/5)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/12/5 11:35~11:57,总计22min.地点:东北师 ...

  7. 王者荣耀交流协会final发布第五次scrum例会

    1.例会照片 成员高远博,冉华,王磊,王玉玲,任思佳,袁玥,王磊,王超. master:王磊 2.时间跨度 2017年12月5日 18:00 — 18:21,总计21分钟 3.地点 一食堂二楼沙发座椅 ...

  8. js中模拟a标签的点击事件

    var a = document.createElement('a'); a.target = "_blank"; a.href = "personal"; a ...

  9. vim文本处理技巧

    如果要把这篇文章写的详细透彻,那我没有必要去书写,因为已经有了这本书--<Vim实用技巧> 如果时间和精力足够的同学可以购买或者借阅,真的是写的很详细. Vim实用技巧 (豆瓣)http: ...

  10. C++:类中两个易被忽略的默认函数

    C++的自定义类中有六个默认的函数,即如果用户没有显式定义这些函数时,C++编译器会类中生成这些函数的默认形式.除了大家所熟知的构造函数.拷贝构造函数.赋值函数和析构函数外,C++为自定义类 还提供了 ...