HDU 4612 Warm up 边双连通+树的直径

Warm upTime Limit: 10000/5000 MS (Java/Others)Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4454Accepted Submission(s): 1009

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 41 21 31 42 30 0

Sample Output

0

Author

SYSU

Source

2013 Multi-University Training Contest 2

很巧的是,在看这道题时,叉姐群里正好在讨论双连通判重的问题^w^

确实不需要bin神题解中的每边加一bit用来记录重边,代码美如画

/** Author: ☆·aosaki(*’(OO)’*) niconiconi★ **/#pragma comment(linker, "/STACK:1024000000,1024000000")//#include<bits/stdc++.h>#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <algorithm>#include <functional>#include <cmath>#include <vector>#include <queue>#include <map>#include <set>//#include <tuple>#define ALL(v) (v).begin(),(v).end()#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)#define SIZE(v) ((int)(v).size())#define mem(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define lp(k,a) for(int k=1;k<=a;k++)#define lp0(k,a) for(int k=0;k<a;k++)#define lpn(k,n,a) for(int k=n;k<=a;k++)#define lpd(k,n,a) for(int k=n;k>=a;k–)#define sc(a) scanf("%d",&a)#define sc2(a,b) scanf("%d %d",&a,&b)#define lowbit(x) (x&(-x))#define ll long long#define pi pair<int,int>#define vi vector<int>#define PI acos(-1.0)#define pb(a) push_back(a)#define mp(a,b) make_pair(a,b)#define TT cout<<"*****"<<endl;#define TTT cout<<"********"<<endl;inline int gcd(int a,int b){return a==0?b:gcd(b%a,a);}using namespace std;const int MAXN=200010;const int MAXM=2000010;struct Side{int to,next;bool cut;} e[MAXM];int head[MAXN],tot;int low[MAXN],dfn[MAXN],stack[MAXN],belong[MAXN];int tm,top;int block;bool instack[MAXN];int bridge;void add(int u,int v){e[tot].to=v;e[tot].next=head[u];e[tot].cut=0;head[u]=tot++;}void tarjan(int u,int pre){int v;low[u]=dfn[u]=++tm;stack[top++]=u;for(int i=head[u];~i;i=e[i].next){v=e[i].to;if((i^1)==pre) continue;if(!dfn[v]){tarjan(v,i);low[u]=min(low[u],low[v]);if(low[v]>dfn[u]){e[i].cut=true;e[i^1].cut=true;}}else if(low[u]>dfn[v])low[u]=dfn[v];}if(low[u]==dfn[u]){block++;for(;;){v=stack[–top];belong[v]=block;if(v==u) break;}}}void init(){mem(dfn);mem(instack);tm=top=block=0;tot=0;mem1(head);}vector<int>vec[MAXN];int dep[MAXN];void dfs(int u){for(int i=0;i<vec[u].size(); i++){int v=vec[u][i];if(dep[v]!=-1)continue;dep[v]=dep[u]+1;dfs(v);}}void solve(int n){tarjan(1,0);lp(i,block)vec[i].clear();lp(i,n)for(int j=head[i];~j;j=e[j].next)if(e[j].cut){vec[belong[i]].push_back(belong[e[j].to]);}mem1(dep);dep[1]=0;dfs(1);int k=1;lp(i,block)if(dep[i]>dep[k])k=i;mem1(dep);dep[k]=0;dfs(k);int re=0;lp(i,block)re=max(re,dep[i]);printf("%d\n",block-re-1);}int main(){freopen("in.txt","r",stdin);int n,m;int u,v;while(~sc2(n,m) && n!=0){init();lp0(i,m){sc2(u,v);if(u==v) continue;add(u,v);add(v,u);}solve(n);}return 0;}

,午餐,晚餐。或许吃得不好,可是却依旧为对方擦去嘴角的油渍。

HDU 4612 Warm up 边双连通+树的直径

相关文章:

你感兴趣的文章:

标签云: