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. HTTP - PUT 上传文件/Shell (二)

    上一篇文章 HTTP - PUT 上传文件/Shell 讲到自己搭了一个环境,去测试HTTP - PUT上传Shell.最近又遇到几个PUT上传的例子,也成功上传了几次,来分享一下思密达. 0x00 ...

  2. asp.net中对象的序列化,方便网络传输

    对象序列化 是将对象状态转换为可保持或传输的格式的过程.反序列化 是将流转换为对象序列化和反序列化相结合 可以使对象数据轻松的存储和传递 在 .NET 中,如果是对象可序列化,需要在 声明对象的开始部 ...

  3. sqlserver 2008express版本启用混合登陆和sa

    本机环境:win10 64位  vs2010及其自带的数据库 sqlserver2008 express版本 用命令行登陆数据库: osql -E -Slocalhost\sqlexpress 登陆成 ...

  4. 将CMD内的显示内容输出到txt文件

    将CMD内的显示内容输出到txt文件 xxxx -t >c:\test.txt        //xxxx为命令  如ping www.baidu.com //-t >c:\test.tx ...

  5. 设计的SOA架构

    新来老大年前开会说各位同学,公司业务越来越重,未来几年要成倍增长......,要梳理出一套新架构,才能更好的支持N万用户.....,以后升职加薪当上....打败..... 想想还有点小激动呢,于是过年 ...

  6. 如何在Ubuntu Unity上修改应用程序图标

    转自如何在Ubuntu Unity上修改应用程序图标 这篇文章将教大家在Ubuntu Unity上修改应用程序图标,这个教程适合于Ubuntu 14.04, Ubuntu 13.10, Ubuntu ...

  7. [杂题]CSUOJ1274Balls and Boxes

    题目链接 题意:中文题 题意不多赘述 值得注意的是n<m 不必考虑n==m的情况 (m是盒子个数, n是每次选取的盒子个数, 不要弄反了!) 这题一看就是同余方程 每次选取n个盒子放球 也就是说 ...

  8. 怎样在WINDOWS下面编译LIBCURL

    我测试过,好像没OK This is a short note about building cURL with SSL support on Windows. Tools required: cUR ...

  9. 给自己加油,一定要学会MFC!

    我自己对于没有学会MFC始终耿耿于怀,都什么时代了啊,但是我仍然坚持会去学MFC,因为MFC虽然落后与复杂,但是在Windows平台上仍然是无所不能的(其实Windows平台仍然是唯一可以赚钱的平台, ...

  10. 【Python】代码行数统计

    两级目录,可扩展为N级. # Count the line of dir or file import os, fnmatch, fileinput def ChkFileType(lst): tmp ...