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. Mvc3提交表格验证(转载)

    Model层:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System ...

  2. ASP.NET WebForm中用async/await实现异步出人意料的简单

    1. 在.aspx中添加异步标记 <%@ Page Language="C#" Async="true"%> 2. 在.aspx.cs或者.ascx ...

  3. C#面向对象面试题集锦

    1.简述C#中的虚方法 答:注意:当使用virtual关键字修饰符后,不允许再同时使用abstract,static,或override关键字进行修饰 使用virtual关键字修饰的方法就是虚方法,虚 ...

  4. 他们在军训,我在搞 OI(一)

    Day 1 理论上,我现在不应该坐在电脑前打字,因为早在今天上午 6:20 全体新高一同学就坐车前往军(无)训(尽)基(炼)地(狱)了,而今天上午 6:20 我还在被窝里呢…… 没错,我旷掉了军训,然 ...

  5. 在pc游览器端模拟移动端幻灯片

    用简单的思路写了下pc端模拟web端的图片滑动效果... 效果卡,bug多,完毕,继续学习c3方法写这个,iscroll就是可以模拟这种效果,还在学习中,难点<触点判断> 代码一份 < ...

  6. MySQL建立索引的注意事项

    对于大数据量的表格,尤其是百万行以上的数据表,一定要对其建立索引,否则查询速度极慢.(参考后面的测试结果)建立索引时需注意: MySQL的索引有两种:单列索引(即在某一列上建索引).多列组合索引(即在 ...

  7. 谷歌、百度、1万ip能赚多少钱?1000IP能够值多少钱呢?

    谷歌.百度.1万ip能赚多少钱?1000IP能够值多少钱呢? (2014-04-03 11:50:52) 转载▼ 标签: 广告联盟 百度联盟 谷歌联盟 ip赚钱       很多在人问:谷歌.百度:1 ...

  8. js 基本介绍

    ecma  对象 三个包类型  String   ParseInt  ParseDouble instanceof typeof Math  对象 Array Date RegExp -- bom对象 ...

  9. schedule CCCallfunc CCCallfuncN CCCallfuncND

    schedule(schedule_selector(HelloWorld::step), 1.0f); void HelloWorld::step(float dt) { CCLog("d ...

  10. php对象引用和析构函数的关系

    在php中构造函数和析构函数都属于魔术方法,比如构造函数在某一个类中,当这个类被实例化的时候就会自动调用,而析构函数是在这个类的对象被销毁的时候自动调用,默认情况下是在程序执行结束时自动调用. 如果我 ...