思路:缩点,求最长链。重点在于重边的判断。我遇到了Runtime Error(STACK_OVERFLOW)的错误,手动扩栈即解决。
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2543 Accepted Submission(s): 595
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.
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.
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
#include <cstdio>
#include <algorithm>
#include <cstring>
#pragma comment(linker,"/STACK:102400000,102400000")//手动扩栈
using namespace std;
const int N = 200005;
int n, m;
int head[N], dfn[N], low[N], belong[N];
int cnt, scc, index;
int cnt2, head2[N];
int dis[N];
int sta[4000005], top;
struct Edge{
int v, next;
}edge[2000005], edge2[2000005];
void addedge(int u, int v){
edge[cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void addedge2(int u, int v){
edge2[cnt2].v = v;
edge2[cnt2].next = head2[u];
head2[u] = cnt2++;
}
void tarjan(int u, int fa){
dfn[u] = low[u] = ++index;
sta[++top] = u;
int multi = 0;//标记重边
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(!dfn[v]){
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(fa == v){
if(multi) low[u] = min(low[u], dfn[v]);
multi++;
}
else low[u] = min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
scc++;
do
{
x = sta[top--];
belong[x] = scc;
}while(x!=u);
}
}
void DFS(int u){
for(int i = head2[u]; i != -1; i = edge2[i].next){
int v = edge2[i].v;
if(dis[v] == -1){
dis[v] = dis[u]+1;
DFS(v);
}
}
}
int main(){
while(scanf("%d %d", &n, &m), (m||n)){
top = 0;
cnt = scc = index = 0;
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(belong, 0, sizeof(belong));
for(int i = 1; i <= m; i++){
int a, b;
scanf("%d %d", &a, &b);
if(a == b) continue;
addedge(a, b);
addedge(b, a);
}
tarjan(1, -1);
//建新图
cnt2 = 0;
memset(head2, -1, sizeof(head2));
for(int u = 1; u <= n; u++){
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(belong[u] != belong[v]){
addedge2(belong[u], belong[v]);
}
}
}
memset(dis, -1, sizeof(dis));
dis[1] = 0;
DFS(1);
int tmp = 0;
int s;
for(int i = 1; i <= scc; i++){
if(tmp < dis[i]){
tmp = dis[i];
s = i;
}
}
memset(dis, -1, sizeof(dis));
dis[s] = 0;
DFS(s);
int len = 0;
for(int i = 1; i <= scc; i++){
if(len < dis[i]){
len = dis[i];
}
}
printf("%d\n", scc - len - 1);
}
return 0;
}