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
 
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[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用的真的很好哇。。。省了好多代码。 其实这就是找一条直径方法比较快的浓缩了。

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

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

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

  1. Warm up---hdu4612(缩点,树的直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥最少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...

  2. [HDOJ4612]Warm up(双连通分量,缩点,树直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...

  3. xdoj-1319 求树上任意一点的最大距离----利用树的直径

    1 #include <bits/stdc++.h> using namespace std; ; vector < vector <int> > g(N); in ...

  4. (求树的直径)Warm up -- HDU -- 4612

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥至少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...

  5. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  6. hdu 4612 Warm up 双连通缩点+树的直径

    首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...

  7. HDU 4612 Warm up (边双连通分量+缩点+树的直径)

    <题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...

  8. 2015 HDU 多校联赛 5363 Key Set

    2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...

  9. 2015 HDU 多校联赛 5317 RGCDQ 筛法求解

    2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...

随机推荐

  1. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

  2. http status 源码

    private static readonly String[][] s_HTTPStatusDescriptions = new String[][] { null, new String[] { ...

  3. 工程与科学数值方法的Matlab实现

    %stats.m function [mean,stdev]=stats(x) n=length(x);mean=sum(x)/n;stdev=sqrt(sum((x-mean).^2/(n-1))) ...

  4. 未能加载文件或程序集 system.data.sqlite 完美解决

    错误提示如下图所示: 解决办法: 使用SQLITE 预编译的静态链接DLL 下载地址:http://pan.baidu.com/s/1kT5i8bP

  5. RouteHttpMap要添加的引用

    System.Web.Routing.RouteCollection' does not contain a definition for 'MapHttpRoute' 此错的解决方式是添加 Syst ...

  6. ORACLE SQL单行函数(一)【weber出品必属精品】

    1.SUBSTR:求父串中的子串 SUBSTR('HelloWorld',1,5) 1:代表子串的起始位置,如果为正,正数,如果为负,倒数 5:代表字串的终止位置,只能向右数,可以省略,如果省略就是数 ...

  7. va_list/va_start/va_arg/va_end深入分析

    http://www.cnblogs.com/justinzhang/archive/2011/09/29/2195969.html

  8. mysql忘记密码的处理方式(整理非原创)

    方案1.通过跳过授权的方式 1.修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的中加上:skip-grant-tables . 2.重新启动mysqld # ubun ...

  9. Codeforces Round #350 (Div. 2)A,B,C,D1

    A. Holidays time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  10. Couchbase 使用方法

    1.数据流向 List<模型>  数据-->MsgPack 打包成byte[]-> couchbase 实例调用 Store(Enyim.Caching.Memcached.S ...