Warm up


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条边的无向图,你可以连接任意两个点形成新边,求连边后最小的桥的数量

题解:

  最直接的思路就是先缩环构成一个新的图

  要让桥的数量最少

  显然连接新图中链最长的两个点

  图论的基础题

  但是注意 求最长链dfs会超时,尽量用bfs

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std; const int N = 2e6+, M = 2e6+, inf = 2e9, mod = 1e9+; int n,m,dfn[N],low[N],top,vis[N],cnt,from,head[N],t,xx[N],yy[N],q[N],inq[N],mx,belong[N],scc,all; struct ss{int to,next,id;}G[M];
void add(int u,int v) {G[t].next=head[u];G[t].to=v;G[t].id = ;head[u]=t++;} void init()
{
memset(head,-,sizeof(head));
scc=;cnt=;top=;
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
t=;
} void dfs(int x,int fa) {
dfn[x] = low[x] = ++cnt;
q[++top] = x;
inq[x]=;
for(int i=head[x];i!=-;i=G[i].next) {
int to = G[i].to;
if(G[i].id) continue;
G[i].id = G[i^].id=;
if(!dfn[to]) {
dfs(to,x);
low[x] = min(low[x],low[to]);
}
else if(inq[to])low[x] = min(low[x],dfn[to]);
}
if(low[x]==dfn[x]) {
scc++;
do {
inq[q[top]]=;
belong[q[top]]=scc;
}while(x!=q[top--]);
}
} void Tarjan() {
for(int i=;i<=n;i++)
if(!dfn[i]) dfs(i,-);
} void rebuild()
{
t=;
memset(head,-,sizeof(head));
for(int i=;i<=m;i++)
{
int x = xx[i];
int y = yy[i];
if(belong[x]==belong[y]) continue;
add(belong[x],belong[y]);
add(belong[y],belong[x]);
// cout<<"dsadas "<<belong[x]<<" "<<belong[y]<<endl;
}
} void bfs(int x)
{
memset(vis,,sizeof(vis));
queue<int >Q;
Q.push(x);
vis[x]=;
mx = ;
while(!Q.empty())
{
int k = Q.front();
Q.pop();
for(int i=head[k];i!=-;i=G[i].next)
{
int to = G[i].to;
if(vis[to]) continue;
Q.push(to);
vis[to] = vis[k] + ;
if(vis[to]>mx) { mx = vis[to];
from = to;
}
}
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
init();
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
xx[i]=a;
yy[i]=b;
}
Tarjan();
rebuild();
mx=;from = ;
bfs();
bfs(from);
printf("%d\n",scc-mx);
}
return ;
}
Recommend

HDU 4612 Warm up tarjan缩环+求最长链的更多相关文章

  1. hdu 4612 Warm up(缩点+树上最长链)

    本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...

  2. HDU 4612 Warm up (边双连通分量+DP最长链)

    [题意]给定一个无向图,问在允许加一条边的情况下,最少的桥的个数 [思路]对图做一遍Tarjan找出桥,把双连通分量缩成一个点,这样原图就成了一棵树,树的每条边都是桥.然后在树中求最长链,这样在两端点 ...

  3. HDU 4612 Warm up —— (缩点 + 求树的直径)

    题意:一个无向图,问建立一条新边以后桥的最小数量. 分析:缩点以后,找出新图的树的直径,将这两点连接即可. 但是题目有个note:两点之间可能有重边!而用普通的vector保存边的话,用v!=fa的话 ...

  4. cf374C Inna and Dima dfs判环+求最长链

    题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...

  5. Grouping ZOJ - 3795 (tarjan缩点求最长路)

    题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...

  6. HDU 4612 Warm up tarjan 树的直径

    Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...

  7. hdu 4612 Warm up 桥缩点

    4612Warm hdu up 题目:给出一个图,添加一条边之后,问能够在新图中得到的最少的桥的数量. 分析:我们可以双联通分量进行缩点,原图变成了一棵树.问题变成了:求树中添加一条边之后,使得不在圈 ...

  8. HDU 4612 Warm up 连通图缩点

    题目大意:给出一个连通图,求再一个边后,剩余的最少桥数. 题目思路:首先进行缩点得到重构后的图,求出重构后树的直径(通过两次BFS求出相距最远的两点间的距离),ans=重构图边数-树的直径 //#pr ...

  9. HDU 4612 Warm up(Tarjan)

    果断对Tarjan不熟啊,Tarjan后缩点,求树上的最长路,注意重边的处理,借鉴宝哥的做法,开标记数组,标记自己的反向边. #pragma comment(linker, "/STACK: ...

随机推荐

  1. WebService中方法的相关注意事项

    2014-11-14 在WebService中定义方法,有一些注意的地方: (1) 方法上面需要增加 [WebMethod] 属性,标志该方法是一个WebService方法: (2)方法的返回值可以为 ...

  2. CentOS 7 使用经验(更新中)

    首先说一下写这篇博客的初衷. 由于公司这一期的产品准备支持的环境有CentOS 7.MySql 5.6.Java 8.Tomcat 8等等,并且因为人员严重不足,我本月的开发任务在原有的基础上又加上了 ...

  3. Dynamic Virtual Channels

    refer http://blogs.msdn.com/b/rds/archive/2007/09/20/dynamic-virtual-channels.aspx An important goal ...

  4. JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)

    截取字符串: var str = "1234567890"; var a = str.substring(0,8);    //==str.substring(8)---结果:12 ...

  5. C语言产生随机数

    rand产生随机数 #include"stdio.h" #include"stdlib.h" void main() { int i; for(i=0;i< ...

  6. 陷阱~EF中的Update与Insert共用一个数据上下文

    事情是这样的,有一个列表,里面有很多用户信息,可能会有重复的用户,将这个列表的用户插入到数据表中,如果用户已经存在,就更新这个用户的FillTimes 字段,让它加1,使用的底层ORM是entity ...

  7. 浏览器兼容性小整理和一些js小问题(后面会继续更新)

    最近在啃jQuery的源码,估计会啃到很多浏览器兼容性的问题,所以整理一下 1,IE下的内存泄露. 在IE中不在DOM树中的独立节点有javascript变量引用它的时候不会被回收. 解决:手动将该j ...

  8. Spark Streaming和Flume-NG对接实验

    Spark Streaming是一个新的实时计算的利器,而且还在快速的发展.它将输入流切分成一个个的DStream转换为RDD,从而可以使用Spark来处理.它直接支持多种数据源:Kafka, Flu ...

  9. Android/iOS微信6.3.5同时发布更新 支持群视频聊天、群公告

    下午微信6.3.5发布更新,新版最大变化就是支持群视频聊天,又一次向手机QQ靠拢.在群管理方面,支持发布群公告,支持群主转让给其他群成员,同样都是QQ玩剩下的功能.另外,新版支持微信运动查看步数图表. ...

  10. PHP引用(&)初探:函数的引用返回

    函数的引用返回 先看代码: <?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } ...