HDU 4612 Warm up tarjan缩环+求最长链

Warm up


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
 
 

题意:

  给你一个n点m条边的无向图,你可以连接任意两个点形成新边,求连边后最小的桥的数量

题解:

  最直接的思路就是先缩环构成一个新的图

  要让桥的数量最少

  显然连接新图中链最长的两个点

  图论的基础题

  但是注意 求最长链dfs会超时,尽量用bfs

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std; const int N = 2e6+, M = 2e6+, inf = 2e9, mod = 1e9+; int n,m,dfn[N],low[N],top,vis[N],cnt,from,head[N],t,xx[N],yy[N],q[N],inq[N],mx,belong[N],scc,all; struct ss{int to,next,id;}G[M];
void add(int u,int v) {G[t].next=head[u];G[t].to=v;G[t].id = ;head[u]=t++;} void init()
{
memset(head,-,sizeof(head));
scc=;cnt=;top=;
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
t=;
} void dfs(int x,int fa) {
dfn[x] = low[x] = ++cnt;
q[++top] = x;
inq[x]=;
for(int i=head[x];i!=-;i=G[i].next) {
int to = G[i].to;
if(G[i].id) continue;
G[i].id = G[i^].id=;
if(!dfn[to]) {
dfs(to,x);
low[x] = min(low[x],low[to]);
}
else if(inq[to])low[x] = min(low[x],dfn[to]);
}
if(low[x]==dfn[x]) {
scc++;
do {
inq[q[top]]=;
belong[q[top]]=scc;
}while(x!=q[top--]);
}
} void Tarjan() {
for(int i=;i<=n;i++)
if(!dfn[i]) dfs(i,-);
} void rebuild()
{
t=;
memset(head,-,sizeof(head));
for(int i=;i<=m;i++)
{
int x = xx[i];
int y = yy[i];
if(belong[x]==belong[y]) continue;
add(belong[x],belong[y]);
add(belong[y],belong[x]);
// cout<<"dsadas "<<belong[x]<<" "<<belong[y]<<endl;
}
} void bfs(int x)
{
memset(vis,,sizeof(vis));
queue<int >Q;
Q.push(x);
vis[x]=;
mx = ;
while(!Q.empty())
{
int k = Q.front();
Q.pop();
for(int i=head[k];i!=-;i=G[i].next)
{
int to = G[i].to;
if(vis[to]) continue;
Q.push(to);
vis[to] = vis[k] + ;
if(vis[to]>mx) { mx = vis[to];
from = to;
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
init();
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
xx[i]=a;
yy[i]=b;
}
Tarjan();
rebuild();
mx=;from = ;
bfs();
bfs(from);
printf("%d\n",scc-mx);
}
return ;
}
Recommend
上一篇:SqlServer中把结果集放到到临时表的方法


下一篇:泛函编程(36)-泛函Stream IO:IO数据源-IO Source & Sink