日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時間:2023/12/13 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图论与java_算法笔记_150:图论之双连通及桥的应用(Java) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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; //給定圖的頂點數(shù)

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]) //當(dāng)邊temp為割邊(或者橋)時

ans.add(temp);

}else if(j != parent[start]) { //當(dāng)j不是start的直接父母節(jié)點時

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();

}

}

運(yùn)行結(jié)果:

7 7

1 2

2 3

3 4

2 5

4 5

5 6

5 7

2

參考資料:

總結(jié)

以上是生活随笔為你收集整理的图论与java_算法笔记_150:图论之双连通及桥的应用(Java)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。