多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。
Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1398 Accepted Submission(s): 320
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.
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.
1 2
1 3
1 4
2 3
0 0
感想:
当时被爆栈爆到无语了。完全不知道为什么的爆栈。 这个题目讲的很清楚。。方法也很容易看出来,就是求无向图的桥的问题。问题是!他竟然无缘无故爆栈!! 心情都爆没了。超想骂数据。。后来看到标程时。。
这三条语句不知道干什么的:
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 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。的更多相关文章
- Warm up---hdu4612(缩点,树的直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥最少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...
- [HDOJ4612]Warm up(双连通分量,缩点,树直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...
- xdoj-1319 求树上任意一点的最大距离----利用树的直径
1 #include <bits/stdc++.h> using namespace std; ; vector < vector <int> > g(N); in ...
- (求树的直径)Warm up -- HDU -- 4612
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥至少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- hdu 4612 Warm up 双连通缩点+树的直径
首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...
- HDU 4612 Warm up (边双连通分量+缩点+树的直径)
<题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...
- 2015 HDU 多校联赛 5363 Key Set
2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...
- 2015 HDU 多校联赛 5317 RGCDQ 筛法求解
2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目 http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...
随机推荐
- Building bridges_hdu_4584(排序).java
Building bridges Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) ...
- [Javascript] Call Stack
Every time when a function run it will be push into the call stack and put on the top, you can thin ...
- QiniuUpload- 一个方便用七牛做图床然后插入markdown的小工具
最近一段时间有用markdown做笔记,其他都好,但是markdown插入图片挺麻烦的,特别是想截图之后直接插入的时候.需要首先把图片保存了,然后还要上传到一个地方生成链接才能插入.如果有个工具可以直 ...
- Android动态加载jar/dex
前言 在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优 ...
- http断点续传原理
断点续传一是断点,一续传. 断点是在下载时,将下载文件分多片,同时进行多片一起下载,如果任务被暂停,暂停的位置就是断点. 续传就是未完成的下载再次开始时,会从上次的断点继续传送. 在下载(或上传)过程 ...
- AngularJs练习Demo12Provider
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- 16 Socket通信(简单例子)
服务端代码: import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Da ...
- C#界面设计疑问
1.就是想做一个类似下面界面的窗体,上面一排按键,点击一个下面对应改变一次界面的内容,这是如何实现的呢 ...是不是通过,比如这里有四个按键,然后使用4个大小相同的面板,每个面板内容不同.按一个按键, ...
- pubwin 客户端会员无法自助结账的排查方法
客户端会员无法自助结账按以下方法排查:1,看客户端能不能打开web https 后台,打不开的话,在服务端打上2048证书补丁(按下面帖子操作)http://bbs.pubwin.com.cn/for ...
- python Cmd实例之网络爬虫应用
python Cmd实例之网络爬虫应用 标签(空格分隔): python Cmd 爬虫 废话少说,直接上代码 # encoding=utf-8 import os import multiproces ...