【Tarjan缩点】PO3352 Road Construction
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 12532 | Accepted: 6309 |
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
Sample Input 1
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 Sample Input 2
3 3
1 2
2 3
1 3
Sample Output
Output for Sample Input 1
2 Output for Sample Input 2
0
题意
就是说求最少加多少边能使一个无向图去任意一条边后仍然联通……
题解
刷水题有益身心健康~
就是一个简单的Tarjan缩点求最后只连一条边的点的个数
记得要(ans+1)/2~
代码
//by 减维
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
#define ll long long
#define maxn
using namespace std; struct edge{
int fr,to,ne;
}e[]; int n,m,ecnt,cnt,ans,tot,num,dfn[],low[],map[];
int head[],zhan[],bian[];
bool pd[]; void tarjan(int x,int fa)
{
dfn[x]=low[x]=++num;
zhan[++tot]=x;
pd[x]=;
//for(int i=1;i<=n;++i)printf("%d ",dfn[i]);printf("\n");
//for(int i=1;i<=n;++i)printf("%d ",low[i]);printf("\n");
//for(int i=1;i<=n;++i)printf("%d ",map[i]);printf("\n");
for(int i=head[x];i;i=e[i].ne)
{
int dd=e[i].to;
//printf("%d\n",dd);
if(dd==fa)continue;
if(!dfn[dd]){
tarjan(dd,x);
low[x]=min(low[x],low[dd]);
}else if(pd[dd]){
low[x]=min(dfn[dd],low[x]);
}
}
if(dfn[x]==low[x]){
cnt++;
int t;
do{
t=zhan[tot--];
map[t]=cnt;
pd[t]=;
}while(t!=x);
}
} void add(int x,int y)
{
e[++ecnt].to=y;
e[ecnt].fr=x;
e[ecnt].ne=head[x];
head[x]=ecnt;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;++i)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=;i<=n;++i)if(!dfn[i])tarjan(i,);
//for(int i=1;i<=n;++i)printf("%d ",dfn[i]);printf("\n");
//for(int i=1;i<=n;++i)printf("%d ",low[i]);printf("\n");
//for(int i=1;i<=n;++i)printf("%d ",map[i]);printf("\n");
for(int i=;i<=ecnt;i+=)
{
int x=e[i].fr,y=e[i].to;
if(map[x]!=map[y])bian[map[x]]++,bian[map[y]]++;
}
memset(pd,,sizeof(pd));
ans=;
for(int i=;i<=n;++i)
if(!pd[map[i]]&&bian[map[i]]==)ans++,pd[map[i]]=;
//else if(!pd[map[i]]&&bian[map[i]]==0)ans+=2,pd[map[i]]=1;
printf("%d",(ans+)/);
}
【Tarjan缩点】PO3352 Road Construction的更多相关文章
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- POJ3352 Road Construction Tarjan+边双连通
题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去 ...
- POJ3352 Road Construction(边双连通分量)
...
- POJ3352 Road Construction (双连通分量)
Road Construction Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ P3352 Road Construction 解题报告
P3352 Road Construction 描述 这几乎是夏季,这意味着它几乎是夏季施工时间!今年,负责岛屿热带岛屿天堂道路的优秀人士,希望修复和升级岛上各个旅游景点之间的各种道路. 道路本身也很 ...
- 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率
2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1638 Solved: 433[Submit][Statu ...
- 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP
1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 787 Solved: 318[Submit][Stat ...
随机推荐
- Android酷炫有用的开源框架
一.代码库 1.from 代码家 整理比較好的源代码连接 一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才開始支持的,ActionBarSher ...
- 使用boost::multi_index高速构建排行榜
使用boost::multi_index高速构建排行榜 前几天在boost的maillist上看到boost::multi_index将要支持ranked_index(邮件内容见附件2),这实乃我等苦 ...
- HLJU 1223: 寻找区间和 (交替推进法)
1223: 寻找区间和 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 13 Solved: 4 [Submit][Status][pid=1223& ...
- 【 全干货 】5 分钟带你看懂 Docker !
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者丨唐文广:腾讯工程师,负责无线研发部地图测试. 导语:Docker,近两年才流行起来的超轻量级虚拟机,它可以让你轻松完成持续集成.自动交付 ...
- mybatis关联
title: mybatis关联 date: 2017-12-18 18:00:30 tags: - [mybatis] categories: - [编程] - [开发工具] permalink: ...
- IdentityServer(12)- 使用 ASP.NET Core Identity
IdentityServer具有非常好的扩展性,其中用户及其数据(包括密码)部分你可以使用任何想要的数据库进行持久化. 如果需要一个新的用户数据库,那么ASP.NET Core Identity是你的 ...
- MySQL 导入外部数据时报错:1153: Got a packet bigger than 'max_allowed_packet' 解决方案
MySQL 导入外部数据时报错:1153: Got a packet bigger than 'max_allowed_packet' 解决方案 zoerywzhou@163.com http://w ...
- jsp中EL表达式不起作用的问题1
问题:在jsp页面中使用el表达式取值,取不到值,但是使用jsp中嵌套java代码可以取到值,对应代码如下: 解决: 只要在 jsp中 头文件中写上 : <%@page isELIgnored= ...
- java-生成任意格式的json数据
最近研究java的东西.之前靠着自己的摸索,实现了把java对象转成json格式的数据的功能,返回给前端.当时使用的是 JSONObject.fromObject(object) 方法把java对象换 ...
- 17089 最大m子段和
17089 最大m子段和 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC;VC Description "最大m子段和 ...