poj3177重修道路——边双连通分量缩点
题目:http://poj.org/problem?id=3177
找桥,缩点,总之都是板子;
对于每个叶子,互相连一条边即可;若最后剩下一个,则去和根节点连边;
所以叶子节点数+1再/2即答案。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,ct=,head[],reg[],c[],dc,dfn[],low[],tim,ans;
bool bri[];
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[],dcc[];
void tarjan(int x,int e)
{
tim++;
dfn[x]=tim;
low[x]=tim;
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
// if(i==(e^1))continue;
if(!dfn[u])
{
tarjan(u,i);
low[x]=min(low[x],low[u]);
if(low[u]>dfn[x])bri[i]=,bri[i^]=;//!!!
}
else if(i!=(e^))
low[x]=min(low[x],dfn[u]);
}
}
void dfs(int x)
{
c[x]=dc;
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
if(!c[u]&&!bri[i])
dfs(u);
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
memset(head,,sizeof head);
memset(reg,,sizeof reg);
memset(c,,sizeof c);
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(bri,,sizeof bri);
ct=;ans=;dc=;tim=;
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
edge[++ct]=N(y,head[x]);head[x]=ct;
edge[++ct]=N(x,head[y]);head[y]=ct;
}
tarjan(,);
for(int i=;i<=n;i++)
{
if(c[i])continue;
dc++;
dfs(i);
}
for(int i=;i<=ct;i+=)
{
int u=edge[i].to,v=edge[i^].to;
if(c[u]==c[v])continue;
reg[c[u]]++;reg[c[v]]++;
}
for(int i=;i<=dc;i++)
if(reg[i]==)ans++;
printf("%d\n",(ans+)/);
}
return ;
}
poj3177重修道路——边双连通分量缩点的更多相关文章
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- poj3177 && poj3352 边双连通分量缩点
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12676 Accepted: 5368 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- POJ3694 Network(边双连通分量+缩点+LCA)
题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- poj 3177 Redundant Paths(边双连通分量+缩点)
链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...
随机推荐
- C#中判断某个值是否存在于枚举
我有一个枚举类型: #region -酒的种类- public enum WineType { 白酒 = 3, 葡萄酒 = 4, 洋酒 = 5, 老年陈酒 = 16, 啤酒 = 17 } #endre ...
- items" does not support runtime expression
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 更改为 <%@tagl ...
- AWR元数据的迁移或导入到其它数据库
我们能够将AWR元数据迁移(导入)到其它数据库.低版本号的导入到高版本号,再用高版本号的数据库生成AWR报告,也能使用一些新特性,如 SQL ordered by Physical Reads (Un ...
- OpenCV2马拉松第15圈——边缘检測(Laplace算子,LOG算子)
收入囊中 拉普拉斯算子 LOG算子(高斯拉普拉斯算子) OpenCV Laplacian函数 构建自己的拉普拉斯算子 利用拉普拉斯算子进行图像的锐化 葵花宝典 在OpenCV2马拉松第14圈--边缘检 ...
- iOS-代理托付的使用
#import "Rigester_ViewController.h" #import "Rigester_ViewController.h" @interfa ...
- Android发送验证码的倒计时button
1 直接上图 2 原理 原理非常easy,就是把对应的倒计时逻辑等封装到一个控件中,并向外部提供接口. 3 代码 import java.util.Timer; import java.util.Ti ...
- Mybatis资料
1. 入门案例 https://www.cnblogs.com/xdp-gacl/p/4261895.html 2. 详细笔记 以及配套视频教程: 笔记:https://blog.csdn.net/S ...
- LR中select next row和update value on的设置
LR的参数的取值,和select next row和update value on的设置都有密不可分的关系.下表给出了select next row和update value on不同的设置,对于LR ...
- android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件
主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...
- C#使用tesseract3.02识别验证码模拟登录(转)
转自http://www.cnblogs.com/JinJi-Jary/p/5625414.html