首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Codeforces Round #615 (Div. 3) F. Three Paths on a Tree

Codeforces Round #615 (Div. 3) F. Three Paths on a Tree

作者头像
glm233
发布于 2020-09-28 03:11:17
发布于 2020-09-28 03:11:17
35700
代码可运行
举报
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
8
1 2
2 3
3 4
4 5
4 6
3 7
3 8

output

Copy

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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过程中保留两个端点到任意点的距离然后找这个最大值就好

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/01/24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Codeforces Round #633 (Div. 2)D Edge Weight Assignment(构造、树的权值异或)
You have unweighted tree of nn vertices. You have to assign a positive weight to each edge so that the following condition would hold:
glm233
2020/09/28
3400
Codeforces Round #633 (Div. 2)D Edge Weight Assignment(构造、树的权值异或)
CF991F「Tree Destruction」
You are given an unweighted tree with nnn vertices. Then ollowing operations are applied to the tree. A single operation consists of the following steps:
hotarugali
2022/03/03
6730
[2019HDU多校第一场][HDU 6582][E. Path]
题意: t 组样例,n个点,m条边。i 行a到b距离c. 要求去掉最短路的最小代价。去掉每条路代价等于长度。
用户2965768
2019/08/01
3160
Codeforces Round #544 (Div. 3) F1. Spanning Tree with Maximum Degree(bfs)
题目链接:http://codeforces.com/contest/1133/problem/F1
Ch_Zaqdt
2019/03/15
4400
Codeforces Round #547 (Div. 3)G. Privatization of Roads in Treeland
Treeland consists of n cities and n−1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
glm233
2020/09/28
3790
51nod 1649 齐头并进(Codeforces 601A The Two Routes) (最短路)
题目链接(Codeforces):http://codeforces.com/problemset/problem/601/A
Ch_Zaqdt
2019/01/10
3930
Codeforces Round #548 (Div. 2) C. Edgy Trees(思维+dfs)
题目链接:https://codeforces.com/contest/1139/problem/C
Ch_Zaqdt
2019/04/09
5410
Codeforces Round #615 (Div. 3) E. Obtain a Permutation
② 把矩阵中任意一列整体往上挪一个单元格,如下图矩阵我们对第一列向上挪了一个单元格
glm233
2020/09/28
3420
Codeforces Round #615 (Div. 3) E. Obtain a Permutation
Codeforces Round #624 (Div. 3) B - WeirdSort
You are also given a set of distinct positions p1,p2,…,pmp1,p2,…,pm, where 1≤pi<n1≤pi<n. The position pipi means that you can swap elements a[pi]a[pi] and a[pi+1]a[pi+1]. You can apply this operation any number of times for each of the given positions.
glm233
2020/09/28
5430
牛客寒假算法基础集训营3 B. 处女座的比赛资格(DAG上拓扑排序)
题目链接:https://ac.nowcoder.com/acm/contest/329/B
Ch_Zaqdt
2019/02/27
3750
洛谷 P1886 滑动窗口 /【模板】单调队列 (单调队列、线段树、RMQ(ST表))
有一个长为 nn 的序列 aa,以及一个大小为 kk 的窗口。现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值。
glm233
2020/09/28
7540
洛谷  P1886 滑动窗口 /【模板】单调队列  (单调队列、线段树、RMQ(ST表))
Codeforces Round #625解题报告
题目链接 题意:两种机器人对于n个问题有会和不会(1和0) 希望第一种的得分比第二种高 要如何安排问题分数分布 要求分数中最大的尽量小 算一下第一种比第二种多的1和0 除一下 可能会除零 要先判掉再除
wenzhuan
2022/08/15
2300
The 2019 Asia Nanchang First Round Online Programming Contest B Fire-Fighting Hero(阅读理解)
This is an era of team success, but also an era of heroes. Throughout the ages, there have been numerous examples of using the few to defeat the many. There are VV (Numbers 11 to VV) fire-fighting points in ACM city. These fire-fighting points have EE roads to communicate with each other. Among them, there is a fire-fighting hero in the SS fire-fighting point, and the fire-fighting team is distributed in K fire-fighting points. If a fire-fighting point needs to be put out, the fire-fighting hero or the fire-fighting team must arrive as soon as possible, that is, to choose the shortest route to arrive.
风骨散人Chiam
2020/10/28
2820
PAT (Advanced Level) Practice 1146 Topological Order (25分)
1146 Topological Order (25分) This is a problem given in the Graduate Entrance Exam in 2018: Which of
glm233
2020/09/28
4310
PAT (Advanced Level) Practice 1146 Topological Order (25分)
codeforces round #257 div2 C、D「建议收藏」
由于横切+竖切(或竖切+横切)会对分割的东西产生交叉份数。从而最小的部分不会尽可能的大。
全栈程序员站长
2022/07/10
2320
Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round) B. Mike and Children(思维)
题目链接:https://codeforces.com/contest/1121/problem/B
Ch_Zaqdt
2019/03/15
3980
Educational Codeforces Round 54 (Rated for Div. 2) D. Edge Deletion(dijkstra+bfs)
题目链接:http://codeforces.com/contest/1076/problem/D
Ch_Zaqdt
2019/01/10
3920
P1330 封锁阳光大学
题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街。河蟹看到欢快的曹,感到不爽。河蟹决定封锁阳光大学,不让曹刷街。 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M条道路连接。每只河蟹可以对一个点进行封锁,当某个点被封锁后,与这个点相连的道路就被封锁了,曹就无法在与这些道路上刷街了。非常悲剧的一点是,河蟹是一种不和谐的生物,当两只河蟹封锁了相邻的两个点时,他们会发生冲突。 询问:最少需要多少只河蟹,可以封锁所有道路并且不发生冲突。 输入输出格式 输入格式: 第一行
attack
2018/04/13
7830
文心一言 VS 讯飞星火 VS chatgpt (309)-- 算法导论22.2 7题
七、职业摔跤手可以分为两种类型:“娃娃脸”(“好人”)型和“高跟鞋”(“坏人”)型。在任意一对职业摔跤手之间都有可能存在竞争关系。假定有 n 个职业摔跤手,并且有一个给出竞争关系的 r 对摔跤手的链表。请给出一个时间为 O(n+r) 的算法来判断是否可以将某些摔跤手划分为“娃娃脸”型,而剩下的划分为“高跟鞋”型,使得所有的竞争关系均只存在于娃娃脸型和高跟鞋型选手之间。如果可以进行这种划分,则算法还应当生成一种这样的划分。如果要写代码,请用go语言。
福大大架构师每日一题
2024/08/16
1330
文心一言 VS 讯飞星火 VS chatgpt (309)-- 算法导论22.2 7题
CF1405D「Tree Tag」
Alice and Bob are playing a fun game of tree tag.
hotarugali
2022/03/03
6700
推荐阅读
相关推荐
Codeforces Round #633 (Div. 2)D Edge Weight Assignment(构造、树的权值异或)
更多 >
领券
一站式MCP教程库,解锁AI应用新玩法
涵盖代码开发、场景应用、自动测试全流程,助你从零构建专属AI助手
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档