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. Android Fragment详解(三): 实现Fragment的界面

    为fragment添加用户界面: Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layout ...

  2. jquery之批量上传图片

    //var btn; /** * * 获取当前时间 */ ==================================js=================================== ...

  3. SuperSocket学习笔记(二)

    上一篇博客SuperSocket学习笔记(一)说明了怎么快速搭建一个服务器端,这篇文章我想深挖一下SuperSocket 1. 每一个客户端连接到服务器端时,服务器端会将客户端的信息保存到一个Sess ...

  4. oracle中简单查询语句的格式及执行顺序分析

    一条简单的查询sql格式如下: SELECT ... FROM .... [WHERE ...] --过滤单行 [GROUP BY ...   [HAVING ...]]--GROUP BY对前面wh ...

  5. 翻译一篇SpiderMonkey GC的文章

    前言 这篇文章包含了对SpiderMonkey中Rooted<T>, Handle<T>的解释. 翻译自 https://developer.mozilla.org/en-US ...

  6. CODEVS 3279 奶牛的健美操

    3279 奶牛健美操 USACO  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description Farmer John为了保持奶牛们的 ...

  7. Linux上安装JDK

    1.下载rpm文件并安装 rpm -ivh jdk-7u51-linux-x64.rpm 2.修改/etc/profile文件,增加以下配置 export JAVA_HOME=/usr/java/jd ...

  8. CSS Reset方法

    CSS Reset 即重设浏览器的样式.在各种浏览器中,都会对CSS的选择器默认一些数值,譬如当h1没有被设置数值时,显示一定大小. 但并不是所有的浏览器都使用一样的数值,所以,有了CSS Reset ...

  9. Funny String

    def main(): t = int(raw_input()) for _ in xrange(t): s = raw_input().strip() s_len = len(s) is_funny ...

  10. 搬寝室(HDU 1421 DP)

    搬寝室 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...