Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18580   Accepted: 7711

Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample:

One visualization of the paths is:

   1   2   3
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +

Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.

   1   2   3
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -

Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
Every pair of fields is, in fact, connected by two routes.

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

Source

题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走。现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立的路。两条独立的路是指:没有公共边的路,但可以经过同一个中间顶点。

分析:在同一个边双连通分量中看做同一个点,缩点后,新图是一棵树,树的边就是原无向图的桥。

问题转化为:在树中至少添加多少条边能使图变为双连通图。

结论:添加边数=(树中度为1的节点数+1)/2

代码:

 #include<cstdio>
#include<cstring>
#include "algorithm"
using namespace std;
const int N = + ;
const int M = + ;
struct P {
int to, nxt;
} e[M * ];
int head[N], low[N], dfn[N], beg[N], du[N], st[M], ins[M];
int cnt, id, top, num; void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
} void tarjan(int u, int fa) {
low[u] = dfn[u] = ++id;
st[++top] = u;
ins[u] = ;
for (int i = head[u]; i != -; i = e[i].nxt) {
int v = e[i].to;
if (i == (fa ^ )) continue;
if (!dfn[v]) tarjan(v, i), low[u] = min(low[u], low[v]);
else if (ins[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
int v;
do {
v = st[top--];
ins[v] = ;
beg[v] = num;
} while (u != v);
num++;
}
} void init() {
cnt = id = top = num = ;
memset(head, -, sizeof(head));
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
memset(du, , sizeof(du));
} int n, m;
int main() {
scanf("%d%d", &n, &m);
init();
for (int i = ; i < m; i++){
int u, v;
scanf("%d%d", &u, &v);
add(u, v), add(v, u);
}
for (int i = ; i <= n; i++) if (!dfn[i]) tarjan(i, -);
for (int i = ; i <= n; i++) {
for (int j = head[i]; j != -; j = e[j].nxt){
int v = e[j].to;
if (beg[i] != beg[v]) du[beg[i]]++;
}
}
int ans = ;
for (int i = ; i < num; i++)
if (du[i] == ) ans++;
printf("%d\n", (ans + ) / );
return ;
}

最新文章

  1. git常用功能
  2. EventBus--出现的问题
  3. linux配置ssh互信
  4. ios之无限 自动 图片轮播器的实现
  5. spring AOP Bean添加新方法
  6. HDOJ 1995 汉诺塔V
  7. StringHelper类,内容截取,特别适合资讯展示列表
  8. HLS(HTTP Live Streaming)协议之m3u8文件生成方式
  9. ShareSDK for Android 只有新浪微博分享
  10. requests模拟登录
  11. 记一次lvs-tunnel模式的故障分析(7)
  12. WPF: WPF 中的 Triggers 和 VisualStateManager
  13. 你用.NET开发APP时,在云平台打包APP要填个“包名”的含义
  14. [Swift]LeetCode384. 打乱数组 | Shuffle an Array
  15. MVCC 能解决幻读吗?
  16. win10添加右键打开命令窗口
  17. 使用fiddler模拟session失效的测试方法
  18. python基础学习笔记(六)
  19. zoj3469 区间dp好题
  20. ts项目报错:Import sources within a group must be alphabetized

热门文章

  1. laravel5.4学习笔记
  2. RAC修改spfile位置
  3. 【2017-06-06】Qt中的键盘事件:QKeyEvent及相关处理函数
  4. March 21 2017 Week 12 Tuesday
  5. mongodb文档替换
  6. Spring 声明式事务管理方式
  7. 九、IntelliJ IDEA 编译方式介绍及编译器的设置和选择
  8. P4722 【模板】最大流
  9. 【办公】Microsoft Office 2016 专业增强版下载及永久激活-亲测分享
  10. webstorm开发nodejs的技巧--代码提示DefinitelyTyped