Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4026 Accepted Submission(s): 911
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
Source
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you:
5173
5172
5171
5170
5169
实际上是很水的,昨天做到2点,一直超时,超时快20次,今天看了一下才发现,,输入m条边,,输入成n条边了。。。
还有就是会栈溢出,要加栈
题目大意:就是问增加一条边后的桥数
ac代码
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<queue>
#include<string>
#include<vector>
#include<iostream>
#define max(a,b) (a>b?a:b)
#define min(a,b) (b>a?a:b)
#define M 200005
#define N 2000010
using namespace std;
int low[M],dfn[M],stack[M],ins[M],head[M],belong[M],dis[M];
int cnt,time,taj,top,n,m,bcnt,zd,maxn,tcnt;
struct s
{
int u,v,next;
}edge[N];
vector<int>map[M];
void init()
{
memset(low,-1,sizeof(low));
memset(dfn,-1,sizeof(dfn));
memset(belong,-1,sizeof(belong));
memset(head,-1,sizeof(head));
memset(ins,0,sizeof(ins));
tcnt=bcnt=cnt=time=taj=top=0;
}
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void tarjan(int u,int pre)
{
low[u]=dfn[u]=time++;
stack[top++]=u;
ins[u]=1;
int flag=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(flag&&v==pre)
{
flag=0;
continue;
}
if(dfn[v]==-1)
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
bcnt++;
}
}
else
if(ins[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
int now;
taj++;
do
{
now=stack[--top];
ins[now]=0;
belong[now]=taj;
}while(now!=u);
}
}
void bfs(int u)
{
int i;
queue<int>q;
memset(ins,0,sizeof(ins));
ins[u]=1;
dis[u]=0;
zd=u;
maxn=-1;
q.push(u);
while(!q.empty())
{
int u=q.front();
q.pop();
for(i=0;i<map[u].size();i++)
{
int v=map[u][i];
if(!ins[v])
{
ins[v]=1;
dis[v]=dis[u]+1;
q.push(v);
if(dis[v]>maxn)
{
maxn=dis[v];
zd=v;
}
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF,n||m)
{
int i;
init();
for(i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==b)
continue;
add(a,b);
add(b,a);
}
for(i=1;i<=n;i++)
{
if(dfn[i]==-1)
tarjan(i,-1);
}
for(i=1;i<=n;i++)
{
map[i].clear();
}
for(i=0;i<cnt;i++)
{
int u,v;
u=edge[i].u;
v=edge[i].v;
if(belong[u]!=belong[v])
{
map[belong[u]].push_back(belong[v]);
map[belong[v]].push_back(belong[u]);
}
}
bfs(1);
bfs(zd);
printf("%d\n",bcnt-maxn);
}
}