Warm up

Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

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条双向通道连接着m对行星。问你新建一条双向通道后,无向图中最少会剩下多少条桥。有重边。
 
解题思路:无向图求边双连通分量,缩点,重新构图,形成树。求树的直径,然后用原图总的桥减去树的直径即为结果。求树的直径,我们用两次搜索,第一次从任意点出发,搜到的最远结点即为直径的一端,然后从这一端再次进行搜索,搜到直径的另一端。
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 200100;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn*10];
int head[maxn], tot;
int dfs_clock, dfn[maxn], brinum;
int Stack[maxn], instack[maxn], top, ebccno[maxn], ebcc_cnt;
int deg[maxn];
vector<int>G[maxn];
void init(){
tot = 0;
brinum = dfs_clock = 0;
top = 0;
ebcc_cnt = 0;
memset(deg,0,sizeof(deg));
memset(head,-1,sizeof(head));
}
void AddEdge(int _u,int _v){
edges[tot] = Edge(_v,head[_u]);
head[_u] = tot++;
}
int dfs(int u,int fa){
int lowu = dfn[u] = ++dfs_clock;
Stack[++top] = u;
// instack[u] = 1;
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){
int lowv = dfs(v,i);
lowu = min(lowu,lowv);
if(lowv > dfn[u]){
brinum++;
}
}else if(dfn[v] < dfn[u] && (fa^1) != i){//这里用边的编号来标记是否是同一条边的回边
lowu = min(lowu,dfn[v]);
}
}
if(dfn[u] == lowu){ //找到一个边双连通分量
ebcc_cnt++;
for(;;){
int v = Stack[top--];
// instack[v] = 0;
ebccno[v] = ebcc_cnt; //给每个点划分一个分量标号
if(u == v){
break;
}
}
}
// low[u] = lowu;
return lowu;
}
void find_ebcc(int n){
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
for(int i = 1; i <= n; i++){
if(!dfn[i]){
dfs(i,-1);
}
}
}
int pos, Maxd;
void dfs1(int u,int dep,int fa){ //求树的直径
if(dep > Maxd){
Maxd = dep;
pos = u;
}
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(fa == v){ continue; }
dfs1(v,dep+1,u);
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
init();
for(int i = 0; i <= n; i++){
G[i].clear();
}
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
AddEdge(b,a);
}
find_ebcc(n);
for(int i = 1; i <= n; i++){
for(int j = head[i]; j != -1; j = edges[j].next){
int v = edges[j].to;
if(ebccno[i] != ebccno[v]){ //重新构图,形成树
G[ebccno[i]].push_back(ebccno[v]);
}
}
}
pos = 1, Maxd = 0;
dfs1(1,0,-1);
int st = pos; Maxd = 0;
dfs1(pos,0,-1);
printf("%d\n",brinum - Maxd);
}
return 0;
}

  

 
 

HDU 4612——Warm up——————【边双连通分量、树的直径】的更多相关文章

  1. HDU 4612 Warm up (边双连通分量+缩点+树的直径)

    <题目链接> 题目大意:给出一个连通图,问你在这个连通图上加一条边,使该连通图的桥的数量最小,输出最少的桥的数量. 解题分析: 首先,通过Tarjan缩点,将该图缩成一颗树,树上的每个节点 ...

  2. HDU 4612 Warm up(双连通分量缩点+求树的直径)

    思路:强连通分量缩点,建立一颗新的树,然后求树的最长直径,然后加上一条边能够去掉的桥数,就是直径的长度. 树的直径长度的求法:两次bfs可以求,第一次随便找一个点u,然后进行bfs搜到的最后一个点v, ...

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

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

  4. hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  5. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

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

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

  7. HDU 4612 Warm up(2013多校2 1002 双连通分量)

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Su ...

  8. [HDOJ4612]Warm up(双连通分量,缩点,树直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...

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

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

随机推荐

  1. group by 两个或以上条件的分析

    首先group by 的简单说明: group by 一般和聚合函数一起使用才有意义,比如 count sum avg等,使用group by的两个要素:   (1) 出现在select后面的字段 要 ...

  2. luogu p4174 最大获利(最大权闭合子图)

    luogu p4174 最大获利(最大权闭合子图) 给定n个点,m条边,每条边有一个贡献,每个点有一个代价.选择一条边,会付出边所连两个点的代价,问最大代价. 我们换个建图方式:把图G中的边\(e_i ...

  3. Linux文件属性用户、组、权限

    Linux系统中的用户是分角色的,用户的角色是由UID和GID来识别的(也就是说系统识别的是用户的UID.GID,而非用户用户名),有个UID是唯一的(系统中唯一如同身份证一样)用来标识系统的用户账号 ...

  4. CUDA中自动初始化显卡设备宏

    每次为了减少初始化的工作量,可以写入下面的宏. #define CUT_DEVICE_INIT(ARGC,ARGV){ \ int deviceCount; \ CUDA_SAFE_CALL_NO_S ...

  5. jpa batch批量操作save和persist比较

    1.网上最常见的JPA----entityManager批量操作方法 private EntityManager em; @PersistenceContext(name = "Entity ...

  6. Python发送邮件代码

    Python发送带附件的邮件代码 #coding: utf-8 import smtplib import sys import datetime from email.mime.text impor ...

  7. [SHOI2002]百事世界杯之旅

    题目:"--在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽奖活动,获得球星背包,随声听,更克赴日韩观看世界杯. ...

  8. 「模拟赛20180406」膜树 prufer编码+概率

    题目描述 给定一个完全图,保证\(w_{u,v}=w_{v,u}\)且\(w_{u,u}=0\),等概率选取一个随机生成树,对于每一对\((u,v)\),求\(dis(u,v)\)的期望值对\(998 ...

  9. 数据结构19: BF算法(普通模式匹配算法)

    判断两个串之间是否存在主串与子串的关系,这个过程称为串的模式匹配. 在串的模式匹配过程,子串 T 通常被叫做“模式串”. 普通的模式匹配(“BF”算法) 判断两个串是否存在子串与主串的关系,最直接的算 ...

  10. MySQL数据查询结果导出生成文件

    select url from news where url like "%美女%"  into outfile  "/导出的文件路径" : 在这里有个坑,对于 ...