天天看點

POJ 3352 Road Construction 使得無向圖邊變雙連通圖

road construction

time limit: 2000ms

memory limit: 65536k

total submissions: 8168

accepted: 4106

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 output

source

給你一個無向圖,至少添加幾條邊,使其變為雙連通圖?

縮點之後重新構圖,就會形成一顆樹,問題就變為:在樹上至少添加多少條邊,使其變為雙連通圖。

公式:至少增加的邊數=( 這棵樹總度數為1的結點數 + 1 )/ 2。

一個有橋的連通圖,如何把它通過加邊變成邊雙連通圖?方法為首先求出所有的橋,然後删除這些橋邊,剩下的每個連通塊都是一個雙連通子圖。把每個雙連通子圖收縮為一個頂點,再把橋邊加回來,最後的這個圖一定是一棵樹,邊連通度為1。

統計出樹中度為1的節點的個數,即為葉節點的個數,記為leaf。則至少在樹上添加(leaf+1)/2條邊,就能使樹達到邊二連通,是以至少添加的邊數就是(leaf+1)/2。具體方法為,首先把兩個最近公共祖先最遠的兩個葉節點之間連接配接一條邊,這樣可以把這兩個點到祖先的路徑上所有點收縮到一起,因為一個形成的環一定是雙連通的。然後再找兩個最近公共祖先最遠的兩個葉節點,這樣一對一對找完,恰好是(leaf+1)/2次,把所有點收縮到了一起。