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 ...
随机推荐
- 十一、Django认证模块--Auth模块
一.常规认证方法 我们学生管理之登录实现一文中已经了解了自己写一个登录逻辑的过程: 1.url配置 urlpatterns = [ url(r'^login/$', views.login), url ...
- Win10版本号区分
版本号 内部版本号 UniversalApiContract 首个正式版 1507 10240 1 首个重大更新 1511 10586 2 一周年更新 Anniversary Update ...
- C# 通过copydata实现进程间通信
最近公司需要实现一个基于copydata进程间通信的功能.原来一直没有接触过Windows的进程通信,这次正好可以学习一下. 程序是基于Winform的,下面直接上代码. 公共类: public cl ...
- TPO-22 C1 Complain about a biased article
/* 加粗:语音部分 * 红色:单词部分 * 斜体:语法部分 * 下划线:信号词/句 */ 第 1 段 1.Listen to a conversation between a student and ...
- Siki_Unity_4-4_丛林战争_Socket/TCP网络游戏开发
Unity 4-4 丛林战争(Socket/TCP网络游戏开发) 任务1:素材.演示.Prerequisite 使用c#的有关TCP的底层API进行服务器端的开发(直接通过socket进行通信) 功能 ...
- 教你如何编写、保存与运行 Python 程序
第一步 接下来我们将看见如何在 Python 中运行一个传统的“Hello World”程序.Python教程本章将会教你如何编写.保存与运行 Python 程序. 通过 Python 来运行的你的程 ...
- Docker配置
Docker基本配置 1.安装 在ubuntu下面执行 wget -qO- https://get.docker.com/ | sh 命令安装Docker. 如果命令的方式无法安装,也可以使用apt- ...
- groupadd命令详解
基础命令学习目录首页 原文链接:https://wtj6891.iteye.com/blog/2096076 groupadd创建组群 使用groupadd命令可以在系统中创建组群账户 语法: gro ...
- python之multiprocessing创建进程
python的multiprocessing模块是用来创建多进程的,下面对multiprocessing总结一下使用记录. multiprocessing创建多进程在windows和linux系统下的 ...
- SpringBoot初始教程之Redis集中式Session管理
1.介绍 有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器).基于Cookie(安全性差).基于全局的统一Session管理(redi ...