多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。

来源:互联网 发布:在阿里巴巴找淘宝客户 编辑:程序博客网 时间:2024/04/28 06:04

Warm up

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


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
 

Recommend
zhuyuanchen520
 

感想:

       当时被爆栈爆到无语了。完全不知道为什么的爆栈。  这个题目讲的很清楚。。方法也很容易看出来,就是求无向图的桥的问题。问题是!他竟然无缘无故爆栈!! 心情都爆没了。超想骂数据。。后来看到标程时。。


这三条语句不知道干什么的:

int size = 256 << 20; // 256MB/char *p = (char*)malloc(size) + size;__asm__("movl %0, %%esp\n" :: "r"(p) );

百度后发现跟什么寄存器有关系。。。可是这跟我爆栈有关系么??
当我把这几句删掉后,标程都是爆栈。。  然后我就彻底无语了。。。真的无语了。。真的真的很无语。。 我实在很佩服当时那些能AC的大牛们。。真不知道你们怎么解决爆栈的问题的。。

---------------------- 不过如今我知道原因了。  我们程序在深度递归的时候,存在一个最大的内存限制,而这个限制是1M。  所以我们需要添加特定的语句来修改默认栈值。

----------------------  by ipqhjjybj  @date 20130824

/* * @author ipqhjjybj * @date  20130727 * */#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <cmath>#include <algorithm>#include <cstring>#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))const int N=222222,M=2222222;int dfn[N],low[N],sig,ret,firstEdge[N],nextEdge[M],to[M],cnt,vst[M],dp[N][2];int n,m;void addEdge(int u,int v){    to[cnt]=v;    nextEdge[cnt]=firstEdge[u];    firstEdge[u]=cnt++;}void tarjan(int u){    dp[u][0]=dp[u][1]=0;    dfn[u]=low[u]=sig++;    for(int st=firstEdge[u];st!=-1;st=nextEdge[st]){        if(!vst[st>>1]){            vst[st>>1]=1;            int v=to[st];            if(dfn[v]==-1){                tarjan(v);                low[u]=min(low[u],low[v]);                ret+=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;            }else{                low[u]=min(low[u],dfn[v]);            }        }    }}int main(){    //freopen("1002.in","r",stdin);    int size = 256 << 20; // 256MB/char *p = (char*)malloc(size) + size;__asm__("movl %0, %%esp\n" :: "r"(p) );    while(scanf("%d %d",&n,&m) && n+m){        cnt=0;memset(firstEdge,-1,sizeof(firstEdge));        for(int i=0,a,b;i<m;i++){            scanf("%d %d",&a,&b);            addEdge(a-1,b-1);            addEdge(b-1,a-1);        }        memset(dfn,-1,sizeof(dfn));        memset(vst,0,sizeof(vst));        sig=0,ret=0;        tarjan(0);        int ans=0;        for(int i=0;i<n;i++)                ans=max(ans,dp[i][0]+dp[i][1]);        printf("%d\n",ret-ans);    }    return 0;}

本来想接着用说的思路写的,就是双连通+缩点+树最长直径来做。后来看到标程这个DP用的真的很好哇。。。省了好多代码。 其实这就是找一条直径方法比较快的浓缩了。

标准程序还是挺好的,就是爆栈的数据让我无语了。。 

恩。。接着决定继续好好学习