POJ3352-Road Construction(边连通分量)
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, vand 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 题解:
给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图。
裸题吧,只需要边双联通后,判断统计度为一的点,然后(cnt+1)/2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm> using namespace std; const int N=;
vector<int>g[N];
int dfn[N],low[N],dg[N],tim;
bool vis[N],map[N][N];
int n,r; void tarjan(int u,int fa)
{
dfn[u]=low[u]=++tim;
vis[u]=;
for (int i=;i<g[u].size();i++)
{
int v=g[u][i];
if (v==fa) continue;
if (!dfn[v])
{
tarjan(v, u);
low[u]=min(low[u],low[v]);
}
else if (vis[v]) low[u]=min(low[u],dfn[v]);
}
}
void init()
{
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
tim=;
tarjan(,-);
}
int main()
{
while (~scanf("%d%d",&n,&r))
{
for (int i=;i<=n;i++)
g[i].clear();
memset(map,,sizeof(map));
int u,v;
for (int i=;i<r;i++)
{
scanf("%d%d",&u,&v);
if (!map[u][v])
{
g[u].push_back(v);
g[v].push_back(u);
map[u][v]=map[v][u]=;
}
} init();
memset(dg,,sizeof(dg)); for (int u=;u<=n;u++)
for (int i=;i<g[u].size();i++)
{
int v=g[u][i];
if (low[u]!=low[v]) dg[low[u]]++;
} int cnt=;
for (int i=;i<=n;i++)
if (dg[i]==) cnt++;
printf("%d\n",(cnt+)/);
}
}
POJ3352-Road Construction(边连通分量)的更多相关文章
- 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
[POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...
- POJ3352 Road Construction(边双连通分量)
...
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- POJ3352 Road Construction Tarjan+边双连通
题目链接:http://poj.org/problem?id=3352 题目要求求出无向图中最少需要多少边能够使得该图边双连通. 在图G中,如果任意两个点之间有两条边不重复的路径,称为“边双连通”,去 ...
- [POJ3352]Road Construction(缩点,割边,桥,环)
题目链接:http://poj.org/problem?id=3352 给一个图,问加多少条边可以干掉所有的桥. 先找环,然后缩点.标记对应环的度,接着找桥.写几个例子就能知道要添加的边数是桥的个数/ ...
- 边双联通问题求解(构造边双连通图)POJ3352(Road Construction)
题目链接:传送门 题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图 题目思路:tarjan算法加缩点,缩点后求出度数为1的叶子节点个数,需要加边数为(leaf+1)/2 #include ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
随机推荐
- Abp Framework中文文档上线
感谢 ABP框架中国小组 给我们带来的ABP中文翻译,Web+为方便广大学习爱好者随时查阅,现推出了Gitbook风格的在线阅读文档:http://www.webplus.org.cn/documen ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第三天(非原创)
文章大纲 一.课程介绍二.简单功能实现三.图片上传功能实战四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的使用. ...
- SpringBoot2.1.3修改tomcat参数支持请求特殊符号
最近遇到一个问题,比如GET请求中,key,value中带有特殊符号,请求会报错,见如下URL: http://xxx.xxx.xxx:8081/aaa?key1=val1&a.[].id=1 ...
- Program received signal SIGILL, Illegal instruction
Program received signal SIGILL, Illegal instruction 这个错误,发现是直接在printf 的%s中直接使用string类型,而没有使用c字符串格式造成 ...
- darknet+opencv在windows上的编译
darknet 源码网站:https://github.com/pjreddie/darknet 技术支持官网:https://pjreddie.com/darknet/ darknet采用C++编写 ...
- window下phpstudy开启redis扩展
注:一定要注意自己PHP的版本结构是64还是32位的!其次查看PHP Extension Build是NTS or TS! 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本(特 ...
- 基于C++11的call wrapper
要在C++中应用AOP,不像在其他的基于解释器的语言中那么方便,作为一种静态语言,如果给函数或者类的方法造一个wrapper,在wrapper里面嵌入调用前的代码和调用后的代码,也能达到一定程度的代码 ...
- mybatis 返回值
转载: 在使用ibatis插入数据进数据库的时候,会用到一些sequence的数据,有些情况下,在插入完成之后还需要将sequence的值返回,然后才能进行下一步的操作. 使用ibatis的 ...
- matlab中数据类型
在MATLAB中有15种基本数据类型,分别是8种整型数据.单精度浮点型.双精度浮点型.逻辑型.字符串型.单元数组.结构体类型和函数句柄.这15种基本数据类型具体如下. 有符号整数型:int8,int1 ...
- Laravel Mix编译前端资源
目前项目是使用的vue+laravel来写的,其中laravel和vue分别放了一个目录,但是这样有个问题,那就是vue需要经常更新,不然运行项目会经常出现各种问题,这里就看了看laravel的文档, ...