1 问题描述

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

2 解决方案

具体代码如下:

packagecom.liuzhen.practice;importjava.util.ArrayList;importjava.util.Scanner;importjava.util.Stack;public classMain {public static int n; //给定图的顶点数

public static int count; //记录遍历次序

public static int[] DFN;public static int[] Low;public static int[] parent; //parent[i] = j,表示顶点i的直接父母顶点为j

public static Stackstack;public static ArrayList[] map;public static ArrayList ans; //存储给定图中为桥的边

static classedge {public int a; //边的起点

public int b; //边的终点

public boolean used; //表示边是否已被访问

public edge(int a, intb) {this.a =a;this.b =b;this.used = false;

}

}

@SuppressWarnings("unchecked")public voidinit() {

count= 0;

DFN= new int[n + 1];

Low= new int[n + 1];

parent= new int[n + 1];

stack= new Stack();

map= new ArrayList[n + 1];

ans= new ArrayList();for(int i = 1;i <= n;i++) {

DFN[i]= -1;

Low[i]= -1;

parent[i]= -1;

map[i]= new ArrayList();

}

}public void TarJan(int start, intfather) {

DFN[start]= count++;

Low[start]=DFN[start];

parent[start]=father;

stack.push(start);for(int i = 0;i < map[start].size();i++) {

edge temp=map[start].get(i);if(temp.used)continue;int t =temp.b;for(int p = 0;p < map[t].size();p++) {if(map[t].get(p).b ==temp.a) {

map[t].get(p).used= true;break;

}

}

temp.used= true;int j =temp.b;if(DFN[j] == -1) {

TarJan(j, start);

Low[start]=Math.min(Low[start], Low[j]);if(Low[j] > DFN[start]) //当边temp为割边(或者桥)时

ans.add(temp);

}else if(j != parent[start]) { //当j不是start的直接父母节点时

Low[start] =Math.min(Low[start], DFN[j]);

}

}

}public voidgetResult() {for(int i = 1;i <= n;i++) {if(parent[i] == -1)

TarJan(i,0);

}int[] degree = new int[n + 1];for(int i = 0;i < ans.size();i++) {int a =ans.get(i).a;int b =ans.get(i).b;

degree[a]++;

degree[b]++;

}int result = 0;for(int i = 1;i <= n;i++) {if(degree[i] == 1)

result++;

}

result= (result + 1) / 2;

System.out.println(result);return;

}public static voidmain(String[] args) {

Main test= newMain();

Scanner in= newScanner(System.in);

n=in.nextInt();int m =in.nextInt();

test.init();for(int i = 0;i < m;i++) {int a =in.nextInt();int b =in.nextInt();

map[a].add(newedge(a, b));

map[b].add(newedge(b, a));

}

test.getResult();

}

}

运行结果:

7 7

1 2

2 3

3 4

2 5

4 5

5 6

5 7

2

参考资料:

图论与java_算法笔记_150:图论之双连通及桥的应用(Java)相关推荐

  1. java 寻找和为定值的多个数_算法笔记_037:寻找和为定值的两个数(Java)

    1 问题描述 输入一个整数数组和一个整数,在数组中查找两个数,满足他们的和正好是输入的那个整数.如果有多对数的和等于输入的整数,输出任意一对即可.例如,如果输入数组[1,2,4,5,7,11,15]和 ...

  2. 强化学习经典算法笔记(十四):双延迟深度确定性策略梯度算法TD3的PyTorch实现

    强化学习经典算法笔记(十四):双延迟深度确定性策略梯度算法TD3的PyTorch实现 TD3算法简介 TD3是Twin Delayed Deep Deterministic policy gradie ...

  3. 金蝉素数c语言,算法笔记_204:第四届蓝桥杯软件类决赛真题(Java语言C组)

    前言:以下代码仅供参考,若有错误欢迎指正哦~ 1好好学习 汤姆跟爷爷来中国旅游.一天,他帮助中国的小朋友贴标语.他负责贴的标语是分别写在四块红纸上的四个大字:"好.好.学.习".但 ...

  4. 算法提高 求最大值java_算法笔记_096:蓝桥杯练习 算法提高 求最大值(Java)

    1 问题描述 问题描述 给n个有序整数对ai bi,你需要选择一些整数对 使得所有你选定的数的ai+bi的和最大.并且要求你选定的数对的ai之和非负,bi之和非负. 输入格式 输入的第一行为n,数对的 ...

  5. 算法笔记_056:蓝桥杯练习 未名湖边的烦恼(Java)

    目录 1 问题描述 2 解决方案 2.1 递归法 2.2 递推法   1 问题描述 问题描述 每年冬天,北大未名湖上都是滑冰的好地方.北大体育组准备了许多冰鞋,可是人太多了,每天下午收工后,常常一双冰 ...

  6. 算法笔记_204:第四届蓝桥杯软件类决赛真题(Java语言C组)

    目录 1 好好学习 2 埃及分数 3 金蝉素数 4 横向打印二叉树 5 危险系数 6 公式求值   前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 好好学习 汤姆跟爷爷来中国旅游.一天,他帮助中国的 ...

  7. 算法笔记_120:蓝桥杯第六届省赛(Java语言B组部分习题)试题解答

     目录 1 三角形面积 2 立方变自身 3 三羊献瑞 4 九数组分数 5 饮料换购 6 生命之树   前言:以下试题解答代码部分仅供参考,若有不当之处,还请路过的同学提醒一下~ 1 三角形面积 三角形 ...

  8. 算法笔记_202:第三届蓝桥杯软件类决赛真题(Java高职)

    目录 1 填算式 2 提取子串 3 机器人行走 4 地址格式转换 5 排日程   前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 填算式 [结果填空] (满分11分)看这个算式:☆☆☆ + ☆☆☆ ...

  9. 算法提高课-图论-有向图的强连通分量-AcWing 367. 学校网络:强连通分量、tarjan算法

    文章目录 题目解答 题目来源 题目解答 来源:acwing 分析: 第一问:通过tarjan算法求出强连通分量并且缩点后,统计入度为0的点的个数p即可. 第二问,至少加几条边才能使图变成强连通分量?这 ...

最新文章

  1. [转]expect实现ssh自动交互
  2. mysql数据备份常用命令
  3. 动态规划—最长公共子序列
  4. 《Spring2.0 技术手册》读书笔记五-与Spring容器的交互(2)
  5. cookie,session的区别和联系(补充token)
  6. [BZOJ3669] [NOI2004] 魔法森林 LCT维护最小生成树
  7. docker- 构建镜像:
  8. Android 获取cpu序列号
  9. python无法启动此程序因为_python报错:无法启动此程序,因为计算机中丢失
  10. Pdf转Word用Python轻松实现
  11. 【 信息搜集的内容,信息搜集的方法,信息搜集的工具,信息搜集结果的利用等】
  12. Linux如何修改网卡ip地址!
  13. wps序号打乱重新排序_wps序号怎么自动排列
  14. 网络:TCP的滑动窗口与流量控制和拥塞控制
  15. 撸代码更有劲了(这应该算是福利吧)
  16. Power BI(二十四)power pivot之产品/客户分类分析(ABC分析)
  17. CentOS7 中注册服务并随系统启动
  18. 学习表——受任于败军之际,奉命于危难之间(11.28-12.4)
  19. Springboot+mysql+大学生就业管理系统 毕业设计 -附源码290915
  20. 老虎证券contract和positions

热门文章

  1. springboot 别名不起作用_springboot之mybatis别名的设置
  2. 自动点击器如何设置最快_铁粉技巧 | iPhone如何设置自动开关机,iPhone更改字体...
  3. 不愿意和别人打交道_如果你的交际能力很差,不喜欢与人打交道,这3种职业最适合你...
  4. 【转】编译DCMTK
  5. 单片机与PC机一样都是计算机,51单片机与PC机通信资料
  6. android contentprovider api,Content Provider Basics
  7. Qt: 找不到Qt5Widgets.lib
  8. SparkStreaming - 无状态与有状态 updataStateByKey
  9. 【Python CheckiO 题解】Between Markers
  10. 【Processing学习】 - 公交车马路动态绘制