Warm up

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

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
 
Source
 
Recommend
zhuyuanchen520
 

问加一条边,最少可以剩下几个桥。

先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥。

本题要处理重边的情况。

如果本来就两条重边,不能算是桥。

还会爆栈,只能C++交,手动加栈了

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std; const int MAXN = ;//点数
const int MAXM = ;//边数,因为是无向图,所以这个值要*2 struct Edge
{
int to,next;
bool cut;//是否是桥标记
bool cong;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~block
int Index,top;
int block;//边双连通块数
bool Instack[MAXN];
int bridge;//桥的数目 void addedge(int u,int v,bool pp)
{
edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut=false;
edge[tot].cong = pp;
head[u] = tot++;
} void Tarjan(int u,int pre,bool ff)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u];i != -;i = edge[i].next)
{
v = edge[i].to;
if(v == pre && (!ff))continue;
if( !DFN[v] )
{
Tarjan(v,u,edge[i].cong);
if( Low[u] > Low[v] )Low[u] = Low[v];
if(Low[v] > DFN[u])
{
bridge++;
edge[i].cut = true;
edge[i^].cut = true;
}
}
else if( Instack[v] && Low[u] > DFN[v] )
Low[u] = DFN[v];
}
if(Low[u] == DFN[u])
{
block++;
do
{
v = Stack[--top];
Instack[v] = false;
Belong[v] = block;
}
while( v!=u );
}
}
void init()
{
tot = ;
memset(head,-,sizeof(head));
} int du[MAXN];//缩点后形成树,每个点的度数
vector<int>vec[MAXN];
int dep[MAXN];
void dfs(int u)
{
for(int i = ;i < vec[u].size();i++)
{
int v = vec[u][i];
if(dep[v]!=-)continue;
dep[v]=dep[u]+;
dfs(v);
}
}
void solve(int n)
{
memset(DFN,,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
Index = top = block = ;
Tarjan(,,false);
for(int i = ;i <= block;i++)
vec[i].clear();
for(int i = ;i <= n;i++)
for(int j = head[i];j != -;j = edge[j].next)
if(edge[j].cut)
{
vec[Belong[i]].push_back(Belong[edge[j].to]);
}
memset(dep,-,sizeof(dep));
dep[]=;
dfs();
int k = ;
for(int i = ;i <= block;i++)
if(dep[i]>dep[k])
k = i;
memset(dep,-,sizeof(dep));
dep[k]=;
dfs(k);
int ans = ;
for(int i = ;i <= block;i++)
ans = max(ans,dep[i]);
printf("%d\n",block--ans);
}
struct NN
{
int u,v;
}node[MAXM];
bool cmp(NN a,NN b)
{
if(a.u != b.u)return a.u<b.u;
else return a.v<b.v;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
int u,v;
while(scanf("%d%d",&n,&m)==)
{
if(n== && m==)break;
init();
for(int i = ;i < m;i++)
{
scanf("%d%d",&u,&v);
if(u==v)continue;
if(u>v)swap(u,v);
node[i].u = u;
node[i].v = v;
}
sort(node,node+m,cmp);
for(int i = ;i < m;i++)
{
if(i == || (node[i].u!=node[i-].u || node[i].v != node[i-].v))
{
if(i < m- && (node[i].u==node[i+].u && node[i].v == node[i+].v))
{
addedge(node[i].u,node[i].v,true);
addedge(node[i].v,node[i].u,true);
}
else
{
addedge(node[i].u,node[i].v,false);
addedge(node[i].v,node[i].u,false);
}
}
}
solve(n);
}
return ;
}

HDU 4612 Warm up(2013多校2 1002 双连通分量)的更多相关文章

  1. Hdu 4612 Warm up (双连通分支+树的直径)

    题目链接: Hdu 4612 Warm up 题目描述: 给一个无向连通图,问加上一条边后,桥的数目最少会有几个? 解题思路: 题目描述很清楚,题目也很裸,就是一眼看穿怎么做的,先求出来双连通分量,然 ...

  2. HDU 4667 Building Fence(2013多校7 1002题 计算几何,凸包,圆和三角形)

    Building Fence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)To ...

  3. 【HDU 4612 Warm up】BCC 树的直径

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 题意:一个包含n个节点m条边的无向连通图(无自环,可能有重边).求添加一条边后最少剩余的桥的数 ...

  4. HDU 4612——Warm up——————【边双连通分量、树的直径】

    Warm up Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  6. HDU 4704 Sum (2013多校10,1009题)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  7. HDU 4699 Editor (2013多校10,1004题)

    Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  8. HDU 4696 Answers (2013多校10,1001题 )

    Answers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  9. HDU 4690 EBCDIC (2013多校 1005题 胡搞题)

    EBCDIC Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Su ...

随机推荐

  1. grub rescue修复引导项

    1.需要先找到linux系统盘所在到目录 grub rescue > ls 然后依次 ls (hd0,msdosX)/ 假如我们到系统在msdos2 2.输入 set root=(hd0,msd ...

  2. ASP.NET MVC 4 WebAPI. Support Areas in HttpControllerSelector

    This article was written for ASP.NET MVC 4 RC (Release Candidate). If you are still using Beta versi ...

  3. MVC+Ef项目(3) 抽象数据库访问层的统一入口;EF上下文线程内唯一

    抽象一个数据库访问层的统一入口(类似于EF的上下文,拿到上下文,就可以拿到所有的表).实际这个入口就是一个类,类里面有所有的仓储对应的属性.这样,只要拿到这个类的实例,就可以点出所有的仓储,我们在 R ...

  4. Git之 基本常用命令

    Git基本常用命令如下: mkdir:         XX (创建一个空目录 XX指目录名) pwd:          显示当前目录的路径. git init          把当前的目录变成可 ...

  5. 【同行说技术】Android图片处理技术资料汇总(一)

    对于Android开发的童鞋们来说,图片处理时或多或少都会遇到令人头疼和不满意的问题,今天小编收集了5篇Android图片处理的干货文章,一起来看看吧! 一.Android 高清加载巨图方案 拒绝压缩 ...

  6. 【量化】docker

    查看docker docker ps docker ps -a 删除docker docker stop 8809752ca95a docker rm 8809752ca95a 打包fly cd ~/ ...

  7. 【C#学习笔记】文本复制到粘贴板

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. #define XXX do{...}while(0)

    <ol> <li>函数式宏定义的参数没有类型,预处理器只负责做形式上的替换,而不做参数类型检查,所以传参时要格外小心.</li> <li>调用真正函数的 ...

  9. JBPM4之decision节点:3、程序猿|菜鸟|攻城狮|牛人

    JBPM入门系列文章: JBPM4入门——1.jbpm简要介绍 JBPM4入门——2.在eclipse中安装绘制jbpm流程图的插件 JBPM4入门——3.JBPM4开发环境的搭建 JBPM4入门—— ...

  10. MySQL与Oracle 差异比较之一数据类型

    数据类型 编号 ORACLE MYSQL 注释 1 NUMBER int / DECIMAL DECIMAL就是NUMBER(10,2)这样的结构INT就是是NUMBER(10),表示整型:MYSQL ...