【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 ...
随机推荐
- COCOS学习笔记--关于使用cocostudio打安卓包
我在之前的博客里也写到过,如今cocos引擎提供了一键打包.很方便. 今天正好做个測试,想用引擎的一键打包弄个apk的安卓包.以下就简介一下详细过程和自己的一些理解: 先说一下项目背景,因为我的电脑是 ...
- 深入了解MyBatis返回值
深入了解MyBatis返回值 想了解返回值,我们须要了解resultType,resultMap以及接口方法中定义的返回值. 我们先看resultType和resultMap resultType和r ...
- 原生js实现一个简单的倒计时功能
大家好,我是云中君!欢迎大家来观看我的博客 之前那,在群里看到很多人问,关于电商网站中的倒计时功能怎么实现,很多人说在网上找了很多插件,但是不是很会用,所以今天就在这里分享一下我封装的一个小的倒计时功 ...
- 利用AndroidStudio开发java工程
1.新建一个project,或者如果你已经有project的话,那就直接新建一个module.注意选择Java library,然后下一步. 2.输入module的一些信息.点击finish.(在创建 ...
- 为 Azure IoT Edge 设备部署 Azure Stream Analytics 服务
在前面的两篇文章<Azure IoT Edge on Windows 10 IoT Core>和<Azure IoT Edge on Raspberry Pi 3 with Rasp ...
- iOS 计时器三种定时器的用法NSTimer、CADisplayLink、GCD
原文:http://www.cocoachina.com/ios/20160919/17595.html DEMO链接
- php面向对象4
成员属性 基本介绍 成员属性是类的一个组成部分,一般是基本数据类型,也可以是复合类型,资源类型.比如 public $age;就是成员属性 说明 ①成员属性可以是php的任何数据类型(int, flo ...
- VMware的一些总结
一.虚拟主机联网的三种方式: 1.仅主机(Host Only),虚拟主机只能与宿主机联网通信,无法访问外网和宿主机所在局域网的其它主机. 2.桥接(Bridge),在桥接模式下,虚拟主机就像是宿主机所 ...
- [数据结构]C语言队列的实现
我个人把链表.队列.栈分为一类,然后图.树分为一类.(串不考虑),分类的理由就是每一类有规律可循,即你能通过修改极少数的代码把链表变成队列.栈.(这里我们不考虑其他诸如设计模式等因素),因此本贴在讲完 ...
- Panel控件的使用
我们对控件进行分组的原因不外乎三个: 1.为了获得清晰的用户界面而将相关的窗体元素进行可视化分组. 2.编程分组,如对单选按钮进行分组. 3.为了在设计时将多个控件作为一个单元来移动. 在vb.net ...