Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 3160    Accepted Submission(s): 718

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

题意:求出给定图中所有桥的数量,减去缩点后的最长链,即为题中所求答案(实际不用缩点也可求)

思路参考于:http://blog.csdn.net/qq172108805/article/details/9564705

#include "stdio.h"   //用到双连通分量和树形dp的思想
#include "string.h"
#pragma comment(linker,"/STACK:102400000,102400000") //手动扩大栈区(不扩栈会运行错误) #define N 201000
#define M 1001000 struct node
{
int x,y;
bool visit; //标记该边是否走过
int next;
}edge[4*M];
int idx,head[N]; inline int MIN(int a,int b){ return a<b?a:b; } void Init()
{
idx=0;
memset(head,-1,sizeof(head));
}
void Add(int x,int y)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].visit = false; //开始时所有边都未走过
edge[idx].next = head[x];
head[x] = idx++;
} int n,m;
int sum,temp;
int low[N],dfn[N],time;
int dp1[N],dp2[N]; void DFS(int x)
{
int i,y;
dp1[x] = dp2[x] = 0;
low[x] = dfn[x] = ++time;
for(i=head[x]; i!=-1; i=edge[i].next)
{
if(edge[i].visit) continue;
y = edge[i].y;
edge[i].visit = edge[i^1].visit = true;
if(!dfn[y]) //点y未被访问过
{
DFS(y);
low[x] = MIN(low[x],low[y]);
if(low[y] > dfn[x])
sum++; //当前边为桥,sum++
temp = dp1[y];
if(low[y] > dfn[x])
temp++;
if(temp > dp1[x])
{
dp2[x] = dp1[x];
dp1[x] = temp;
}
else if(temp > dp2[x])
dp2[x] = temp;
}
else
low[x] = MIN(low[x],dfn[y]);
}
} int main()
{
int i;
int x,y;
while(scanf("%d %d",&n,&m),n||m)
{
Init();
for(i=0; i<m; ++i)
{
scanf("%d %d",&x,&y);
Add(x,y);
Add(y,x);
}
sum = 0; //统计图中桥的条数
time = 1;
memset(dfn,0,sizeof(dfn));
DFS(1);
int dist=0; //记录图的双连通分量缩点后的最长直径(最长的桥的长度)(实际不用处理缩点)
for(i=1; i<=n; ++i)
{
if(dist<dp1[i]+dp2[i]) //dp1[i]+dp2[i]为经过点i的最长路径的长度
dist = dp1[i]+dp2[i];
}
printf("%d\n",sum - dist);//所有桥的条数减最长路径的桥数,即为答案
}
return 0;
}

hdu 4612 Warm up 双连通+树形dp思想的更多相关文章

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

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

  2. HDU 2242 考研路茫茫—空调教室 (边双连通+树形DP)

    <题目链接> 题目大意: 给定一个连通图,每个点有点权,现在需要删除一条边,使得整张图分成两个连通块,问你删除这条边后,两联通块点权值和差值最小是多少. 解题分析: 删除一条边,使原连通图 ...

  3. Hdu 4612 Warm up (双连通分支+树的直径)

    题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...

  4. HDU 2242 考研路茫茫——空调教室(边双连通分量+树形dp+重边标号)

    http://acm.hdu.edu.cn/showproblem.php?pid=2242 题意: 思路:首先求一下双连通分量,如果只有一个双连通分量,那么无论断哪根管子,图还是连通的. 最后只需要 ...

  5. HDU 4612——Warm up——————【边双连通分量、树的直径】

    Warm up Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  6. HDU 4612 Warm up(2013多校2 1002 双连通分量)

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  7. HDU 5739 Fantasia 双连通分量 树形DP

    题意: 给出一个无向图,每个顶点有一个权值\(w\),一个连通分量的权值为各个顶点的权值的乘积,一个图的权值为所有连通分量权值之和. 设删除顶点\(i\)后的图\(G_i\)的权值为\(z_i\),求 ...

  8. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  9. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 了解 JS 作用域与作用域链

    (1)作用域 一个变量的作用域(scope)是程序源代码中定义的这个变量的区域. 1. 在JS中使用的是词法作用域(lexical scope) 不在任何函数内声明的变量(函数内省略var的也算全局) ...

  2. 【UWP】对 Thickness 类型属性进行动画

    好几个月没写 blog 了,一个是在忙新版的碧影壁纸,另一方面是等(观望)周年更新的 api(不过现在还是比较失望,仍然没法支持矩形以外的 Clip).闲话少说,进入主题. 在 UWP 中,出于性能考 ...

  3. (一)JAVA项目(非web项目)部署到windows服务器运行

    [转]http://blog.csdn.net/tracy19880727/article/details/11205063 一般服务器运行的几乎都是web项目,今天遇到一个问题,把写好的Java项目 ...

  4. 【jQuery基础学习】00 序

    作为一个从来没有认真学过jQuery的菜来讲,我所学的都是jQuery基础. 算是让自己从0开始系统学一遍吧.学习书籍为:<锋利的jQuery>. 虽然是个序,表示一下我是个菜,但还是来几 ...

  5. (旧)子数涵数·DW——图文混排页面

    一.首先,打开Dreamweaver,新建一个的HTML项目. 二.在设计区里,写一些文字,随便写一点(也可以在代码区中的<body>和</body>之间写). 三.插入一张图 ...

  6. Struts2中 Result类型配置详解

    一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...

  7. 优化磁盘I/O

    管理I/O,避免过度地寻道可以让硬盘更快.顺序I/O和随机I/O之间的性能差异随便就可以达到40:1,可能更多.这在数据库服务器中尤其重要,因为数据库的日志是以顺序格式写的.选择合适的硬件,合理地配置 ...

  8. WCF服务部署到IIS7.5

    下面介绍如何把WCF服务部署到IIS: 为WCF服务创建.svc文件 我们知道,每一个ASP.NET Web服务都具有一个.asmx文本文件,客户端通过访问.asmx文件实现对相应Web服务的调用.与 ...

  9. Installing FIM 2010 R2 SP1 Portal on SharePoint Foundation 2013

    http://www.fimspecialist.com/fim-portal/installing-fim-2010-r2-sp1-portal-on-sharepoint-foundation-2 ...

  10. 2015年第6本(英文第5本):Harry Potter 1 哈利波特与魔法石

    书名: Harry Potter 1 – Harry Potter and the Sorcerer’s Stone 作者:J.K. Rowling 单词数:7.8万 不重复单词数:6000(我怎么感 ...