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.
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
题意:
给你一个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+10, M = 2e6+10, inf = 2e9, mod = 1e9+7; 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 = 0;head[u]=t++;} void init() { memset(head,-1,sizeof(head)); scc=0;cnt=0;top=0; memset(dfn,0,sizeof(dfn)); memset(vis,0,sizeof(vis)); t=0; } void dfs(int x,int fa) { dfn[x] = low[x] = ++cnt; q[++top] = x; inq[x]=1; for(int i=head[x];i!=-1;i=G[i].next) { int to = G[i].to; if(G[i].id) continue; G[i].id = G[i^1].id=1; 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]]=0; belong[q[top]]=scc; }while(x!=q[top--]); } } void Tarjan() { for(int i=1;i<=n;i++) if(!dfn[i]) dfs(i,-1); } void rebuild() { t=0; memset(head,-1,sizeof(head)); for(int i=1;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,0,sizeof(vis)); queue<int >Q; Q.push(x); vis[x]=1; mx = 1; while(!Q.empty()) { int k = Q.front(); Q.pop(); for(int i=head[k];i!=-1;i=G[i].next) { int to = G[i].to; if(vis[to]) continue; Q.push(to); vis[to] = vis[k] + 1; if(vis[to]>mx) { mx = vis[to]; from = to; } } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; init(); for(int i=1;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=0;from = 1; bfs(1); bfs(from); printf("%d\n",scc-mx); } return 0; }
Recommend