【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 ...
随机推荐
- 使用storyboard设置button边框属性(颜色,宽度,圆角)
通常使用Category时.仅仅能加入方法,不可加入属性.可是在使用Storyboard时我们可能会使用到keyPath,这里设置的key都须要是所设置视图的属性值.而且类型有所限制. 比如:我如今有 ...
- poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)
链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...
- springMVC上传错误StandardMultipartHttpServletRequest
异常信息 java.lang.ClassCastException: org.springframework.security.web.servletapi.HttpServlet3RequestFa ...
- python_web----------数据可视化从0到1的过程
一.数据可视化项目配置 1. django + Echarts 2. 服务器(linux:Ubuntu 17.04 (GNU/Linux 4.10.0-40-generic x86_64)) 3. I ...
- 判断DataTable某一列是否是时间列
DataTable dt = new DataTable("Table0"); dt.Columns.Add("column0", System.Type.Ge ...
- Pycharm配置(三)
1.准备工作 (1)Python版本为2.7或者更高版本 (2)已经创建了一个Python工程并且添加了内容,具体参考: Getting Started tutorial2.第一步——运行代码 打开之 ...
- Spring框架(四)AOP面向切面编程
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- OC学习16——对象归档
转载自 OC学习篇之---归档和解挡 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOutputStream来进行操作的.当然在操作的这些对象都 ...
- APP上传APP Store遇到的各种问题
内容含敏感话题或对苹果不友好的信息(如苹果婊) 使用了友盟的统计SDK,获取了IDFA但是上传填写无广告 采用友盟IDFA的sdk,并用友盟的默认淘宝页面广告,被告知和产品内容不符(最近) App在i ...
- SAP RFC介绍:关于sRFC,aRFC,tRFC,qRFC和bgRFC
大概八月份的时候做过一个有关两个SAP系统的财务集成的项目,使用到了RFC(Remote Function Call)技术.因为之前有着医疗-CRM相关接口开发的经验,以为自己对RFC很熟悉了,做起来 ...