Warm up

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5093    Accepted Submission(s):
1131

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
 
题意:n个点m条边,问新建一条边,可以让桥的数量达到最少,输出最少的桥数
 
题解:先求出所有的桥的数量,将桥连接的各个图进行缩点,根据树的直径的求法,求出最长的路径,用桥数量减去树的直径
//scc代表 双联通分量  写习惯了 顺手就写成scc了也懒得改了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#define MAXM 2000100
#define MAX 200100
#define INF 0x7fffff
using namespace std;
int n,m,bridge,sum;
int low[MAX],dfn[MAX];
int head[MAX],ans,age;
int sccno[MAX];//代表当前点属于哪个双连通分量
int dfsclock,scccnt;
vector<int>newmap[MAX];//储存缩点后的新图
int instack[MAX];//标记当前点是否入栈
int dis[MAX];//求树的直径时记录路径的长度
int vis[MAX];//求树的直径时标记是否入队列
stack<int>s;
struct node
{
int beg,end,next;
}edge[MAXM];
void init()
{
ans=0;
bridge=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
}
void tarjan(int u,int fa)
{
int v,i;
low[u]=dfn[u]=++dfsclock;
instack[u]=1;
s.push(u);
int flag=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(flag&&v==fa)//判重边
{
flag=0;
continue;
}
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
bridge++;
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
scccnt++;
while(1)
{
v=s.top();
s.pop();
instack[v]=0;
sccno[v]=scccnt;
if(v==u)
break;
}
}
}
void find()
{
int i;
memset(low,0,sizeof(low));
memset(sccno,0,sizeof(sccno));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
dfsclock=scccnt=0;
for(i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i,-1);
}
}
void suodian()
{
int u,v,i,j;
for(i=1;i<=scccnt;i++)
newmap[i].clear();
for(i=0;i<ans;i=i+2)
{
u=sccno[edge[i].beg];
v=sccno[edge[i].end];
if(u!=v)
{
newmap[u].push_back(v);
newmap[v].push_back(u);
}
}
}
void bfs(int beg)
{
queue<int>q;
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
int i,j;
while(!q.empty())
q.pop();
sum=0;
age=beg;
vis[beg]=1;
q.push(beg);
int u;
while(!q.empty())
{
u=q.front();
q.pop();
for(i=0;i<newmap[u].size();i++)
{
if(!vis[newmap[u][i]])
{
dis[newmap[u][i]]=dis[u]+1;
vis[newmap[u][i]]=1;
q.push(newmap[u][i]);
if(sum<dis[newmap[u][i]])
{
sum=dis[newmap[u][i]];
age=newmap[u][i];
}
}
}
}
}
void solve()
{
int i,j;
bfs(1);
bfs(age);
printf("%d\n",bridge-sum);
//printf("%d\n%d\n",bridge,sum);
}
int main()
{
while(scanf("%d%d",&n,&m),n|m)
{
init();
getmap();
find();
//printf("%d#\n",bridge);
suodian();
solve();
}
return 0;
}

  

 

hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】的更多相关文章

  1. hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

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

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

  3. HDU 1827 Summer Holiday(tarjan求强连通分量+缩点构成新图+统计入度+一点贪心思)经典缩点入门题

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. hdu 4612 Warm up 双连通+树形dp思想

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

  5. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

  6. HDU-4612 Warm up,tarjan求桥缩点再求树的直径!注意重边

    Warm up 虽然网上题解这么多,感觉写下来并不是跟别人竞争访问量的,而是证明自己从前努力过,以后回头复习参考! 题意:n个点由m条无向边连接,求加一条边后桥的最少数量. 思路:如标题,tarjan ...

  7. 2013杭州网赛 1001 hdu 4738 Caocao's Bridges(双连通分量割边/桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:有n座岛和m条桥,每条桥上有w个兵守着,现在要派不少于守桥的士兵数的人去炸桥,只能炸一条桥 ...

  8. poj3694+hdu2460 求桥+缩点+LCA/tarjan

    这个题使我更深理解了TARJAN算法,题意:无向图,每添加一条边后文桥的数量,三种解法:(按时间顺序),1,暴力,每每求桥,听说这样能过,我没过,用的hash判重,这次有俩个参数(n->10w, ...

  9. hdu4738(边双连通分量,桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. php分页类的二种调用方法(转载)

    php分页类的二种调用方法 原文地址:http://www.xfcodes.com/php/fenye/25584.htm 导读:php分页类的二种调用用法,ajax调用php分页类,非ajax方式调 ...

  2. 从内部剖析C# 集合之--Dictionary

    Dictionary和hashtable用法有点相似,他们都是基于键值对的数据集合,但实际上他们内部的实现原理有很大的差异, 先简要概述一下他们主要的区别,稍后在分析Dictionary内部实现的大概 ...

  3. BZOJ 3956 Count 解题报告

    好点对的个数是\(O(n)\)的,而且我们可以 \(O(n)\) 地求出所有好点对. 我们把这些点对以右端点为关键字从小到大排序,再弄个扫描线,每次把右端点在扫描线上的点对的左端点加入线段树,于是我们 ...

  4. Winform datagridview相关操作

    datagridview显示行号的2种方法: 方法一: 网上最常见的做法是用DataGridView的RowPostPaint事件在RowHeaderCell中绘制行号: privatevoiddat ...

  5. 1101-Trees on the Level

    描述 Trees are fundamental in many branches of computer science. Current state-of-the art parallel com ...

  6. php抓取页面的几种方法详解

    本篇文章是对php抓取页面的几种方法进行了详细的分析介绍,需要的朋友参考下 在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求 ...

  7. MySql 命令积累

    一. 修改表的自增起点 ; 二.获取自增键值 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID ...

  8. DHTMLX 前端框架 建立你的一个应用程序 教程(八)-- 添加表单Form

    添加表单Form 我们下一步是在页面中添加一个表单,表格中的选中字段将会显示在表单中.提供一个提交按钮 可以对显示的数据进行修改提交. 添加表单到布局单元格中 1.在右侧布局中使用attachForm ...

  9. JavaWeb学习总结(三十五)——使用JDBC处理Oracle大数据

    一.Oracle中大数据处理 在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了.因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种 ...

  10. UNICODE并没有提供对诸如Braille, Cherokee, Ethiopic, Khmer, Mongolian, Hmong, Tai Lu, Tai Mau文字的支持

    UNICODE支持欧洲.非洲.中东.亚洲(包括统一标准的东亚象形汉字和韩国象形文字).但是,UNICODE并没有提供对诸如Braille, Cherokee, Ethiopic, Khmer, Mon ...