POJ3177:Redundant Paths——题解
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——题解的更多相关文章
- POJ3177 Redundant Paths 双连通分量
Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...
- POJ3177:Redundant Paths(并查集+桥)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19316 Accepted: 8003 ...
- POJ3177 Redundant Paths —— 边双联通分量 + 缩点
题目链接:http://poj.org/problem?id=3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total ...
- [POJ3177]Redundant Paths(双联通)
在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Tota ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- poj3177 Redundant Paths
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- [POJ3177]Redundant Paths(双连通图,割边,桥,重边)
题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1. 构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...
- POJ3177 Redundant Paths【双连通分量】
题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...
随机推荐
- 在nginx环境下,直接用域名访问(首页)
①: server { listen 80; server_name www.njm1.com; location = / { #=/规则可以直接访问域名.如:www.njm1.com.跳转到http ...
- Linux命令应用大词典-第35章 终端
35.1 tty:显示当前连接到当前标准输入的终端设备文件名 35.2 consoletype:显示连接到标准输入的控制台类型 35.3 fgconsole:显示活动的虚拟终端数量 35.4 ming ...
- lesson 17 The longest suspension bridge in the world
lesson 17 The longest suspension bridge in the world agreeable adj. 令人愉快的:适合的 = pleasant be located ...
- leetcode-单词探索
单词搜索 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母 ...
- [Clr via C#读书笔记]Cp1CLR执行模型
Cp1CLR执行模型 本章的概念点 CLR=Common Language Runtime 内存管理,程序集加载,安全性,异常处理和线程同步.CLR是基础,支持着面向它的各种语言.各种语言会被对应的编 ...
- python计算工资个税
# -*- coding: utf-8 -*- total = int(input("税前总计:")) #公积金10% Gongjijin = total * 0.1 print( ...
- java常见的异常类型
Exception分为两类:非运行是异常和运行时异常. java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常.A:NullPointerExcepti ...
- mysql下分组取关联表指定提示方法,类似于mssql中的cross apply
转至:https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results ...
- codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)
Berland has n cities, some of them are connected by bidirectional roads. For each road we know wheth ...
- Line belt(三分镶嵌)
In a two-dimensional plane there are two line belts, there are two segments AB and CD, lxhgww's spee ...