Codeforces Round #615 (Div. 3) F. Three Paths on a Tree
F. Three Paths on a Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.
Your task is to choose three distinct vertices a,b,ca,b,c on this tree such that the number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc is the maximum possible. See the notes section for a better understanding.
The simple path is the path that visits each vertex at most once.
Input
The first line contains one integer number nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of vertices in the tree.
Next n−1n−1 lines describe the edges of the tree in form ai,biai,bi (1≤ai1≤ai, bi≤nbi≤n, ai≠biai≠bi). It is guaranteed that given graph is a tree.
Output
In the first line print one integer resres — the maximum number of edges which belong to at least one of the simple paths between aa and bb, bb and cc, or aa and cc.
In the second line print three integers a,b,ca,b,c such that 1≤a,b,c≤n1≤a,b,c≤n and a≠,b≠c,a≠ca≠,b≠c,a≠c.
If there are several answers, you can print any.
Example
input
Copy
8
1 2
2 3
3 4
4 5
4 6
3 7
3 8
output
Copy
5
1 8 6
Note
The picture corresponding to the first example (and another one correct answer):
If you choose vertices 1,5,61,5,6 then the path between 11 and 55 consists of edges (1,2),(2,3),(3,4),(4,5)(1,2),(2,3),(3,4),(4,5), the path between 11 and 66 consists of edges (1,2),(2,3),(3,4),(4,6)(1,2),(2,3),(3,4),(4,6) and the path between 55 and 66 consists of edges (4,5),(4,6)(4,5),(4,6). The union of these paths is (1,2),(2,3),(3,4),(4,5),(4,6)(1,2),(2,3),(3,4),(4,5),(4,6) so the answer is 55. It can be shown that there is no better answer.
题意:找出树上三个点,满足这三个点相互形成的简单路径的边并集最大
直觉告诉我们:有两个点一定是直径的两个端点,嗯,你没错,这两个点怎么求?随便找一个点bfs出来一个端点,然后再以这个端点bfs一次出来另一个端点,ok
剩下一个点如何确定?到两个点距离和最大即可,在两次bfs过程中保留两个端点到任意点的距离然后找这个最大值就好
#include<bits/stdc++.h>
#define ll long long
#define rg register ll
using namespace std;
ll n;
ll head[200005],cnt,dis[200005],vis[200005],maxdis,dis1[200005],dis2[200005];
struct node
{
ll to,next,val;
}edge[400005];
inline void add(ll u,ll v,ll val)
{
edge[cnt].to=v;
edge[cnt].val=val;
edge[cnt].next=head[u];
head[u]=cnt++;
}
inline ll bfs(ll x)
{
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
queue<ll>q;
q.push(x);
vis[x]=1;
ll now;
while(!q.empty())
{
now=q.front();
q.pop();
for(rg i=head[now];~i;i=edge[i].next)
{
ll to=edge[i].to;
if(!vis[to])
{
q.push(to);
vis[to]=1;
dis[to]=dis[now]+edge[i].val;
maxdis=max(maxdis,dis[to]);
}
}
}
return now;
}
int main()
{
memset(head,-1,sizeof(head));
cin>>n;
for(rg i=1;i<n;i++)
{
ll a,b;
cin>>a>>b;
add(a,b,1),add(b,a,1);
}
ll ans1=bfs(1);
ll ans2=bfs(ans1);
for(rg i=1;i<=n;i++)dis1[i]=dis[i];
bfs(ans2);
for(rg i=1;i<=n;i++)dis2[i]=dis[i];
ll ans3=0,ans=0;
for(rg i=1;i<=n;i++)
{
if((dis1[i]+dis2[i]+maxdis)/2>ans&&i!=ans1&&i!=ans2)ans=(dis1[i]+dis2[i]+maxdis)/2,ans3=i;
}
cout<<ans<<endl<<ans1<<" "<<ans2<<" "<<ans3<<endl;
while(1)getchar();
return 0;
}
- Go语言学习(一):hello world
- JAVA基础知识点:内存、比较和Final
- python django 学习(一)数据库 目的根据已有BBS建立新的网站步骤
- SpringMVC中@RequestMapping的基本用法
- Java面试系列13
- 【Go 语言 时间相关操作 demo】
- redis AOF保存机制
- 用 TensorFlow 让你的机器人唱首原创给你听
- 不用synchronized块的话如何实现一个原子的i++?
- Oracle升级中的参数补充(r9笔记第2天)
- 【Go 语言,服务器模块】日志系统源码
- wait方法和sleep方法的区别
- Java面试系列10
- python django整理(三)页面基础(仿BBS)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Laravel-admin之修改操作日志的方法
- php使用curl伪造浏览器访问操作示例
- 关于laravel后台模板laravel-admin select框的使用详解
- 基于Laravel-admin 后台的自定义页面用法详解
- php解决约瑟夫环算法实例分析
- 浅谈laravel-admin的sortable和orderby使用问题
- 使用composer安装使用thinkphp6.0框架问题【视频教程】
- 基于laravel-admin 后台 列表标签背景的使用方法
- 解决laravel-admin 自己新建页面里 js 需要刷新一次的问题
- laravel-admin 中列表筛选方法
- Laravel框架控制器的middleware中间件用法分析
- laravel-admin的图片删除实例
- 在laravel-admin中列表中禁止某行编辑、删除的方法
- Laravel的Auth验证Token验证使用自定义Redis的例子
- laravel-admin解决表单select联动时,编辑默认没选上的问题