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.
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.
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.
4 4 1 2 1 3 1 4 2 3 0 0
0
题解:非常水的一道题
1.强连通分量缩点,化成树,然后统计桥的数量。
2.求出树的直径的长度
3.答案即是桥的数量-树的直径的长度
题解:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
using namespace std;
const int MAXN = 200007;
int head[MAXN];
int DFN[MAXN];
int LOW[MAXN];
int visit[MAXN];
int visit2[MAXN];
int p[MAXN];
int stack[MAXN];
int belong[MAXN];
int d[MAXN];
int tot;
int sp;
int cnt;
int real_cnt;
int bridges;
int sccnum;
set<int> st[MAXN];
struct Es{
int u,v,id;
int next;
int cost;
}Es[2000007];
void init(){
sccnum = sp = bridges = real_cnt = tot = cnt = 0;
memset(head,-1,sizeof(head));
memset(p,-1,sizeof(p));
memset(visit,0,sizeof(visit));
memset(DFN,0,sizeof(DFN));
memset(LOW,0,sizeof(LOW));
memset(belong,0,sizeof(belong));
}
inline void add_edge(int i,int j,int id){
Es[cnt].u = i;
Es[cnt].v = j;
Es[cnt].next = head[i];
Es[cnt].id = id;
head[i] = cnt++;
}
void tarjan(int x)
{
visit[x] = 1;
stack[sp++] = x;
DFN[x]=LOW[x]=++tot;
for(int i=head[x];i!=-1;i=Es[i].next)
{
int v = Es[i].v;
if(!visit[v]){
p[v] = Es[i].id;
tarjan(v);
LOW[x] = min(LOW[x],LOW[v]);
}
else if(Es[i].id != p[x]){
LOW[x] = min(LOW[x],DFN[v]);
}
}
if(LOW[x] == DFN[x]){
sccnum++;
if(p[x] != -1)
bridges ++;
int top;
do{
top = stack[--sp];
belong[top] = sccnum;
} while(top != x);
}
return ;
}
int N,M;
void bfs(int x){
memset(visit2,0,sizeof(visit2));
memset(d,0,sizeof(d));
queue<int> Q;
Q.push(x);
visit2[x] = 1;
d[x] = 0;
while(!Q.empty()){
int u = Q.front();Q.pop();
visit2[u] = 1;
set<int>::iterator it = st[u].begin();
for(;it != st[u].end();++it){
int v = *it;
if(!visit2[v]){
d[v] = d[u] + 1;
Q.push(v);
}
}
}
}
int main(){
while(scanf("%d%d",&N,&M) && M+N){
for(int i = 0;i < MAXN;i++) st[i].clear();
init();
for(int i = 0;i < M;i++){
int a,b;
scanf("%d%d",&a,&b);
add_edge(a,b,real_cnt);add_edge(b,a,real_cnt++);
}
for(int i = 1;i <= N;i++){
if(!DFN[i]){
tarjan(i);
}
}
for(int i = 1;i <= N;i++){
for(int e = head[i];e != -1;e = Es[e].next){
int v = Es[e].v;
if(belong[v] == belong[i]) continue;
st[belong[i]].insert(belong[v]);
st[belong[v]].insert(belong[i]);
}
}
bfs(1);
int maxi = 0,vertex;
for(int i = 1;i <= sccnum;i++){
if(d[i] > maxi){
maxi = d[i];
vertex = i;
}
}
bfs(vertex);
int ans = 0;
for(int i = 1;i <= sccnum;i++){
ans = max(ans,d[i]);
}
printf("%d\n",bridges - ans);
}
return 0;
}