poj3352
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 7980 | Accepted: 4014 |
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
题意:给出一个图求出至少添加多少边才能将其变为一个双联通图。
sl:缩点之后得到一个DAG,求出DAG图所有的叶子节点,可以通过low数组记录下每个节点所属的联通分量,然后
枚举每个节点的子节点low值是不是一样,不一样则其中一个节点的入度加1然后就是图论的小知识点了。
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<vector>
5 using namespace std;
6 const int MAX = ;
7 vector<int> G[MAX];
8 int pre[MAX],low[MAX],deg[MAX];
9 int dfs_clock;
void add_edge(int from,int to)
{
G[from].push_back(to);
G[to].push_back(from);
}
int dfs(int u,int fa)
{
int lowu=pre[u]=++dfs_clock;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
}
else if(pre[u]>pre[v]&&v!=fa)
{
lowu=min(lowu,pre[v]);
}
}
low[u]=lowu;
// printf("%d\n",lowu);
return lowu;
}
void solve(int n)
{
memset(pre,,sizeof(pre));
memset(low,,sizeof(low));
memset(deg,,sizeof(deg)); int ans=;
dfs(,-);
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
// printf("!!%d %d\n",low[i],low[G[i][j]]);
if(low[i]!=low[G[i][j]])
deg[low[i]]++;
}
}
for(int i=;i<=n;i++)
if(deg[i]==) ans++;
ans=(ans+)>>;
printf("%d\n",ans);
}
int main()
{
int n,m; int a,b;
while(scanf("%d %d",&n,&m)==)
{
for(int i=;i<=n;i++) G[i].clear();
dfs_clock=;
for(int i=;i<m;i++)
{
scanf("%d %d",&a,&b);
add_edge(a,b);
}
solve(n);
}
return ;
}
poj3352的更多相关文章
- [POJ3352]Road Construction
[POJ3352]Road Construction 试题描述 It's almost summer time, and that means that it's almost summer cons ...
- 【POJ3352】Road Construction(边双联通分量)
题意:给一个无向图,问最少添加多少条边后能使整个图变成双连通分量. 思路:双连通分量缩点,缩点后给度为1的分量两两之间连边,要连(ans+1) div 2条 low[u]即为u所在的分量编号,flag ...
- poj3177 && poj3352 边双连通分量缩点
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12676 Accepted: 5368 ...
- POJ3352 Road Construction(边双连通分量)
...
- POJ3352 Road Construction (双连通分量)
Road Construction Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- [POJ3352]Road Construction(缩点,割边,桥,环)
题目链接:http://poj.org/problem?id=3352 给一个图,问加多少条边可以干掉所有的桥. 先找环,然后缩点.标记对应环的度,接着找桥.写几个例子就能知道要添加的边数是桥的个数/ ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...
- poj3352添加多少条边可成为双向连通图
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13311 Accepted: 671 ...
- poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解
题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...
- 边双联通问题求解(构造边双连通图)POJ3352(Road Construction)
题目链接:传送门 题目大意:给你一副无向图,问至少加多少条边使图成为边双联通图 题目思路:tarjan算法加缩点,缩点后求出度数为1的叶子节点个数,需要加边数为(leaf+1)/2 #include ...
随机推荐
- bzoj 1710: [Usaco2007 Open]Cheappal 廉价回文【区间dp】
只要发现添加一个字符和删除一个字符是等价的,就是挺裸的区间dp了 因为在当前位置加上一个字符x就相当于在他的对称位置删掉字符x,所以只要考虑删除即可,删除费用是添加和删除取min 设f[i][j]为从 ...
- robotframework - create dictionary 操作
1.创建字典 2.从字典中获取的项 -- 打印出 item 3.获取字典的key -- 打印出 key 4.获取字典的value -- 打印出 value 5.获取字典key,value 6.打印出字 ...
- [COCI2006-2007 Contest#3] BICIKLI
不难的一道题,就是码的时候出了点问题,看了其他巨佬的题解才发现问题所在... 题目大意: 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答 ...
- 思维/构造 HDOJ 5353 Average
题目传送门 /* 思维/构造:赛后补的,当时觉得3题可以交差了,没想到这题也是可以做的.一看到这题就想到了UVA_11300(求最小交换数) 这题是简化版,只要判断行不行和行的方案就可以了,做法是枚举 ...
- 使用JS分页 <span> beta 3.0 完成封装的分页
<html> <head> <title>分页</title> <style> #titleDiv{ width:500px; backgr ...
- 聊聊MyBatis缓存机制
https://tech.meituan.com/mybatis_cache.html 前言 MyBatis是常见的Java数据库访问层框架.在日常工作中,开发人员多数情况下是使用MyBatis的默认 ...
- SQL server 查询语句 练习题
用SQL语句创建四个表: create database tongjigouse tongjigocreate table student(Sno varchar(20) not null prima ...
- Modbus通讯错误检测方法
标准的Modbus串行网络采用两种错误检测方法.奇偶校验对每个字符都可用,帧检测(LRC和CRC)应用于整个消息.它们都是在消息发送前由主设备产生的,从设备在接收过程中检测每个字符和整个消息帧. 用户 ...
- Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1
记录一次傻逼的问题, 自己把自己蠢哭:Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1 错误说明: 返回的1是影响的行数,并不是自增的主键id: 想要获取自增主键id,需要 ...
- CAD设置水印
主要用到函数说明: _DMxDrawX::Watermark 设置控件水印图片显示,字符串用逗号隔开,分为: “文件名,透明度,x方向距离,y方向距离,是否居中”, 是否居中0代表在上角定位,1表示自 ...