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

HDU4612 Warm up

文章目录

  • Problem Description
  • 题意:
  • 题解:
  • 代码:

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个边,问在添加一个边后,输出最小数量的桥
(桥:如果去掉这个边,会使得一些点与其他点隔离)

题解:

tarjan求出每个边双连通分量后,对每个双连通分量进行缩点,缩点就就是一颗无根树,在树上添加一个边,使得树上的桥最少,该怎么做?
一个树,所有边都是桥(因为你去掉任何一个边都会使得点与点分离),而我们只能加一个边,那我们就尽可能保留最多点,这样就可以最大程度减少桥的数量,最多能减少多少?其实就是树上的最长路,也就是从一个叶子节点到另外一个叶子节点,连接后,这个就形成闭环,因为是最长路,所有保留的最多。问题就转变成求树上最长路,即求树的直径
求树的直径可以用两边dfs来实现(原理我也忘了)
从任意一点x开始,dfs找与x最远的点y,然后从y开始找与y最远的点z,x与z的距离就是

代码:

代码参考
本人对代码详细注释

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-8;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e5+10;struct Edge
{int to, next;
}edge[MAXN*10];
int tot, head[MAXN];
vector<int>g[MAXN];void addedge(int u, int v)
{edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
}int index, dfn[MAXN], low[MAXN];
int top, Stack[MAXN], instack[MAXN];
int block, belong[MAXN];void Tarjan(int u, int pre)
{dfn[u] = low[u] = ++index;Stack[top++] = u;instack[u] = true;for(int i = head[u]; i!=-1; i = edge[i].next){//因为一对点之间可能有多条边,所以不能根据v是否为上一个点来防止边是否被重复访问。而需要根据边的编号if((i^1)==pre) continue;int v = edge[i].to;if(!dfn[v]){Tarjan(v, i);low[u] = min(low[u], low[v]);}else if(instack[v])low[u] = min(low[u], dfn[v]);}if(low[u]==dfn[u]){block++;int v;do{v = Stack[--top];instack[v] = false;belong[v] = block;}while(v!=u);}
}//常规的tarjan缩点操作int diameter, endpoint;
int dfs(int u, int pre, int dep)//当前节点  前节点   深度
{if(dep>diameter)//深度越深时,长度越长 {endpoint = u; //记录第一个端点 diameter = dep; //最深深度 }for(int i = 0; i<g[u].size(); i++)if(g[u][i]!=pre)dfs(g[u][i], u, dep+1);
}void init(int n)
{tot = 0;memset(head, -1, sizeof(head));index = 0;memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));top = 0;memset(instack, false, sizeof(instack));block = 0;for(int i = 1; i<=n; i++)belong[i] = i, g[i].clear();
}int main()
{int n, m;while(scanf("%d%d", &n, &m) && (n||m) ){init(n);for(int i = 1; i<=m; i++){int u, v;scanf("%d%d", &u, &v);addedge(u, v);addedge(v, u);}Tarjan(1, -1);for(int u = 1; u<=n; u++)for(int i = head[u]; i!=-1; i = edge[i].next){int v = edge[i].to;if(belong[u]!=belong[v])//如果不在同一个强连通量 g[belong[u]].push_back(belong[v]);//生成树 }endpoint = 1, diameter = 0;dfs(1,  -1, 0);dfs(endpoint,  -1, 0);printf("%d\n", block-1-diameter);//block(分块数量)-1表示最初桥的数量 //diameter表示最长距离,也就是取消的桥数量 }
}

HDU4612 Warm up相关推荐

  1. HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Ot ...

  2. HDU 4619 Warm up 2 最大独立集

    Warm up 2 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4619 Description Some 1×2 dominoes are pla ...

  3. 【学习率预热】Warm up

    1.什么是warm up   Warmup是在ResNet论文中提到的一种学习率预热的方法,它在训练开始的时候先选择使用一个较小的学习率,训练了一些epoches或者steps(比如4个epoches ...

  4. Resnet-18-训练实验-warm up操作

    实验数据:cat-dog 二分类,训练集:19871 验证集:3975 实验模型:resnet-18 batchsize:128*2 (一个K80吃128张图片) 存在的问题: 对训练集 accura ...

  5. Agent with Warm Start and Active Termination for Plane Localization in 3DUltrasound基于强化学习的超声标准切面

    目录 摘要 方法 平面定位的深度强化学习框架 动作Action 状态State 奖励Reward 智能体Agent 回放缓存Replay Buffer 损失Loss 热启动的特征点感知平面对准 过程 ...

  6. Ultra Light Waterproof Jacket 2014 Warm down Coats Cheap

    Regardless, thе arroyo covering is beautiful and will endure a continued time. Most of thе time, Ult ...

  7. Sentinel流控效果—Warm Up

    流控效果Warm Up有什么作用? 比如有一个系统平常没人访问,突然某个时刻系统访问量达到最大:这样的话系统容易崩掉,所以需要预热,让请求慢慢的升高: 比如:秒杀系统在开启的瞬间,会有很多流量上来,很 ...

  8. 【warm up】热身训练 的学习率设置

    warm up 策略 一.介绍 二.使用场景 三.有效原因 一.介绍 warmup 顾名思义就是热身,在刚刚开始训练时以很小的学习率进行训练,使得网络熟悉数据,随着训练的进行学习率慢慢变大,到了一定程 ...

  9. SONiC Warm Reboot

    原文:https://github.com/jipanyang/SONiC/blob/69d76c5fd2d870e2c53cbe367fd09927bb4836ba/doc/warm-reboot/ ...

最新文章

  1. 学好python工资一般多少钱-Python工资多少?就业发展前景怎么样?
  2. 在react-router中进行代码拆分
  3. iOS面试知识点梳理
  4. EXCEL小技巧:如何统计非空单元格
  5. unable to launch什么意思_都表示太...以至于,so … that…?与too… to …有着明显区别...
  6. Centos7.5 VMtools的安装与卸载
  7. 服务器共享文件夹权限设置软件,局域网共享文件访问控制软件、共享文件夹权限设置软件的使用方法...
  8. jquery 2.0.3代码结构
  9. pytorch_GPU安装
  10. android侧边栏点击,侧边菜单栏 android-menudrawer
  11. 如何将Win7、Win10笔记本,台式机系统C盘软件搬家? 只需3个步骤!!!
  12. EXCEL 导入MSSqlserver数据库报错
  13. 松下服务器显示18号报警,松下伺服报警代码预览表
  14. HTML-淘宝导航条
  15. 安兔兔html5测试跑分榜,2021年最新安兔兔手机性能跑分排行榜
  16. 金三银四,给大家肝一下面试题~
  17. Vue3 使用marked【代码高亮,安装使用教程】
  18. 管理菜单 结贴回复 来自 202.112.36.253 的回复: TTL 传输中过期
  19. Android 11 正式版发布
  20. 批量导入手机通讯录_手机QQ批量导入电话号码

热门文章

  1. 史上最变态高考数学题,让99%的考生献上膝盖,看完我惊了......
  2. 荷兰人发明的新客机是劈叉的!乘客坐在机翼上
  3. 《悦趣式连锁反应》玩转STEM教育!529块积木元件,N+1款炫酷模型
  4. python中test_在python中生成py.test测试
  5. mysql改密码脚本_mysql密码修改脚本
  6. 适合手机端的ckeditor样式_抖音运营干货(三):9款手机视频剪辑APP,让你轻松玩转后期!...
  7. ubuntu mysql集群搭建_ubuntu server部署mysql集群
  8. bp神经网络训练_数据分析模型6——神经网络基础(人工智能的底层模型)
  9. python交通标志识别_YOLOv3目标检测实战:交通标志识别
  10. mysql脚本的制作_制作脚本实现mysql自动备份