算法笔记_150:图论之双连通及桥的应用(Java)
2017-04-27 21:10
225 查看
1 问题描述
DescriptionIn 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
USACO 2006 January Gold
题目链接:http://poj.org/problem?id=3177
2 解决方案
具体代码如下:package com.liuzhen.practice; import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main { 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 Stack<Integer> stack; public static ArrayList<edge>[] map; public static ArrayList<edge> ans; //存储给定图中为桥的边 static class edge { public int a; //边的起点 public int b; //边的终点 public boolean used; //表示边是否已被访问 public edge(int a, int b) { this.a = a; this.b = b; this.used = false; } } @SuppressWarnings("unchecked") public void init() { count = 0; DFN = new int[n + 1]; Low = new int[n + 1]; parent = new int[n + 1]; stack = new Stack<Integer>(); map = new ArrayList[n + 1]; ans = new ArrayList<edge>(); for(int i = 1;i <= n;i++) { DFN[i] = -1; Low[i] = -1; parent[i] = -1; map[i] = new ArrayList<edge>(); } } public void TarJan(int start, int father) { 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 void getResult() { 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++; } 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 void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(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(new edge(a, b)); map[b].add(new edge(b, a)); } test.getResult(); } }
[b][b]运行结果:[/b]
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
2
[b]参考资料:[/b]
1.pku 3177 (3352) Redundant Paths
2. PKU3352(Road Construction)-图的双连通,桥
相关文章推荐
- 算法笔记_149:图论之桥的应用(Java)
- 算法笔记_144:有向图强连通分量的Tarjan算法(Java)
- 算法笔记_145:拓扑排序的应用(Java)
- java笔记--超级类Object多线程的应用+哲学家进餐算法内部类与多线程结合
- 算法笔记_146:TarJan算法的应用(Java)
- 算法笔记_147:有向图欧拉回路判断应用(Java)
- 黑马程序员________Java中IO技术字节流字符流的应用及File类学习笔记
- JAVA高级应用学习笔记----流
- JAVA 数据结构与算法学习笔记一(转载)
- Java Socket套接字应用实例-java学习笔记(1)
- Java数组扩容算法及Java对它的应用
- java笔记:SpringSecurity应用(二)
- Flex企业应用开发实践学习笔记(八)——Flex on Java企业应用架构
- Java 2 应用编程 150 例
- 图论小结(一)包括一些最短路,最小生成树,差分约束,欧拉回路,的经典题和变种题。强连通,双连通,割点割桥的应用。二分匹配,KM,支配集,独立集,还有2-SAT。
- 分布式java应用学习笔记一
- Memcached学习笔记 - 在JAVA中的应用
- Java 线程同步问题 生产者-消费者 算法实现 -Java学习笔记(29)
- java学习笔记(二) ----基本数据类型应用
- Servlet学习笔记(一)javaWeb应用基本