POJ3352(连通分量缩点)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10352 | Accepted: 5140 |
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算法将图中的双连通部分缩为一点,进而得到一棵树。那么(这棵树的叶子结点数目+1)/2 即为答案。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
bool mp[MAXN][MAXN];
int n,m;
int dfn[MAXN],low[MAXN],time;
int stack[MAXN],top;
int ins[MAXN];
int belong[MAXN],cnt;
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++time;
stack[top++]=u;
ins[u]=true;
for(int v=;v<=n;v++)
{
if(mp[u][v])
{
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(v!=fa&&ins[v]) low[u]=min(low[u],dfn[v]);
}
} if(dfn[u]==low[u])
{
int v;
cnt++;
do{
v=stack[--top];
ins[v]=false;
belong[v]=cnt;
}while(u!=v);
}
}
int deg[MAXN];
void cal()
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(mp[i][j]&&belong[i]!=belong[j])
{
deg[belong[i]]++;
deg[belong[j]]++;
}
}
int res=;
for(int i=;i<=cnt;i++)
{
if(deg[i]==)
res++;
}
printf("%d\n",(res+)/);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(ins,false,sizeof(ins));
time=;
cnt=;
memset(belong,,sizeof(belong));
memset(mp,false,sizeof(mp));
memset(deg,,sizeof(deg));
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=true;
}
tarjan(,-);
cal(); }
}
POJ3352(连通分量缩点)的更多相关文章
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- HDU 2242 连通分量缩点+树形dp
题目大意是: 所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible 这道题需要先利用tarjan算法将在同一连通分量中的点缩成一 ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- poj3177 && poj3352 边双连通分量缩点
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12676 Accepted: 5368 ...
- POJ3694 Network(边双连通分量+缩点+LCA)
题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
随机推荐
- oracle函数中lead,lag,over,partition by 的使用
lead,lag函数的分析 http://blog.csdn.net/mazongqiang/article/details/7621328 举例如下: SQL> select * from ...
- 【解决】无法连接 MKS:套接字连接尝试次数太多正在放弃
https://blog.csdn.net/wjunsing/article/details/78496224 我的电脑 -> 右键 -> 管理 -> 服务和应用程序 -> 服 ...
- 13-redis主从复制
一:redis主从复制 (1)配置要求 一台主maste 二台从slave (2)架构图 (3)配置流程 1:分配拷贝2份redis.conf 分别命名为 redis6380.conf redis63 ...
- 嵌入式开发之davinci--- 8148/8168/8127 中的xdc 简介
XDC是TI公司为嵌入式实时系统可重用软件组件(在XDC里被成为packages,以下成为包)制定的一套标准.它包括一些有用的工具,标准的API函数,静态配置文件和打包(packaging)操作.XD ...
- python 基础 2.8 python练习题
python 练习题: #/usr/bin/python #coding=utf-8 #@Time :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...
- 以python理解Linux的IO多路复用,select、poll、epoll
题外话 之前在看Unix环境高级编程的时候,看完高级IO那一章,感觉自己萌萌哒,0.0 ,有点囫囵吞枣的感觉,之后翻了几篇博客,从纯系统的角度理解,稍微有了点概念,以这两篇为例,可以以后参考: htt ...
- win10+UEFI下u盘安装ubuntu16.04
本人电脑是华硕,由于要求使用linux所以安装: 1.首先给linux划出一个大分区,感觉最少50G: win10下磁盘管理,在最后的分区中压缩出50g,空间,其他的不用问了,也不用继续分区,一个大的 ...
- Objective-c中的delegate浅析
delegate初探 在ios开发中,我们常常会用到类似例如以下的对话框: 因此,例如以下这段代码我们也就非常熟悉了: - (IBAction)showSheet:(id)sender { UIAct ...
- JDK动态proxy原理解析
转: 之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白.比如说:InvocationHandler的invoke方法是由谁来调用的,代理对象是怎么生成的,直到前几个星期才把这些问题全部搞明白 ...
- php 文件头部(header)
发布:sunday01 来源:net [大 中 小] 有关php文件头部信息(header)的详细介绍,是脚本学堂见过的最详细的一篇,有需要的朋友,千万不要错过这么好的文章. php文件头 ...