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

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

首先把那些边是桥计算出来,把位于同一个连通分量里面的点缩成一个点(并查集),然后计算缩点后有多少个点的度数为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. 开箱即用 - Memcache缓存

    废话少说,先上代码C# memcache Demo memcache 是服务器缓存系统,以键值对方式保存数据到内存中,把对象序列化后,理论上可支持所有的数据类型. 使用情景:怎么用都可以,注意的是它只 ...

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

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

  3. [Processing] 弹球

    PVector localPos = new PVector(0,0);//起始位置 PVector velocity;//速度方向 float speed = 20;//速度大小 void setu ...

  4. Python实现学生系统

    # 4. 修改之前的学生信息管理程序,实现添加菜单和选择菜单操作功能: # 菜单: # +-----------------------------+ # | 1) 添加学生信息 | # | 2) 查 ...

  5. [C++]模板类和模板函数

    参考: C++ 中模板使用详解 C++模板详解 概念 为了避免因重载函数定义不全面而带来的调用错误,引入了模板机制 定义 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模 ...

  6. kafka consumer demo

    kafka消费者demo pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmln ...

  7. Visual Studio Code搭建NodeJs的开发环境

    一.Visual Studio Code搭建NodeJs的开发环境 1.下载安装NodeJs并配置环境变量 可以参考:NodeJs的安装和环境变量配置 2.下载安装 VS Code编辑器 可以参考:V ...

  8. windows 7 php 7.1 命令行 执行 中文文件名 的PHP文件

    在PHP5.6时代直接执行 php.exe  文件.php 是没有的这个问题 在win下的命令行中 活动代码页命令 chcp 修改 chcp 936  //gbk chcp 65001 //utf-8 ...

  9. 满帮集团CEO:未来将向“智慧型”公司转变,要成为一家生态公司

    谁都想成为下一个滴滴.显然,王刚也希望在物流业,货车帮与运满满在合并后,能够企及滴滴的高度. 货车帮与运满满,都曾是货运物流领域的翘楚,也因为业务的竞争关系有过水火不容厮杀.但最终还是在资本与地方政府 ...

  10. Python基础_异常处理与跟踪

    异常的种类 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x IOError 输入/输出异常:基本上是无法打开文件 ImportError 无法引入模块或 ...