#1050 : 树中的最长路

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一棵二叉树!还可以拼凑成一棵多叉树——好吧,其实就是更为平常的树而已。

但是不管怎么说,小Ho喜爱的玩具又升级换代了,于是他更加爱不释手(其实说起来小球和木棍有什么好玩的是吧= =)。小Ho手中的这棵玩具树现在由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了不同的数字,并且这些数字都是出于1..N的范围之内,每根木棍都连接着两个不同的小球,并且保证任意两个小球间都不存在两条不同的路径可以互相到达。总而言之,是一个相当好玩的玩具啦!

但是小Hi瞧见小Ho这个样子,觉得他这样沉迷其中并不是一件好事,于是寻思着再找点问题让他来思考思考——不过以小Hi的水准,自然是手到擒来啦!

于是这天食过早饭后,小Hi便对着又拿着树玩具玩的不亦乐乎的小Ho道:“你说你天天玩这个东西,我就问你一个问题,看看你可否知道?”

“不好!”小Ho想都不想的拒绝了。

“那你就继续玩吧,一会回国的时候我不叫上你了~”小Hi严肃道。

“诶!别别别,你说你说,我听着呢。”一向习惯于开启跟随模式的小Ho忍不住了,马上喊道。

小Hi满意的点了点头,随即说道:“这才对嘛,我的问题很简单,就是——你这棵树中哪两个结点之间的距离最长?当然,这里的距离是指从一个结点走到另一个结点经过的木棍数。”。

“啊?”小Ho低头看了看手里的玩具树,困惑了。

提示一:路总有折点,路径也不例外!

输入

每个测试点(输入文件)有且仅有一组测试数据。

每组测试数据的第一行为一个整数N,意义如前文所述。

每组测试数据的第2~N行,每行分别描述一根木棍,其中第i+1行为两个整数Ai,Bi,表示第i根木棍连接的两个小球的编号。

对于20%的数据,满足N<=10。

对于50%的数据,满足N<=10^3。

对于100%的数据,满足N<=10^5,1<=Ai<=N, 1<=Bi<=N

小Hi的Tip:那些用数组存储树边的记得要开两倍大小哦!

输出

对于每组测试数据,输出一个整数Ans,表示给出的这棵树中距离最远的两个结点之间相隔的距离。

样例输入
8
1 2
1 3
1 4
4 5
3 6
6 7
7 8
样例输出
6

//一棵树的直径就是这棵树上存在的最长路径。
// 任意两点间有唯一路径的无向图是树 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <string>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <queue>
using namespace std;
const int N=1e5+;
int ans;
bool vis[N];
int dis[N];
int n,point;
struct Node{
int to,w;
Node(){}
Node(int TO,int W){
to=TO;
w=W;
}
}nod[N];
vector<Node>ve[N];
int bfs(int u)
{
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
queue<int>que;
que.push(u);
vis[u]=;
point=;
while(!que.empty()){
int v=que.front();
que.pop();
if(ans<dis[v]){
ans=dis[v];
point=v;
}
for(int i=;i<ve[v].size();i++){//这是树,直接走就可以了。
Node tmp=ve[v][i];
int w=tmp.to;
if(!vis[w]){
vis[w]=;
dis[w]=dis[v]+tmp.w;
que.push(w);
}
}
}
return point;
}
int main()
{
while(~scanf("%d",&n)){
int x,y;
for(int i=;i<n-;i++){
scanf("%d%d",&x,&y);
ve[x].push_back(Node(y,));
ve[y].push_back(Node(x,));
}
ans=;
//int point=bfs(1);
bfs();
ans=;
//cout<<point<<endl;
bfs(point);
printf("%d\n",ans);//树的直径
for(int i=;i<=n;i++) {
ve[i].clear();
}
}
return ;
}

Nordic Collegiate Programming Contest 2015​

A. Adjoin the Networks

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

题目来源

Nordic Collegiate Programming Contest 2015​

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
const int N=1e5+;
bool vis1[N],vis2[N];//同步进行
int c,l;
struct Node{
int to,w;
Node(){}
Node(int TO,int W){
to=TO;
w=W;
}
};
vector<Node>ve[N];
int ans[N],dis[N];
int bfs1(int x)
{
queue<int>q;
vis1[x]=;
dis[x]=;//这样dis 就不用清0了。
q.push(x);
int ret=,point=;
while(!q.empty()){
int v=q.front();
q.pop();
if(ret<dis[v]){
ret=dis[v];
point=v;
}
for(int i=;i<ve[v].size();i++){
Node tmp=ve[v][i];
int w=tmp.to;
if(!vis1[w]){
vis1[w]=;
dis[w]=dis[v]+tmp.w;
q.push(w);
}
}
}
return point;
}
int bfs2(int x)
{
queue<int>q;
vis2[x]=;
dis[x]=;
q.push(x);
int ret=,point=;
while(!q.empty()){
int v=q.front();
q.pop();
if(ret<dis[v]){
ret=dis[v];
point=v;
}
for(int i=;i<ve[v].size();i++){
Node tmp=ve[v][i];
int w=tmp.to;
if(!vis2[w]){
vis2[w]=;
dis[w]=dis[v]+tmp.w;
q.push(w);
}
}
}
return point;
}
bool cmp(int a,int b){
return a>b;
}
int main()
{
scanf("%d%d",&c,&l);
int x,y;
for(int i=;i<l;i++){
scanf("%d%d",&x,&y);
ve[x].push_back(Node(y,));
ve[y].push_back(Node(x,));
}
int t=;
for(int i=;i<c;i++){
if(!vis1[i])//访问过的不再重复
{
int u=bfs1(i);
int v=bfs2(u);
ans[t++]=dis[v];
}
}
sort(ans,ans+t,cmp);
//只能是下面三种情况
int x1=ans[];
int y1=(ans[]+)/+(ans[]+)/+;
int z1=(ans[]+)/+(ans[]+)/+;//l==0时也正确,所有点围成半径为1的圆
int MAX=max(x1,max(y1,z1));
//其他的连在最长直径的子树上,并以圆的形式放置
printf("%d\n",MAX);
return ;
}

hiho 1050 树的直径的更多相关文章

  1. [HIHO] 1050 树中的最长路

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  2. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  3. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  4. VIJOS1476旅游规划[树形DP 树的直径]

    描述 W市的交通规划出现了重大问题,市政府下决心在全市的各大交通路口安排交通疏导员来疏导密集的车流.但由于人员不足,W市市长决定只在最需要安排人员的路口安放人员.具体说来,W市的交通网络十分简单,它包 ...

  5. poj2631 树的直径

    设s-t是这棵树的直径,那么对于任意给予的一点,它能够到达的最远的点是s或者t. 这样我们可以通过2次bfs找到树的直径了. #include<cstdio> #include<qu ...

  6. 【BZOJ-1912】patrol巡逻 树的直径 + DFS(树形DP)

    1912: [Apio2010]patrol 巡逻 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1034  Solved: 562[Submit][St ...

  7. 牡丹江.2014B(图论,树的直径)

    B - Building Fire Stations Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & ...

  8. 2227 邮票--FUoj(链接表+树的直径)

    http://acm.fzu.edu.cn/problem.php?pid=2227 我感觉这道题可以随意搞 题目大意: 给你的一个图就是一条链,但是不知道起始点和结束点,而且每个点只会访问一次. 因 ...

  9. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

随机推荐

  1. Vue部分知识

    一.本尊建议的学习顺序:https://zhuanlan.zhihu.com/p/23134551(侵删) 二.安装: 1.安装 Node.js,可以去Node.js的官网上下载: 2.(非必选)如果 ...

  2. docker部署mysql远程连接 解决1251 client does not support ..

    现象:用虚拟机上Docker启动mysql之后无法在本地安装的navicat上远程连接已启动的mysql,错误截图: 原因:mysql 8.0 默认使用 caching_sha2_password 身 ...

  3. js浮点数乘除法

    JS在处理浮点数计算时经常会遇到精度的问题,上一篇博客封装了JS浮点数加减法的方法,这一次来封装一下js浮点数乘除法运算. 其实浮点除法的封装跟加减法的封装原理是一样,只是在第一次计算完后会再复位小数 ...

  4. Android 4.4及以后将内容布局延伸到状态栏

    首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示:   界面 这个界面的布局就是co ...

  5. 【开发小结】Two Steps from Deadline

    进度条可以救我也可以杀死我 # START 2018年4月17日晚我测试了11组四则运算的UI,每个exe程序生成的每一道题都有恐怖的倒计时.PSP表格清晰的记录了开发过程中消耗的时间,但是在结对作业 ...

  6. UML的九种模型图

    本文转自UML 的九种模型图,仅供学习交流! 一.作为一种建模语言,UML的定义包括UML语义和UML表示法两个部分. UML语义:描述基于UML的精确元模型定义. UML表示法:定义UML符号的表示 ...

  7. 为什么HDFS的副本数通常选择3?

    HDFS采用一种称为机架感知的策略来改进数据的可靠性.可用性和网络带宽的利用率. 在大多数情况下,HDFS的副本系数是3,HDFS的存放策略是一个副本存放在本地机架节点上,另一个副本存放在同一机架的另 ...

  8. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)

    题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...

  9. nginx 的反向代理及缓存功能

    上游服务器的设置 server { #监听的IP及端口 listen 127.0.0.1:8080; #虚拟主机对硬解析的主机名 #server_name localhost; #charset ko ...

  10. (原)IPhone开发时把ToolBar中的元素居中的技巧

    在IPhone应用开发时,经常用到ToolBar,其中的控件通常都是居左,想让它居中就有点困难. 这里介绍一种方法: 将Flexible Space Bar Button Item从库中拖到位于控件左 ...