HDOJ 4612 Warm up

来源:互联网 发布:qsv格式转换mp4 mac 编辑:程序博客网 时间:2024/05/01 22:06

缩点+树的直径+扩栈

Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3140    Accepted Submission(s): 715


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
 

Source
2013 Multi-University Training Contest 2
 


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cstdlib>#include <cmath>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;const int maxn=220000;struct Edge{    int from,to,next,id;}edge[maxn*10],edge2[maxn*10];int Adj[maxn],Size,n,m;int Adj2[maxn],Size2;void init(){    Size=0;    memset(Adj,-1,sizeof(Adj));}void init2(){    Size2=0;    memset(Adj2,-1,sizeof(Adj2));}void Add_Edge(int u,int v,int id){    edge[Size].from=u;    edge[Size].id=id;    edge[Size].to=v;    edge[Size].next=Adj[u];    Adj[u]=Size++;}void Add_Edge2(int u,int v){    edge2[Size2].from=u;    edge2[Size2].to=v;    edge2[Size2].next=Adj2[u];    Adj2[u]=Size2++;}int Low[maxn],DFN[maxn],Stack[maxn],Belong[maxn];int Index,top,scc;bool Instack[maxn],vis[maxn],ve[maxn*10];void Tarjan(int u,int fa){    int v;    Low[u]=DFN[u]=++Index;    Stack[top++]=u;    Instack[u]=true;    for(int i=Adj[u];~i;i=edge[i].next)    {        v=edge[i].to;        if(v==fa&&ve[edge[i].id]) continue;        ve[edge[i].id]=true;        if(!DFN[v])        {            Tarjan(v,u);            Low[u]=min(Low[u],Low[v]);        }        else        {            Low[u]=min(Low[u],DFN[v]);        }    }    if(Low[u]==DFN[u])    {        scc++;        do        {            v=Stack[--top];            Belong[v]=scc;            Instack[v]=false;        }while(v!=u);    }}void scc_solve(){    memset(DFN,0,sizeof(DFN));    memset(Instack,0,sizeof(Instack));    Index=scc=top=0;    memset(ve,0,sizeof(ve));    for(int i=1;i<=n;i++)    {        if(!DFN[i]) Tarjan(i,i);    }}struct PT{    int p,d;    PT() {}    PT(int a,int b):p(a),d(b) {}};int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n==0&&m==0) break;        init();        for(int i=0;i<m;i++)        {            int a,b;            scanf("%d%d",&a,&b);            Add_Edge(a,b,i); Add_Edge(b,a,i);        }        scc_solve();        int ans=scc-1;        if(ans==0)        {            puts("0"); continue;        }        init2();        PT stp(Belong[1],0);        PT edp(-1,-1);        for(int i=0;i<2*m;i+=2)        {            int A=edge[i].from,B=edge[i].to;            if(Belong[A]==Belong[B]) continue;            Add_Edge2(Belong[A],Belong[B]);            Add_Edge2(Belong[B],Belong[A]);        }        queue<PT> q;        memset(vis,0,sizeof(vis));        q.push(stp); vis[stp.p]=true;        while(!q.empty())        {            PT u,v;            u=q.front(); q.pop();            edp=u;            for(int i=Adj2[u.p];~i;i=edge2[i].next)            {                v.p=edge2[i].to; v.d=u.d+1;                if(vis[v.p]) continue;                vis[v.p]=true;                q.push(v);            }        }        while(!q.empty()) q.pop();        stp.p=-1,stp.d=-1;        memset(vis,0,sizeof(vis));        vis[edp.p]=true; edp.d=0;        q.push(edp);        while(!q.empty())        {            PT u,v;            u=q.front(); q.pop();            stp=u;            for(int i=Adj2[u.p];~i;i=edge2[i].next)            {                v.p=edge2[i].to; v.d=u.d+1;                if(vis[v.p]) continue;                vis[v.p]=true;                q.push(v);            }        }        printf("%d\n",ans-stp.d);    }    return 0;}




0 0