HDU4612 Warm up

ACM

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3532 Accepted Submission(s): 813

Problem Description

   N planets are connected by M bidirectional channels that allow instant transportation. It’s always possible to travel between any two planets through these channels.
If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
People don’t like to be isolated. So they ask what’s the minimal number of bridges they can have if they decide to build a new channel.
Note that there could be more than one channel between two planets.

Input

  The input contains multiple cases.
Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
(2<=N<=200000, 1<=M<=1000000)
Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
A line with two integers ‘0’ terminates the input.

Output

  For each case, output the minimal number of bridges after building a new channel in a line.

Sample Input

4 4 1 2 1 3 1 4 2 3 0 0

Sample Output

0

Source




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma comment(linker, "/STACK:1024000000,1024000000")//C++加栈
#include
#include
#include
#include
#include
#include
#include
#include<map>
#include
#include
#include
using namespace std;
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#define LLU "%" "I" "6" "4" "u"
#define LL_MAX _I64_MAX
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#define LLU "%" "l" "l" "u"
#define LL_MAX _I64_MAX
#endif
#define N 200005
#define M 1000005
int n, m;
const int inf = ~0u &gt;&gt; 1;
const LL lnf = ~0ull &gt;&gt; 1;
struct edge {
int v, nxt;
} e[M &lt;&lt; 1];
struct bridge {
int u, v;
bridge(int u, int v) {
this-&gt;u = u;
this-&gt;v = v;
}
};
int pos, head[N];
int top, stk[N];
int pre[N], dfs_clock, belong[N], bcnt;
vector brg;
void InitEdge() {
memset(head, -1, sizeof(head));
pos = 0;
}
void add(int u, int v) {
e[pos].v = v;
e[pos].nxt = head[u];
head[u] = pos++;
}
int dfs(int u, int fa) {
int lowu = pre[u] = ++dfs_clock;
stk[top++] = u;
bool vis = true;
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if (v == fa &amp;&amp; vis) {
vis = false; //u-v的反向边一定是v-u邻接表里的第一条边。
continue;
}
if (!pre[v]) {
int lowv = dfs(v, u);
lowu = min(lowu, lowv);
if (lowv &gt; pre[u]) { //u-v为桥
brg.push_back(bridge(u, v));
}
} else lowu = min(lowu, pre[v]);
}
if (pre[u] == lowu) {
bcnt++;
do {
belong[stk[--top]] = bcnt;
} while (stk[top] != u);
}
return lowu;
}
void tarjan() {
top = bcnt = dfs_clock = 0;
memset(pre, 0, sizeof(pre));
memset(belong, 0, sizeof(belong));
brg.clear();
for (int i = 1; i &lt;= n; i++) {
if (!pre[i]) dfs(i, -1);
}
}
void rebuild() { //根据桥来缩点重构图
InitEdge();
for (int i = 0; i &lt; (int) brg.size(); i++) {
int u = belong[brg[i].u];
int v = belong[brg[i].v];
add(u, v);
add(v, u);
}
}
int diameter = 0;
int findR(int u, int fa) {
int h1 = 0, h2 = 0; // 以u为根,h1最高的儿子,h2次高儿子。放在dfs里面两者不会重合。
for (int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if (v == fa) continue;
int h = findR(v, u) + 1; //若路径有权重,则把1改为u-v的权重
if (h &gt; h1) h2 = h1, h1 = h;
else h2 = max(h, h2);
}
diameter = max(diameter, h1 + h2);
return h1;
}
void tree_diameter() {
diameter = 0;
findR(1, -1);
}
int main() {
while (scanf("%d%d", &amp;n, &amp;m), n || m) {
int u, v;
InitEdge();
for (int i = 0; i &lt; m; i++) {
scanf("%d%d", &amp;u, &amp;v);
add(u, v);
add(v, u);
}
tarjan();
rebuild();
tree_diameter();
printf("%d\n", bcnt - diameter - 1);
}
}