POJ3352 Road Construction(边双连通分量)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11210 | Accepted: 5572 |
Description
It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.
The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.
Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.
So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.
Input
The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.
Output
One line, consisting of an integer, which gives the minimum number of roads that we need to add.
Sample Input
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10 3 3
1 2
2 3
1 3
Sample Output
2
0
【分析】本题就是问最少加多少边使得它成为边双连通图(边连通度>1)。
先用Tarjan模板将各双连通分量缩点,然后ans=(度数为一的缩点数+1)/2。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int s,t,n,m,cnt,tim,top,cut,k;
int head[M],dfn[M],low[M],stack1[M];
int num[M],in[M],out[M],vis[M],w[N][N];
bool flag=false;
struct man {
int to,nxt;
} edg[M];
void addedg(int u,int v) {
edg[cnt].to=v;
edg[cnt].nxt=head[u];
head[u]=cnt++;
edg[cnt].to=u;
edg[cnt].nxt=head[v];
head[v]=cnt++;
}
void init() {
cnt=;
tim=;
top=cut=k=;
memset(head,-,sizeof head);
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(stack1,,sizeof stack1);
memset(num,,sizeof num);
memset(in,,sizeof in);
memset(out,,sizeof out);
memset(vis,,sizeof vis);
memset(edg,,sizeof edg);
memset(w,,sizeof w);
}
void Tarjan(int u,int fa) {
int v;
low[u] = dfn[u] = ++tim;
vis[u] = ;
for(int e = head[u]; e != -; e = edg[e].nxt) {
v = edg[e].to;
if(!dfn[v]) {
Tarjan(v,u);
low[u] = min(low[u], low[v]);
} else if(vis[v]&&v!=fa) {
low[u] = min(low[u], dfn[v]);
}
} }
int main() {
int T,u,v;
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=; i<m; i++) {
scanf("%d%d",&u,&v);
addedg(u,v);
}
Tarjan(,);
for(int i=; i<=n; i++) {
for(int j=head[i]; j!=-; j=edg[j].nxt) {
int v=edg[j].to;
if(low[i]!=low[v])out[low[i]]++,out[low[v]]++;
cut=max(cut,low[i]);
//printf("!!!%d %d\n",i,low[i]);
}
}
int son=;
for(int i=; i<=cut; i++) {
if(out[i]==)son++;
}//printf("here\n");
printf("%d\n",(son+)/);
}
return ;
}
POJ3352 Road Construction(边双连通分量)的更多相关文章
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- POJ 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- POJ 3352 Road Construction (边双连通分量)
题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...
- poj 3352 Road Construction(边双连通分量+缩点)
题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...
- [POJ3352]Road Construction
[POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...
- POJ3352 Road Construction (双连通分量)
Road Construction Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
随机推荐
- APP store 上架过程中碰到的那些坑&被拒的各种奇葩原因整理&审核指南中文版
苹果官方发布的十大常见被拒原因 1.崩溃次数和Bug数量.苹果要求开发者在将应用提交给App Store之前彻查自己的应用,以尽量避免Bug的存在. 2.链或错误的链接.应用中所有的链接必须是真实且有 ...
- iOS 高级框架的介绍
Core Data.Core Graphics.Core Animation 和 OpenGL ES 框架,是对于应用程序开发很重要的高级技术,因此需要花时间来学习和掌握. Core Data 框架管 ...
- ++index 与 index++
摘自:C++标准程序库
- phpstom 实用laravel 需要附加的 命令
首先利用composer 下载相关的插件 在根目录执行此代码 composer require barryvdh/laravel-ide-helper 再者在config/app.php 添加一条命令 ...
- node 学习笔记
以下笔记默认安装完成node 及npm 1.安装express 新版本的express-generator已经独立出来,全局安装这个包就ok. npm install express-generato ...
- Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.
修改settings.xml: <mirror> <id>nexus-osc</id> <mirrorOf>*</mirrorOf> < ...
- HDU 4462
http://acm.hdu.edu.cn/showproblem.php?pid=4462 一道题意不清的水题 题意:给一个n*n的格子,在上面放草人,每个草人有恐惧范围,问最少选择几个草人可以覆盖 ...
- 解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException
解决spring配置中的bean类型的问题:BeanNotOfRequiredTypeException这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体 ...
- 重定位shell
http://www.tldp.org/LDP/abs/html/io-redirection.html http://unix.stackexchange.com/questions/20469/d ...
- C++ QUICK REFERENCE
C++ string 用法详解 字符串分割(C++) C++ QUICK REFERENCE Matt Mahoney, mmahoney@cs.fit.edu DECLARATIONS enum ...