Warm up(hdu4612双连通分量+缩点+树的直径)

来源:互联网 发布:网络疯传婚礼新娘吃 编辑:程序博客网 时间:2024/05/12 12:08

/*

http://acm.hdu.edu.cn/showproblem.php?pid=4612

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 1808    Accepted Submission(s): 415

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

0

 

Source

2013 Multi-University Training Contest 2

 

Recommend

zhuyuanchen520

参考来自:http://www.cnblogs.com/wangfang20/archive/2013/07/25/3215313.html

 解析:

 题意:给出一个图,求加上一条边后得到的桥最少、

 思路:

 1求无向图的双连通分量,bbcn

 2 将其连通分量缩点

 3.构造一棵树求树的直径ans,最终结果bccn-1-ans;

Judge Status Exe.Time Exe.Memory Code Len. Language

2013-07-29 23:10:50 Accepted 890MS 41912K 2791 B C++

*/

方法一:


#pragma comment(linker, "/STACK:1024000000,1024000000")//若果不加这条语句话会栈溢出#include<stdio.h>#include<string.h>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<iostream>using namespace std;const int maxn=210110;const int maxm=2501000;struct  Edge{int s;int t;int flag;int next;}edge[maxm];int low[maxn],bcc[maxn],vis[maxn],pre[maxn],head1[maxn],head2[maxn],st[maxn];int n1,n2,ans,dfsc,bccn,top;int min(int a,int b){return a<b? a:b;}int max(int a,int b){return a>b? a:b;}void init(){memset(low,0,sizeof(low));memset(bcc,0,sizeof(bcc));memset(vis,0,sizeof(vis));memset(pre,0,sizeof(pre));memset(st,0,sizeof(st));memset(head1,-1,sizeof(head1));memset(head2,-1,sizeof(head2));top=n1=n2=ans=dfsc=bccn=0;}void add1(int s,int t)//建图{edge[n1].flag=0;edge[n1].s=s;edge[n1].t=t;edge[n1].next=head1[s];head1[s]=n1++;edge[n1].flag=0;edge[n1].s=t;edge[n1].t=s;edge[n1].next=head1[t];head1[t]=n1++;}void add2(int s,int t)//建树{edge[n2].flag=0;edge[n2].s=s;edge[n2].t=t;edge[n2].next=head2[s];head2[s]=n2++;edge[n2].flag=0;edge[n2].s=t;edge[n2].t=s;edge[n2].next=head2[t];head2[t]=n2++;}void Tarjan(int u)//求双连通分量{pre[u]=low[u]=++dfsc;st[top++]=u;;for(int i=head1[u];i!=-1;i=edge[i].next)  {  int v=edge[i].t;  if(edge[i].flag)continue;  edge[i].flag=edge[i^1].flag=1;  if(!pre[v])  {   Tarjan(v);   low[u]=min(low[u],low[v]);  }  //else if(!bcc[v])  //{   low[u]=min(low[u],pre[v]);  //}  }  if(low[u]==pre[u])  {  bccn++;     for(;;)     {     int x=st[--top];     bcc[x]=bccn;     if(x==u)     break;     }  }}int treelen(int u)//求树的直径{    vis[u]=1;    int i,temp=0,m1=0,m2=0;    for(i=head2[u];i!=-1;i=edge[i].next)    {        if(!vis[edge[i].t])//如果该结点没有被访问        {            temp=treelen(edge[i].t);            if(temp+1>=m1)            {                m2=m1;                m1=temp+1;            }            else            {                if(temp+1>m2)                    m2=temp+1;            }            if(m2+m1>ans)                ans=m2+m1;        }    }    return m1;}void solve(){    int i,u,v,ed;    Tarjan(1);    ed=n1;     n2=0;    for(i=0;i<ed;i+=2)//缩点后建树    {        u=bcc[edge[i].s];        v=bcc[edge[i].t];        add2(u,v);    }    memset(vis,0,sizeof(vis));    ans=0;    treelen(1);    printf("%d\n",bccn-1-ans);}int main(){int n,m,i,u,v;while(scanf("%d%d",&n,&m)!=EOF){if(n==0&&m==0)break;init();for(i=0;i<m;i++){scanf("%d%d",&u,&v);add1(u,v);//建图}solve();}return 0;}方法二:连通分量+树形DP;#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 222222;const int M = 2222222;const int INF = 111111111;int n, m, edgeCnt, firstEdge[N], to[M], nextEdge[M], dfnCnt, dfn[N], low[N], dp[N][2], result;bool visit[M];void addEdge(int u, int v){    to[edgeCnt] = v;    nextEdge[edgeCnt] = firstEdge[u];    firstEdge[u] = edgeCnt ++;}void dfs(int u){    dfn[u] = low[u] = dfnCnt ++;    dp[u][0] = dp[u][1] = 0;    for(int iter = firstEdge[u]; iter != -1; iter = nextEdge[iter]){        if(not visit[iter >> 1]){            visit[iter >> 1] = true;            int v = to[iter];            if(dfn[v] == -1){                dfs(v);                result += dfn[u] < low[v];                int temp = dp[v][0] + (dfn[u] < low[v]);                if(temp > dp[u][0]){                    dp[u][1] = dp[u][0];                    dp[u][0] = temp;                }else if(temp > dp[u][1]){                    dp[u][1] = temp;                }                low[u] = min(low[u], low[v]);            }else{                low[u] = min(low[u], dfn[v]);            }        }    }}int main(){int size = 256 << 20; // 256MBchar *p = (char*)malloc(size) + size;__asm__("movl %0, %%esp\n" :: "r"(p) );    while(scanf("%d%d", &n, &m) == 2 and n + m){        result = 0;        edgeCnt = 0;        memset(firstEdge, -1, sizeof(firstEdge));        for(int i = 0; i < m; ++ i){            int a, b;            scanf("%d%d", &a, &b);            addEdge(a - 1, b - 1);            addEdge(b - 1, a - 1);        }        dfnCnt = 0;        memset(dfn, -1, sizeof(dfn));        memset(visit, 0, sizeof(visit));        dfs(0);         int temp = 0;        for(int i = 0; i < n; ++ i){//            printf("%d: %d %d\n", i + 1, dp[i][0], dp[i][1]);            temp = max(temp, dp[i][0] + dp[i][1]);        }        printf("%d\n", result - temp);        //  printf("%d %d\n",result, temp);    }    return 0;}