Redundant Paths

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19316   Accepted: 8003

题目链接:http://poj.org/problem?id=3177

Description:

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input:

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output:

Line 1: A single integer that is the number of new paths that must be built.

Sample Input:

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output:

2

题意:

给出一个无向图,问最少加多少条边,图中不存在桥。

题解:

还是先对图进行缩点,然后将图变成一颗树,然后我们考虑加最少的边让这个图不存在桥。

这时,我们加边的话,就会形成一条环,环上面所有的边都不为桥。那么我们考虑尽量加边形成较大的环。

这样,其实就只需要把入度为1的点找出来,假设其个数为cnt,那么答案就是(cnt+1)/2了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = , M = ;
int n,m,cnt;
struct Edge{
int u,v,next;
bool operator < (const Edge &A)const{
if(u==A.u) return v<A.v;
return u<A.u;
}
}e[M<<],g[M<<];
int T,tot;
int dfn[N],low[N],cut[N],f[N],d[N],num[N],head[N];
void adde(int u,int v){
e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
}
void init(){
T=;tot=;cnt=;
memset(head,-,sizeof(head));
memset(cut,,sizeof(cut));
memset(dfn,,sizeof(dfn));
}
int find(int x){
return f[x]==x ? f[x] : f[x]=find(f[x]);
}
int same(int x,int y){
return find(x)==find(y);
}
void Union(int x,int y){
int fx=find(x),fy=find(y);
if(fx!=fy) f[fx]=fy;
}
void Tarjan(int u,int pre){
dfn[u]=low[u]=++T;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(v==pre) continue ;
if(!dfn[v]){
Tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u]){
cut[v]=;
}else Union(u,v);
}else{
low[u]=min(low[u],dfn[v]);
}
}
}
int main(){
scanf("%d%d",&n,&m);
init();
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
if(u>v) swap(u,v);
adde(u,v);adde(v,u);
g[i].u=u;g[i].v=v;
}
sort(g+,g+m+);
for(int i=;i<=n;i++) f[i]=i;
Tarjan(,);
for(int i=;i<=m;i++){
int u=g[i].u,v=g[i].v;
if(g[i].u==g[i-].u&&g[i].v==g[i-].v) continue ;
if(same(u,v)) continue ;
int fx=find(u),fy=find(v);
if(!num[fx]) num[fx]=++cnt;
if(!num[fy]) num[fy]=++cnt;
d[num[fx]]++;d[num[fy]]++;
}
int ans = ;
for(int i=;i<=cnt;i++) if(d[i]==) ans++;
cout<<(ans+)/;
return ;
}

POJ3177:Redundant Paths(并查集+桥)的更多相关文章

  1. [POJ3177]Redundant Paths(双联通)

    在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  2. POJ3177 Redundant Paths 双连通分量

    Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...

  3. POJ3177 Redundant Paths(边双连通分量+缩点)

    题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...

  4. POJ3177 Redundant Paths —— 边双联通分量 + 缩点

    题目链接:http://poj.org/problem?id=3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total ...

  5. [POJ3177]Redundant Paths(双连通图,割边,桥,重边)

    题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1.  构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...

  6. poj3177 Redundant Paths

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  7. poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解

    题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...

  8. poj3177 Redundant Paths 边双连通分量

    给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...

  9. POJ3177 Redundant Paths【双连通分量】

    题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...

随机推荐

  1. JDBC中使用Properties类及配置文件的操作

    同时发布于:https://blog.csdn.net/Activity_Time/article/details/81149710 一.properties配置文件 开发中获得连接的4个参数(驱动. ...

  2. poj 3468 (区间修改 区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions:147133   ...

  3. Java算法2

    实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析:若从前向后遍历的话,那Happy后面 ...

  4. Map Reduce Application(Join)

    We are going to explain how join works in MR , we will focus on reduce side join and map side join. ...

  5. java超强分页标签演示

    最近在做一个项目,用到了一个分页,于是动手写了个分页标签,先将代码贴出来,供大家交流,写的不好,请见谅!. 以下是java标签类,继承自SimpleTagSupport package com.lyn ...

  6. maven 教程二 深入

    一:编写POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  7. 4.安装hive

      下载安装包并解压安装元数据库配置hive添加hvie环境变量修改hive-env.sh修改hive配置文件初始化metastore使用hive cli配置hivemestore配置hiveserv ...

  8. svn服务器 备份,迁移,部署方案

    这次做业务迁移,要从一个云厂商迁移到某云厂商,之前每天到全备svn排到用场了,需要搭建一个全新到svn服务并要做迁移,并实现我们开发机到时时代码同步 一.svn备份有很多种,优劣都不同,百度可查,我采 ...

  9. 4-2:实现cp命令

    #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h& ...

  10. Uncaught Error: Syntax error, unrecognized expression: |117的js错误

    117指的是js代码在浏览器运行时的出错的行号 var  a="117|117" 前面的错误是由于有特殊符号“|”,用$("txtId"+a).val();去取 ...