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+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#devexpress GridContorl添加进度条

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

随机推荐

  1. c语言,文件操作总结

    C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程序就可用此FILE指针来 ...

  2. Swift内部类调用外部类方法、属性的变通

    Swift的内部类比较鸡肋,在内部类里面不能调用外部类的属性或方法,那么如何解决这个问题,把内部类里面调用外部类的那部分代码方法移动到外部类里面,成为外部类的方法,是一种变通解决方式.

  3. WebSocket 和 Golang 实现聊天功能

    http://www.open-open.com/lib/view/open1416379948711.html 这个示例应用程序展示了如何使用 WebSocket, Golang 和 jQuery  ...

  4. [开源]基于ffmpeg和libvlc的视频剪辑、播放器

    [开源]基于ffmpeg和libvlc的视频剪辑.播放器 以前研究的时候,写过一个简单的基于VLC的视频播放器.后来因为各种项目,有时为了方便测试,等各种原因,陆续加了一些功能,现在集成了视频播放.视 ...

  5. require.js添加css文件实现代码:css.min.js

    define( function () { if (typeof window == "undefined")return { load: function (n, r, load ...

  6. python笔记:#005#算数运算符

    算数运算符 计算机,顾名思义就是负责进行 数学计算 并且 存储计算结果 的电子设备 目标 算术运算符的基本使用 01. 算数运算符 算数运算符是 运算符的一种 是完成基本的算术运算使用的符号,用来处理 ...

  7. 国内各大支付平台的API地址

    1丶目前国内最火的支付平台--蚂蚁金服开放平台(支付宝) https://open.alipay.com/platform/home.htm 2丶国内游戏帝国--腾讯(微信支付) https://pa ...

  8. Java父类对象调用子类实体:方法重写与动态调用

    众所周知Java的handle和C++的ponter而不是object对应,我们很熟悉C++的父类pointer调用子类实体的例子,那么对于Java的handle是不是也可以这样呢? 这里我先给一个例 ...

  9. Java中static关键字和final关键字

    static: 1. 修饰变量,方法 表示静态方法,静态变量. 2. static修饰代码块 static{ } 此种形式为静态代码块,用于初始化同时被final static修饰的变量.(当然,更常 ...

  10. Binary Search 的递归与迭代实现及STL中的搜索相关内容

    与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search. 首先介绍一下binary search,其原理很直接,不断地选取 ...