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的更多相关文章

  1. 【bzoj1718】Redundant Paths 分离的路径

    1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 964  Solve ...

  2. 【POJ 3177】Redundant Paths(边双连通分量)

    求出每个边双连通分量缩点后的度,度为1的点即叶子节点.原图加上(leaf+1)/2条边即可变成双连通图. #include <cstdio> #include <cstring> ...

  3. 【POJ 3177】Redundant Paths

    http://poj.org/problem?id=3177 又写了一遍手动栈... 把边双都缩点,缩成一棵树,答案就是树中度数为1的点的个数除2上取整. 为什么呢?因为1个度数为1的点的树需要多连0 ...

  4. 【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 ...

  5. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  6. 【题解】【排列组合】【素数】【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 ...

  7. 【题解】【矩阵】【回溯】【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 ...

  8. 【数组】Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. 【数组】Unique Paths

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

随机推荐

  1. 【shiro】shiro学习笔记1 - 初识shiro

    [TOC] 认证流程 st=>start: Start e=>end: End op1=>operation: 构造SecurityManager环境 op2=>operati ...

  2. pedestal-工作记

    1.基于bootstrap-v3和flat-ui-v3为第十届外语活动月写了个页面 http://www.pedestal.cn/static/activity/index.html 2.资料 boo ...

  3. jQuery on(),live(),trigger()

    jQuery on()方法是官方推荐的绑定事件的一个方法. $(selector).on(event,childSelector,data,function,map); 由此扩展开来的几个以前常见的方 ...

  4. Python高手之路【八】python基础之requests模块

    1.Requests模块说明 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2  ...

  5. 在创维E900-S悦Me盒子上安装第三方软件

    0x00 不甘寂寞 创维E900-S这款悦Me盒子功能还算可以,但不能接受它禁止安装第三方软件这一点.网上搜了半天,可能是比较新的机型没人关注,找不到任何方法,只好自己动手试试. 0x01 Fiddl ...

  6. 史上最全Html与CSS布局技巧

    单列布局水平居中水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元素的父 ...

  7. MySQL基础 - 如何系统地学习数据库?

    对于数据库的认知,除了大学的时候上过数据库这门课,留下的印象大概就是几条SQL语句一些模棱两可的基本概念,直到工作后面临使用场景才发现数据库的重要性.故归纳总结一下自己的数据库学习之路. 学习资源: ...

  8. SQL语句的添加、删除、修改多种方法

    SQL语句的添加.删除.修改虽然有如下很多种方法,但在使用过程中还是不够用,不知是否有高手把更多灵活的使用方法贡献出来? 添加.删除.修改使用db.Execute(Sql)命令执行操作╔------- ...

  9. 浪潮之巅IT那点事之三——神奇的规律

    “道可道,非常道”是老子在<道德经>中的开篇第一句话,这句话的意思是:万事万物其真理是可以探索并道说得出来的,但这些真理并非是永恒的,天道轮转,没有永恒不变的真理(来自百度百科).在IT行 ...

  10. Win7下手动卸载oracle 11G

    由于安装过程中遇到的之前提到的那个问题,http://www.cnblogs.com/shenliang123/p/3141886.html  知道解决方法后,也只能将oracle重新卸载后再进行安装 ...