【poj3177】 Redundant Paths
http://poj.org/problem?id=3177 (题目链接)
题意
给出一个n个节点m条边的无向图,求最少连几条边使图中没有桥。
Solution
我们可以发现,用最少的边使得图中没有桥,那么就是将图缩点得到树,求使每个叶子节点相连所需要的最少边数,即 (叶子节点个数+1)/2 。
Tarjan求出图中的桥,以及并查集记录下每个节点属于哪个双连通分量,只与一座桥相连的点即为叶子节点,统计答案即可。
听说会有重边,不过对我的程序好像并没有什么影响。
细节
这道题如果是求双联通分量然后缩点的话,还要考虑重边的影响,比如2 2 1 2 1 2这样的数据输出是1。所以我这里直接将桥抠出来并且用并查集维护双连通分量,这样通过桥来使缩点后树上节点度数增加,这样就能有效避免重边的问题。
代码
// poj3177
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<set>
#define MOD 1000000007
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=10010;
struct edge {int to,next;}e[maxn<<2];
int f[maxn],dfn[maxn],vis[maxn],low[maxn],head[maxn],bridge[maxn][2],cnts[maxn];
int cnt,ind,s,n,m; void insert(int u,int v) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;
}
int find(int x) {
return x==f[x] ? x : f[x]=find(f[x]);
}
void Tarjan(int u,int fa) {
dfn[u]=low[u]=++ind;
vis[u]=1;
for (int i=head[u];i;i=e[i].next) if (e[i].to!=fa) {
if (!vis[e[i].to]) {
Tarjan(e[i].to,u);
low[u]=min(low[u],low[e[i].to]);
if (dfn[u]<low[e[i].to]) {
bridge[++s][0]=u;
bridge[s][1]=e[i].to;
}
else {
int r1=find(u),r2=find(e[i].to);
f[r1]=r2;
}
}
else low[u]=min(low[u],dfn[e[i].to]);
}
}
int main() {
while (scanf("%d%d",&n,&m)!=EOF) {
cnt=ind=s=0;
for (int i=1;i<=n;i++) head[i]=dfn[i]=low[i]=vis[i]=cnts[i]=0;
for (int i=1;i<=m;i++) {
int x,y;
scanf("%d%d",&x,&y);
insert(x,y);
}
for (int i=1;i<=n;i++) f[i]=i;
Tarjan(1,-1);
for (int i=1;i<=s;i++) {
cnts[find(bridge[i][0])]++;
cnts[find(bridge[i][1])]++;
}
int ans=0;
for (int i=1;i<=n;i++) if (cnts[i]==1) ans++;
printf("%d\n",(ans+1)/2);
}
return 0;
}
【poj3177】 Redundant Paths的更多相关文章
- 【bzoj1718】Redundant Paths 分离的路径
1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 964 Solve ...
- 【POJ 3177】Redundant Paths(边双连通分量)
求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...
- 【POJ 3177】Redundant Paths
http://poj.org/problem?id=3177 又写了一遍手动栈... 把边双都缩点,缩成一棵树,答案就是树中度数为1的点的个数除2上取整. 为什么呢?因为1个度数为1的点的树需要多连0 ...
- 【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】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【题解】【排列组合】【素数】【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】Unique Paths II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【数组】Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【数组】Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- 第16章 Windows线程栈
16.1 线程栈及工作原理 (1)线程栈简介 ①系统在创建线程时,会为线程预订一块地址空间(即每个线程私有的栈空间),并调拨一些物理存储器.默认情况下,预订1MB的地址空间并调拨两个页面的存储器. ② ...
- 文件泄露&php代码审计
这道题,还是很不错的.卡在了token绕过那里,不得已看了别人的writeUp,才做出来,惭愧! 但还是想写写WriteUp做一下记录! 首先是打开题目,习惯性查看源码,发现了点蛛丝马迹 知道了,管理 ...
- Android Handler处理机制 ( 三 ) ——Handler,Message,Looper,MessageQueue
在android中提供了一种异步回调机制Handler,使用它,我们可以在完成一个很长时间的任务后做出相应的通知 handler基本使用: 在主线程中,使用handler很简单,new一个Handle ...
- ftp虚拟账号登陆
配置使用虚拟用户登录的FTP服务器,可以避免使用操作系统帐号作为FTP用户带来的一些安全问题,也便于通过数据库或其它程序来进行管理.废话不多说,这里记录下ftp虚拟账号登陆的部署过程及其中遇到的问题: ...
- 关于“服务器提交了协议冲突. Section=ResponseStatusLine"问题
你的问题的原因是这样的,ASP.Net 2.0 增强了安全性,对一些有危害的http 头进行了判断,比如url中有空格的情况,以帮助网站提高网络攻击的防御能力.如果你的http头中有一些ASP.NET ...
- ES配置文件参考与参数详解
cluster.name: data-cluster node.name: "data-es-05" #node.data: false # Indexing & Cach ...
- Restful是什么,SOAP Webservice和RESTful Webservice
首先,应该怀着这样一种心态来学习Restful——Restful你可以将其理解一种软件架构风格,并且诠释了Http协议的设计初衷,所以不要把他理解的那么神秘,Restful风格有好处,当然也是有坏处的 ...
- [原]iBatis.Net(C#)系列一:简介及运行环境
转载请注明http://www.cnblogs.com/13590/archive/2013/02/27/2934580.html 摘要:介绍iBatis.Net的基本情况和运行原理,运行环境中各参数 ...
- with(nolock)的用法
with(nolock)的介绍 大家在写查询时,为了性能,往往会在表后面加一个nolock,或者是with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的. 当同一时间有多个用户 ...
- sys.stdin的三种方式
1. for line in sys.stdin: import sys sys.stdout.write('根据两点坐标计算直线斜率k,截距b:\n') for line in sys.stdin: ...