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(边双连通分量)的更多相关文章

  1. 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 ...

  2. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  3. POJ 3352 Road Construction (边双连通分量)

    题目链接 题意 :有一个景点要修路,但是有些景点只有一条路可达,若是修路的话则有些景点就到不了,所以要临时搭一些路,以保证无论哪条路在修都能让游客到达任何一个景点 思路 :把景点看成点,路看成边,看要 ...

  4. poj 3352 Road Construction(边双连通分量+缩点)

    题目链接:http://poj.org/problem?id=3352 这题和poj 3177 一样,参考http://www.cnblogs.com/frog112111/p/3367039.htm ...

  5. [POJ3352]Road Construction

    [POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...

  6. POJ3352 Road Construction (双连通分量)

    Road Construction Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Sub ...

  7. POJ3352 Road Construction 双连通分量+缩点

    Road Construction Description It's almost summer time, and that means that it's almost summer constr ...

  8. poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解

    题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...

  9. POJ-3352 Road Construction,tarjan缩点求边双连通!

    Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...

随机推荐

  1. MonoRail学习-入门实例篇

    1.到官方网站下载安装文件,地址如下: http://www.castleproject.org/index.php/Castle:Download目前最新版本Beta5(您也可以不需要下载,直接使用 ...

  2. Program E-- CodeForces 18C

    Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...

  3. 深入理解dispatch_queue

    Grand Central Dispatch是苹果过去几年创造出来的非常强大的API,在Let's Build系列的最新一期中,我们将探究dispatch_queue基础功能的重新实现.该主题是Rob ...

  4. unix shell-01 file

    1 一个文件有三种访问方式: 1.读,可以显示该文件的内容 2.写,删除或者编辑这个文件 3.执行,如果该文件时一个shell脚本或程序 按照文件所针对的用户,用户可以分为三种: 1.文件属主,即该文 ...

  5. JS设计模式书籍、原则

    圣经书:JavaScript 高级程序设计 设计模式:DesignPatterns by Erich Gamma.Richard Hlem.Ralph Johnson .Jhon Vlissides ...

  6. 《day16_多线程细节_Eclipse使用》

    多线程的一些细节: 1,面试题:sleep方法和wait方法异同点是什么? 相同点:可以让线程处于冻结状态. 不同点: 1, sleep必须指定时间. wait可以指定时间,也可以不指定时间. 2, ...

  7. struts2+hibernate整合开发步骤

    百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql   creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...

  8. WPF如何实现类似iPhone界面切换的效果(转载)

    WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...

  9. 程序员最爱 Mac、JS 是最热门技术

    概况: 今年,有超过5万名开发者向我们分享了他们是谁,做什么工作,以及他们的成果.通过本文,你将看到有史以来最为全面的一次开发者情况调查的结果. 每8秒钟,就会有一位开发者在Stack Overflo ...

  10. Ubuntu 14.10 下开机不进入图形化界面

    因为装的是Ubuntu 桌面版,很占资源,所以启动时候不进入图形化界面,肯定会省不少内存 进入  /etc/X11/default-display-manager  sudo nano/etc/X11 ...