多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。
2013-07-27 21:05
288 查看
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 4
1 2
1 3
1 4
2 3
0 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的大牛们。。真不知道你们怎么解决爆栈的问题的。。
/*
* @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
,low
,sig,ret,firstEdge
,nextEdge[M],to[M],cnt,vst[M],dp
[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用的真的很好哇。。。省了好多代码。 其实这就是找一条直径方法比较快的浓缩了。
标准程序还是挺好的,就是爆栈的数据让我无语了。。
恩。。接着决定继续好好学习
相关文章推荐
- 多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。
- 多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。
- 纠结了很久,不知道为什么除了什么原因,用任何方查都查不到根源,后来ArrayList存放了Bitmap,结果出错了,终于找到了事情的根源,一阵无语,不能存放竟静态的,为什么不提示呢, 害我以为是那些自
- hdu 4612 Warm up 有重边缩点+树的直径
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
- hdu 4612 Warm up(缩点+树直径)
- F - Warm up - hdu 4612(缩点+求树的直径)
- hdoj 4612 Warm up 【无向图边双连通求桥数 缩点后求树的直径 桥数-树的直径】
- HDU4612 Warm up(强联通分量+缩点重构图后求树的直径)
- HDU4612 Warm up 边双(重边)缩点+树的直径
- HDU 4612--Warm up 【无向图边双连通求桥数 && 缩点后重建图求树的直径】
- HDU 4612 Warm up (缩点+树的直径,有重边)
- 网络教室 树上不同直径的数目
- 2015 多校联赛 ——HDU5414()
- 补交:最最原始的第一次作业(当时没有选上课,所以不知道)
- 互联登录获取用户信息时导入Constans包1直不对不知道啥原因
- 17暑假多校联赛6.8 HDU 6103 Kirinriki
- 问题解决了,原因不知道
- 2016HDU多校联赛-HDU-5734-Acperience(模拟分数运算)
- 2016多校联赛2D Eureka(hdu 5738)