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
给你张无向图 让你加一条边使桥最小
思路:
直接缩点 然后树的直径
因为重边调了一下午 重边真的自闭
但是对判重边更加的深入理解 (不亏hhh)
#include<iostream>
#include<cstring>
#include<stack>
#include<stdio.h>
using namespace std;
const int maxn = 2e5 + 10;
int head[maxn];
int dfn[maxn];
int low[maxn];
int colour[maxn];
int dv[maxn];
int indu[maxn];
int dist[maxn];
int dp[maxn];
int vis[maxn];
int tot;
int tot1;
int dex;
int sum;
int cnt = 0;
int cnt1;
int n, m;
int ans;
int qiao;
struct node{
int to;
int next;
int from;
node() {}
node(int a, int b, int c) : to(a), next(b), from(c) {}
}edge[maxn * 40];
struct nod{
int to;
int next;
int w;
nod() {}
nod(int a, int b, int c) : to(a), next(b), w(c) {}
}edge1[maxn * 40];
void edgeadd(int a, int b){
edge[tot] = node(b, head[a], a);
head[a] = tot++;
edge[tot] = node(a, head[b], b);
head[b] = tot++;
}
void edgeadd1(int a, int b, int c){
edge1[tot1] = nod(b, head[a], c);
head[a] = tot1++;
edge1[tot1] = nod(a, head[b], c);
head[b] = tot1++;
}
void init(){
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(indu, 0, sizeof(indu));
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
tot = 0;
dex = 0;
sum = 0;
ans = 0;
cnt1 = 0;
tot1 = 0;
qiao = 0;
}
stack<int> s;
void tarjan(int u, int rt){
dfn[u] = low[u] = ++dex;
s.push(u);
bool f = 1;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(v == rt && f == 1){//判重边
f = 0;
continue;
}
if(!dfn[v]){
tarjan(v, u);
if(low[v] > dfn[u])
qiao++;
low[u] = min(low[u], low[v]);
}
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]){
sum++;
while(1){
int now = s.top();
colour[now] = sum;
s.pop();
if(now == u) break;
}
}
}
void dagdp(int x, int pre){
dp[x] = 0;
int mx = 0;
vis[x] = 1;//判重标记
for(int i = head[x]; i != -1; i = edge1[i].next){
int v = edge1[i].to;
//cout << v << endl;
if(vis[v])continue;//如果之前跑过 不在更新 这个bug找了好久
if(v == pre)
continue;
dagdp(v, x);
ans = max(ans, mx + dp[v] + edge1[i].w);
mx = max(edge1[i].w + dp[v], mx);
}
dp[x] = mx;
//cout << dp[x] << endl;
}
int main(){
ios::sync_with_stdio(false);
while(cin >> n >> m && n + m){
init();
int x, y;
for(int i = 1; i <= m; i++){
cin >> x >> y;
edgeadd(x, y);
}
for(int i = 1; i <= n; i++){
if(!dfn[i])
tarjan(i, i);
}
memset(head, -1, sizeof(head));
for (int i = 0; i < tot; i++) {
int o, g;
o = colour[edge[i].from];
g = colour[edge[i].to];
if(o != g){
indu[g]++;
indu[o]++;
//cout << 1 << endl;
edgeadd1(o, g, 1);
}
}
/*cout << tot1 << endl;
for(int i= 0; i <= tot1; i++)
cout << edge1[i].to << " " << edge1[i].w << endl;*/
int u = qiao;
dagdp(1, -1);
u -= ans;
cout << u << endl;
}
return 0;
}