http://poj.org/problem?id=3177

明显要求桥的一道题。

(因为有桥就说明只能从那一条路走,换句话说就是只有一种方法)

求完桥后按照结论(加几条边成双连通图的结论,不会请baidu)就可以输出ans啦!

(为此学了一下新的桥的求法……原来的那个常数太大了)

#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
inline int read(){
int x=,w=;char ch=;
while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*w;
}
const int maxn=;
int cnt=,head[maxn];
struct node{
int w;
int ed;
int nxt;
int st;
}edge[];
void add(int u,int v){
cnt++;
edge[cnt].ed=v;
edge[cnt].st=u;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
bool bridge[];
int dfn[maxn];
int low[maxn];
bool instack[maxn];
int fa[maxn];
int from[maxn];
int indeg[maxn];
int t=;
void tarjan(int u){
t++;
dfn[u]=t;
low[u]=t;
for(int i=head[u];i;i=edge[i].nxt){
if(i==(from[u]^))continue;
int v=edge[i].ed;
if(!dfn[v]){
from[v]=i;
tarjan(v);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])bridge[from[v]]=bridge[from[v]^]=;
}else{
low[u]=min(low[u],dfn[v]);
}
}
return;
}
int find(int a){
if(fa[a]==a)return a;
return fa[a]=find(fa[a]);
}
int main(){
int f=read();
int r=read();
for(int i=;i<=r;i++){
int u=read();
int v=read();
add(u,v);
add(v,u);
}
tarjan();
for(int i=;i<=f;i++)fa[i]=i;
for(int i=;i<=cnt;i+=){
if(!bridge[i])fa[find(edge[i].st)]=find(edge[i].ed);
}
for(int i=;i<=cnt;i+=){
if(bridge[i])indeg[find(edge[i].st)]++,indeg[find(edge[i].ed)]++;
}
int leaf=;
for(int i=;i<=f;i++){
if(find(i)==i){
if(indeg[i]==)leaf++;
}
}
printf("%d\n",(leaf+)/);
return ;
}

POJ3177:Redundant Paths——题解的更多相关文章

  1. POJ3177 Redundant Paths 双连通分量

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

  2. POJ3177:Redundant Paths(并查集+桥)

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

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

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

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

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

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

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

  6. poj3177 Redundant Paths

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

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

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

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

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

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

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

随机推荐

  1. C#如何使用反射实现通过字符串创建类

    在做项目中碰到一个问题,就是如何在知道一个类的名字,如何创建这个类呢.做的一个小测试,直接贴代码了. using System; using System.Collections.Generic; u ...

  2. HashMap在并发场景下踩过的坑

    本文来自网易云社区 作者:张伟 关于HashMap在并发场景下的问题有很多人,很多公司遇到过!也很多人总结过,我们很多时候都认为这样都坑距离自己很远,自己一定不会掉入这样都坑.可是我们随时都有就遇到了 ...

  3. 使用QUIC

    QUIC是Google新开发的一个基于UDP的协议,它提供了像TCP一样的传输可靠性保证,可以实现数据传输的0-RTT延迟,灵活的设计使我们可以对它的拥塞控制及流量控制做更多的定制,它还提供了传输的安 ...

  4. unity share current game screen

    using UnityEngine; using System.Collections; using UnityEngine.UI; using System.IO; public class Tak ...

  5. selenium--特殊元素定位

    该篇博客总结特殊元素(select.radio\checkbox.时间控件.文件上传.图片验证码.模拟鼠标操作.Js 或 JQuery调用)操作. 1.select @Test public void ...

  6. [CodeForce721C]Journey

    题目描述 Recently Irina arrived to one of the most famous cities of Berland - the Berlatov city. There a ...

  7. lintcode735. Replace With Greatest From Right

    Given an array of integers, replace every element with the next greatest element (greatest element o ...

  8. Java基础知识:Java实现Map集合二级联动1

    Java实现Map集合二级联动 Map集合可以保存键值映射关系,这非常适合本实例所需要的数据结构,所有省份信息可以保存为Map集合的键,而每个键可以保存对应的城市信息,本实例就是利用Map集合实现了省 ...

  9. css重修之书(一):如何用css制作比1px更细的边框

    如何用css制作比1px更细的边框 在项目的开发过程中,我们常常会使用到border:1px solid xxx,来对元素添加边框: 可是1px的border看起来还是粗了一些粗,不美观,那么有什么方 ...

  10. Python中除法:/和//

    在Python中,除法有两种:/和//. X / Y 对于Python2.X来说,如果两个操作数都是整数,那么结果将向下取整(这个和C里面的不同,C里面是向0取整),也就是说,如果结果本来是-2.5, ...