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条路,求至少要新建多少条路,使得任 ...
随机推荐
- keras----resnet-vgg-xception-inception
来源: https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/ classi ...
- (webstorm的css编写插件)Emmet:HTML/CSS代码快速编写神器
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- Dubbo(一)Dubbo资料
这个资料绝对权威了:http://dubbo.io/user-guide/
- erlang的timer定时器浅析
timer作为其计时器: erlang的计时器timer是通过一个唯一的timer进程实现的,该进程是一个gen_server,用户通过timer:send_after和timer:apply_aft ...
- Go语言入门系列1:安装,How to Write Go Code
https://golang.org/doc/code.html src contains Go source files, pkg contains package objects, and bin ...
- 初识ASP.net-牛腩新闻公布系统
在做牛腩新闻公布的系统的时候,总有一种感觉就是:我仍然在敲机房收费系统,唯一不同的一点.就是敲机房收费的时候,用户界面是是自己手动画界面.而,在牛腩新闻公布系统中,用户界面,却是须要自己 ...
- programming review (c++): (3)graph, binary search
I.graph #include <iostream> #include <vector> using namespace std; vector<vector<, ...
- Ajax学习笔记(2)--load()方法
<head runat="server"> <title></title> <script src="http://localh ...
- 【题解】[JSOI2008]最大数
[题解][P1198 JSOI2008]最大数 正难则反,意想不到. 这道题是动态让你维护一个数列,已经在数列里面的数据不做改变,每次在最后加上一个数,强制在线. 既然正着做很难,考虑如果时间倒流,不 ...
- Dijkstra的堆优化
先附上一个例题:P3371 [模板]单源最短路径 一眼扫去,最短路... spfa可行,但是今天的主题是Dijkstra: #include<iostream> #include<a ...