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 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
随机推荐
- 命令行BASH
shell 壳,把用户的指令翻译给内核kernel,真正工作的是内核 shell分为cli(command line interface)和gui(graphical user interface) ...
- HTML--3css样式表
CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/ 此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...
- hdu 2091
PS:PE了两次....又是这种奇怪的输出格式....两个三角形直接有空行.. 代码: #include "stdio.h" void ou(int n,char a); void ...
- hdu 2099
PS:因为还是不爽...继续水题...感觉这道题就是考输出.. 代码: #include "stdio.h" void cal(int a,int b); int main(){ ...
- GPRS Sniffing Tutorial
- Download sources into ~/gprs_sniffer git clone git://git.osmocom.org/osmocom-bb.git git clone git: ...
- Interview----求 1+2+...+n, 不能用乘除法、for、while if、else、switch、case 等关键字以及条件判断语句 (A?B:C)
题目描述: 求 1+2+...+n, 要求不能使用乘除法.for.while.if.else.switch.case 等关键字以及条件判断语句 (A?B:C). 分析: 首先想到的是写递归函数,但是遇 ...
- BZOJ 4562 食物链
我们需要拓扑一下. #include<iostream> #include<cstring> #include<cstdio> #include<algori ...
- String to Integer (atoi) ---- LeetCode 008
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- App Store审核指南:WatchKit、HealthKit、ApplePay以及HomeKit部分
将此前App Store审核指南中的WatchKit.HealthKit.ApplePay以及HomeKit部分进行了整理和摘取. 10. 用户界面 10.1 应用程序必须遵守苹果的<Apple ...
- 如何从oc中去获取一个私有的变量.....
运行时 的用法 1.定义的一个类,里面有一个私有变量mt_,并且在初始化值为"HaHa Ha ".@interface Mobj : NSObject {@privateNSStr ...