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. selenium,unittest——参数化url,并多线程加快脚本运行速度

    利用参数化连续打开网页: #encoding=utf-8import unittestimport paramunittestimport timefrom selenium import webdr ...

  2. 【outPut_Class 输出类】使用说明

    对象:outPut 说明:定义输出结果类的相关操作.此对象的核心是[JSON]类,所以它继承了[JSON]类的所有方法 重要: 输出结果样式为XML时,自带根节点"root".输出 ...

  3. Java中二进制数与整型之间的转换

    import java.io.*; public class Test{ /** * 二进制与整型之间的转换 * @param args * @throws IOException */ public ...

  4. Windows10系统tensorflow-gpu安装

    准备工作 安装前请确保自己的显卡支持gpu加速,支持加速的gpu型号可在下面的链接中查询. https://www.geforce.com/hardware/technology/cuda/suppo ...

  5. CVPR2018 关于视频目标跟踪(Object Tracking)的论文简要分析与总结

    本文转自:https://blog.csdn.net/weixin_40645129/article/details/81173088 CVPR2018已公布关于视频目标跟踪的论文简要分析与总结 一, ...

  6. Python3 Tkinter-Place

    1.绝对坐标 from tkinter import * root=Tk() lb=Label(root,text='hello Place') lb.place(x=0,y=0,anchor=NW) ...

  7. jdk1.8新特性-Lambda表达式使用要点

    前言 在jdk1.8出来的时候看到过,没怎么了解.但是最近再看kafka和spark框架,框架示例中ava版的很多地方用到Lambda表达式,发现使用Lambda表达式代码确实简单了好多,有些例子大致 ...

  8. [C++] Variables and Basic Types

    Getting Started compile C++ program source $ g++ -o prog grog1.cc run C++ program $ ./prog The libra ...

  9. 常用算法Java实现之冒泡排序

    冒泡排序是所有排序算法中最基本.最简单的一种.思想就是交换排序,通过比较和交换相邻的数据来达到排序的目的. 具体流程如下: 1.对要排序的数组中的数据,依次比较相邻的两个数据的大小. 2.如果前面的数 ...

  10. 如何修改git push时的密码

    如何修改git push时的密码 如下: 打开git bash 输入 cd ~/.ssh ls 确定有 id_rsa 和 id_rsa.pub文件 ssh-keygen -p -f id_rsa 第一 ...