poj3352添加多少条边可成为双向连通图

时间:2022-09-13 10:14:42
Road Construction
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13311   Accepted: 6715

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 这题其实就是缩点后看有多少个度为1的点。其实和poj1236 相似 有向图添加多少条边变成强连通图 这题就用来学习无向图缩点 和求度吧 这些都是基本操作
 #include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = 3e5 + ;
int n, m, u, v, tot, top, cnt, flag;
struct node {
int u, v, next;
} edge[maxn];
int head[maxn], instack[maxn], s[maxn];
int dfn[maxn], low[maxn], belong[maxn];
void init() {
tot = cnt = top = flag = ;
memset(s, , sizeof(s));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(instack, , sizeof(instack));
memset(belong, , sizeof(belong));
memset(low, , sizeof(low));
memset(edge, , sizeof(edge));
}
void add(int u, int v) {
edge[tot].v = v;
edge[tot].u = u;
edge[tot].next = head[u];
head[u] = tot++;
}
void tarjin(int v, int fa) {
dfn[v] = low[v] = ++flag;
instack[v] = ;
s[top++] = v;
for (int i = head[v] ; i != - ; i = edge[i].next) {
int j = edge[i].v;
if (j == fa) continue;
if (!instack[j]) {
tarjin(j, v);
low[v] = min(low[v], low[j]);
} else if (instack[j] == ) low[v] = min(low[v], dfn[j]);
}
if (dfn[v] == low[v]) {
cnt++;
int t;
do {
t = s[--top];
instack[t] = ;
belong[t] = cnt;
} while(t != v) ;
}
}
int du[maxn];
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
init();
memset(du, , sizeof(du));
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 (!instack[i]) tarjin(i, -);
for(int i = ; i <= tot; i += ) {
if(belong[edge[i].u] != belong[edge[i].v]) {
du[belong[edge[i].u]]++;
du[belong[edge[i].v]]++;
}
}
int ans = ;
for (int i = ; i <= cnt ; i++)
if (du[i] == ) ans++;
printf("%d\n", (ans + ) / );
}
return ;
}

poj3352添加多少条边可成为双向连通图的更多相关文章

  1. mybatis&plus;oracle添加一条数据并返回所添加数据的主键问题

    最近做mybatis+oracle项目的时候解决添加一条数据并返回所添加数据的主键问题 controller层 @RequestMapping("/addplan") public ...

  2. QTableView 添加进度条

    记录一下QTableView添加进度条 例子很小,仅供学习 使用QItemDelegate做的实现 有自动更新进度 要在.pro文件里添加 CONFIG += c++ ProgressBarDeleg ...

  3. struts2上传文件添加进度条

    给文件上传添加进度条,整了两天终于成功了. 想要添加一个上传的进度条,通过分析,应该是需要不断的去访问服务器,询问上传文件的大小.通过已上传文件的大小, 和上传文件的总长度来评估上传的进度. 实现监听 ...

  4. 新建一个DataTable如何手动给其添加多条数据!

    早晨起来,想起昨天利用winform做类似于sqlserver数据库导入数据功能的时候,用到了新建一个DataTable手动给其添加多条数据,平时用不到,需要的时候想不起来了,这次不妨把他记下来.以下 ...

  5. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  6. iOS viewController添加导航条以及返回跳转选择

    给单独的viewcontroller或者在Appdelegate的主页面添加导航条,只要在viewcontroller上添加navigationcontroller,在添加此navigationcon ...

  7. 动态sql语句,非存储过程,如何判断某条数据是否存在,如果不存在就添加一条

    已知一个表 table 里面有两个字段  A1 和 A2 如何用动态语句 判断 A1 = A , A2=B 的数据是否存在,如果不存在,就添加一条数据, A1 = A , A2 = B INSERT  ...

  8. EasyUI添加进度条

    EasyUI添加进度条 添加进度条重点只有一个,如何合理安排进度刷新与异步调用逻辑,假如我们在javascript代码中通过ajax或者第三方框架dwr等对远程服务进行异步调用,实现进度条就需要做到以 ...

  9. c&num;devexpress GridContorl添加进度条

    demo 的实现图 下边是步骤和代码 1定义 时钟事件,定时的增加进度条的增量. 2:  添加进度条 3;定义字段属性 using System; using System.Collections.G ...

随机推荐

  1. ASP&period;NET ZERO 学习 HangFire的使用

    hangfire 是一个分布式后台执行服务. 官网:http://hangfire.io/ 1.启用 hangfire 2.Hangfire可以提供一个面板页面,实时显示所有后台作业的状态,你可以按它 ...

  2. 试用Jenkins 2 的 Pipeline 项目

    目前Jenkins最新的版本是2.7,现在试用一下pipeline类型的项目,本来想构建一个1.651版本的Jenkins为例,无奈大陆的网络 访问github不稳定,只好改为简单的工程. 目前有一个 ...

  3. bk&period; 2014&period;12&period;1

    typedef void (*halKeyCback_t) (uint8 key, uint8 state) 表示定义halKeyCBack_T为指向函数的指针,该函数的特点是形参(uint8,uin ...

  4. 黑马程序员——OC语言Foundation框架 结构体

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)结构体 NSRange(location length) NSPoi ...

  5. Webx之表单验证

    引入服务器端表单验证service,是通过在webx.xml中通过服务引入的方式完成的.例如,在user相关信息的表单验证的产生过程是这样的:webx-user.xml通过 <beans:imp ...

  6. Linux文件系统类型和区别

    文件系统EXT3,EXT4和XFS的区别: 1. EXT3 (1)最多只能支持32TB的文件系统和2TB的文件,实际只能容纳2TB的文件系统和16GB的文件 (2)Ext3目前只支持32000个子目录 ...

  7. word20170102日用家电 household appliances

    1. Vacuum cleaner: 吸尘器 2.Cordless vacuum cleaner: 无线吸尘器 3.Robotic vacuum cleaner: 机器人吸尘器 动词:to vacuu ...

  8. 虚拟机Oracle VM VirtualBox linux系统如何访问windows共享文件夹

    1. 在本机系统设置一个共享文件夹,用于与Ubuntu交互的区域空间.     2.右击状态栏上共享文件夹图标或菜单栏“设备-共享文件夹”,打开共享文件夹设置,如图示   3.点击共享文件夹设置框,右 ...

  9. 【杂谈】Remember-Me的实现

    前言 此篇随笔记录了Remember-Me实现过程中出现的问题和解决方案,以及相关的思考. 正文 1. RememberMe是什么? RememberMe意为记住我,对应登录界面的那个勾选项.另一种说 ...

  10. Linq to XML操作XML文件

    LINQ的类型 在MSDN官方文件中,LINQ分为几种类型: . LINQ to Objects(或称LINQ to Collection),这是LINQ的基本功能,针对集合对象进行查询处理,包括基本 ...