计蒜客:ACM

One day your boss explains to you that he has a bunch of computer networks that are currently unreachable from each other, and he asks you, the cable expert's assistant, to adjoin the networks to each other using new cables. Existing cables in the network cannot be touched.

He has asked you to use as few cables as possible, but the length of the cables used does not matter to him, since the cables are optical and the connectors are the expensive parts. Your boss is rather picky on cable usage, so you know that the already existing networks have as few cables as possible.

Due to your humongous knowledge of computer networks, you are of course aware that the latency for an information packet travelling across the network is proportional to the number of hops the packet needs, where a hop is a traversal along a single cable. And since you believe a good solution to your boss' problem may earn you that long wanted promotion, you decide to minimise the maximum number of hops needed between any pair of network nodes.

Input Format

On the first line, you are given two positive integers, the number 1 \le c \le 10^51≤c≤105 of computers and the number 0 \le l \le c - 10≤l≤c−1 of existing cables. Then follow ll lines, each line consisting of two integers aa and bb, the two computers the cables connect. You may assume that every computer has a unique name between 00 and n - 1n−1.

Output Format

The maximum number of hops in the resulting network.

样例输入1

6 4
0 1
0 2
3 4
3 5

样例输出1

3

样例输入2

11 9
0 1
0 3
0 4
1 2
5 4
6 4
7 8
7 9
7 10

样例输出2

4
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=100000+1000;
int a[maxn],vis[maxn],vis1[maxn],minv,head[maxn],tot,T;
struct Edge{int to,next;
}e[2*maxn];
void init(int n){tot=0,T=0;mem1(head),mem0(a),mem0(vis1),mem0(vis);
}
void addedge(int u,int v){e[tot].to=v,e[tot].next=head[u],head[u]=tot++,e[tot].to=u,e[tot].next=head[v],head[v]=tot++;
}
bool cmp(int u,int v){return u>v;
}
struct node{int u,dep;
};
node bfs(int u,int dep){queue<node>q;q.push((node){u,dep});vis[u]=T;int end1=u,count1=0;while(!q.empty()){node ans=q.front();q.pop();int u=ans.u,dep=ans.dep;for(int i=head[u];i!=-1;i=e[i].next){int v=e[i].to;if(vis[v]!=T){q.push((node){v,dep+1});vis[v]=T,end1=v,count1=dep+1;}}}node b={end1,count1};return b;
}
int main(){int n,m,u,v;while(scanf("%d%d",&n,&m)!=EOF){init(n);for(int i=0;i<m;i++){scanf("%d%d",&u,&v);u++,v++,vis1[u]=1,vis1[v]=1;addedge(u,v);}int cnt=0,minv=0,flag=0;for(int i=1;i<=n;i++){if(vis[i]==0&&vis1[i]!=0){++T;    node b=bfs(i,0);++T;node c=bfs(b.u,0);minv=max(minv,c.dep);a[cnt++]=(c.dep+1)/2;flag=1;}}sort(a,a+cnt,cmp);if(n<=2)printf("%d\n",n-1>=0?n-1:0);else if(flag==0)       printf("2\n");else if(cnt>=3&&(a[0]==a[1])&&(a[1]==a[2]))printf("%d\n",max(minv,a[0]+a[1]+2));elseprintf("%d\n",max(minv,a[0]+a[1]+1));}return 0;
}

计蒜客:Adjoin the Networks相关推荐

  1. 计蒜客 挑战难题 爬楼梯

    计蒜客 挑战难题 爬楼梯 假设你现在正在爬楼梯,楼梯有n级.每次你只能爬1级或者2级,那么你有多少种方法爬到楼梯的顶部? 格式: 第一行输入一个数n(n<=50),代表楼梯的级数. 接下来一行输 ...

  2. 无脑博士的试管们java_计蒜客 无脑博士和他的试管们

    标签: 无脑博士有三个容量分别是A,B,C升的试管,A,B,C分别是三个从1到20的整数,最初,A和B试管都是空的,而C试管是装满硫酸铜溶液的.有时,无脑博士把硫酸铜溶液从一个试管倒到另一个试管中,直 ...

  3. 最短路(遍历边)计蒜客—DD坐地铁

    题目: C 城有 n 个站点, m 条双向地铁,每条地铁有一个 companyicompany_icompanyi​表示它的公司,如果连续乘坐同一家公司的地铁只要花 1元钱就好.DD现在想出门找萨摩耶 ...

  4. K - Triangle 计蒜客 - 42405

    K - Triangle 计蒜客 - 42405 题意: 给你一个三角形的三点,再给你三角形边上一个点,让你求另一个点(也要在三角形上),使得平分三角形的面积 题解: 计算几何 三角形的三边ab,ac ...

  5. H - Prince and Princess 计蒜客 - 42402

    H - Prince and Princess 计蒜客 - 42402 题意: 你现在要寻找公主,有三种人,第一种是说真话的人(至少为1,因为公主是说真话的人),第二种人是只会说假话的,第三种是胡说八 ...

  6. C - Digital Path 计蒜客 - 42397 05-29

    C - Digital Path 计蒜客 - 42397 题意: 题意就是给出一个n ∗ m的数字矩阵每个矩阵元素之间只能上下左右走,而且下一个位置必须比当前位置的数字大1,入口和出口必须数边缘元素, ...

  7. 计蒜客可以做计算机编程吗,如果你的编程能力不足以支撑你成为工程师的野心,不妨到计蒜客上学学看...

    在人才招聘领域存在这样一个怪圈,高校每年都说是最难就业年.人才过剩,而对于企业HR来说永远都招不到称心如意的人才.这个怪圈在"计蒜客"创始人俞昊然看来,主要是因为当今高校的教学资源 ...

  8. 计蒜客 - T1012 A*B问题

    计蒜客 - T1012 A*B问题 相信你已经学会 A+B 问题了,那么问题又来了- 输入两个正整数 A 和 B ,求 A×B. 输入格式 一行,包含两个正整数 A 和 B,中间用单个空格隔开. 1≤ ...

  9. 计蒜客网站 ACM-ICPC亚洲区赛题

    The Preliminary Contest for ICPC China Nanchang National Invitational(2019) ACM-ICPC Jiaozuo Onsite ...

  10. 在线计算机 授课,在线计算机教育网站计蒜客改版 推出算法竞赛课程

    [赛迪网讯]4月16日消息,近日,新型在线计算教育平台计蒜客进行了重大改版,网站从底层架构到课程内容都进行了大幅调整.计蒜客新版除了服务速度和稳定性大幅提升外,还对用户的学习体验进行了优化:同时,计蒜 ...

最新文章

  1. oracle 并发请求跟踪,在保留在oracle数据库中的同时处理并发请求?
  2. Java泛型:类型擦除
  3. ASP.NET Core官方文档+源码,这样学效率高10倍!
  4. input文件上传代码片段
  5. netstat -anp | grep 8199 查看端口占用情况
  6. Kali安装问题解决方案
  7. MySQL 文本类型,存储大小
  8. 憎恨之心最强套装攻略_憎恨之心装备选择 | 手游网游页游攻略大全
  9. golang sync/atomic
  10. 企业财务管理中的数据分析应用
  11. c语言满屏爱心,微信聊天可以发满屏动态爱心了 个性又浪漫!
  12. oracle 在此 select 语句中缺少 into 子句,Go database/sql文档
  13. Chromedriver适用谷歌浏览器的各个版本
  14. 操作系统之Windows
  15. nslookup命令详解和实战例子(全)
  16. python接收http请求_python通过get,post方式发送http请求和接收http响应
  17. 问题:网络地址192.168.10.0;子网掩码255.255.255.128(/25)
  18. 【Unity】摄像机常用设置
  19. Vive controller vibration
  20. dell计算机的硬盘如何分区,win10系统戴尔笔记本给硬盘分区的具体技巧

热门文章

  1. 你居然只知道蓝绿发布?今天教你全链路灰度~
  2. 解决执行hive语句时出现虚拟内存不够的问题
  3. Zookeeper kick off
  4. 小程序 消息订阅、统一服务消息、客服消息、消息推送
  5. 做生意值得一看的几部电视剧
  6. 【汇编语言】emu汇编软件破解注册码
  7. 关于看算法导论不懂的时候的思考
  8. 加密项目必修科目:代币经济学
  9. 计算机学报英语,修改稿要求-计算机学报.PDF
  10. HDU 4565(矩阵快速幂)