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. iOS开发摇动手势实现详解

    1.当设备摇动时,系统会算出加速计的值,并告知是否发生了摇动手势.系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你快速摇动设备三次,那只会收到一个摇动事件. ...

  2. msmms (二) sms与mms 简述!

    mms 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . MMS是英文缩写,它可以是Membership Management System的缩写,中文译名为会员管理系统.也可以是M ...

  3. thinkphp-2

    php的跨文件变量? global $g是一个脚本文件中, 函数外部的变量在函数中要使用时的 全局变量 $_GET等是所谓的"超全局变量", 但仍然是只能在一个脚本的范围内使用 要 ...

  4. SecureCRT光标颜色

    SecureCRT连linux光标一直没有,尤其是在vim编辑文档的时候特别麻烦,今天找出解决办法: 选项->会话选项->仿真:将ANSI颜色选中: 选项->会话选项->外观: ...

  5. 利用sourcemap来调试sass

    最近项目用上了sass,作为css的预处理器,它可以让我们用程序化的思维书写样式,极大的简化了css的开发,实在是前端居家旅行必备的利器. 我们都知道,在项目中,样式的频繁调试是不可避免的,用上sas ...

  6. <转载>NPOI Excel 单元格背景颜色对照表

    我转载地址:http://www.holdcode.com/web/details/117 NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 ...

  7. 表单元素的写法及与后台php的交互

    1.<select class="textEnaSty" name="Port" size="1" onchange="Ob ...

  8. C#父类子类对象关系

    案例: 主要有Vehicle.cs  Airplane.cs   Car.cs  3个类. Car和Airplane都继承与Vehicle类.Vehicle中Drive为虚方法,可在子类中重写,父类引 ...

  9. django-cms 代码研究(八)app hooks

    app钩子,啥玩意呢? 就是把现有的app,集成到cms的一种手段. 有两种实现方式: 1) 定义cms_app.py,如下: from cms.app_base import CMSApp from ...

  10. 又一款linux提权辅助工具

    又一款linux提权辅助工具 – Linux_Exploit_Suggester 2013-09-06 10:34 1455人阅读 评论(0) 收藏 举报 https://github.com/Pen ...