Three Paths on a Tree

You are given an unweighted tree with n vertices. Recall that a tree is a connected undirected graph without cycles.

Your task is to choose three distinct vertices a,b,c on this tree such that the number of edges which belong to at least one of the simple paths between a and b, b and c, or a and c 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 n ( 3 ≤ n ≤ 2 ⋅ 1 0 5 ) n (3≤n≤2⋅10^5) n(3n2105) — the number of vertices in the tree.

Next n − 1 n−1 n1 lines describe the edges of the tree in form a i , b i ( 1 ≤ a i , b i ≤ n , a i ≠ b i ) a_i,b_i (1≤a_i, b_i≤n, a_i≠b_i) ai,bi(1ai,bin,ai=bi). It is guaranteed that given graph is a tree.

Output
In the first line print one integer res — the maximum number of edges which belong to at least one of the simple paths between a and b, b and c, or a and c.

In the second line print three integers a,b,c such that 1 ≤ a , b , c ≤ n 1≤a,b,c≤n 1a,b,cn and a ≠ , b ≠ c , a ≠ c a≠,b≠c,a≠c a=,b=c,a=c.

If there are several answers, you can print any.

Example

input
8
1 2
2 3
3 4
4 5
4 6
3 7
3 8
output
5
1 8 6

Note
The picture corresponding to the first example (and another one correct answer):
在这里插入图片描述

If you choose vertices 1,5,6 then the path between 1 and 5 consists of edges (1,2),(2,3),(3,4),(4,5), the path between 1 and 6 consists of edges (1,2),(2,3),(3,4),(4,6) and the path between 5 and 6 consists of edges (4,5),(4,6). The union of these paths is (1,2),(2,3),(3,4),(4,5),(4,6) so the answer is 5. It can be shown that there is no better answer.
a,b,c三个点中的其中两个点之间的路径一定是树的直径。
利用反证法证明:
三个点两两之间的路径会经过同一个点 v v v,假设存在三个点a,b,c,使得连接三个点的路径长度最长,且任意两点之间的路径不是树的直径。如果三个点的公共点 v v v在树的直径上,显然存在矛盾;如果三个点的公共点 v v v不在树的直径上,那么存在一条长度大于0的路径连接点 v v v和树的直径,首先,树的直径一定大于三个点任意两点之间的路径长度,因此a,b,c中任意一点到 v v v的路径加上 v v v到树的直径的长度加上树的直径总长度一定大于原来三个点之间的距离,与最优假设矛盾。综上a,b,c三个点中的其中两个点之间的路径一定是树的直径。
因此本题就很简单了。
但是值得注意的是,有的树没有分叉,需要单独讨论。

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 2e5 + 10;
int head[N], ver[N << 1], Next[N << 1], tot = 1;
int n, a, b, c, ans, d[N], pre[N];

inline void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}

inline void read() {
    n = qr();
    repi(i, 1, n - 1) {
        int x = qr(), y = qr();
        add(x, y), add(y, x);
    }
}

inline void bfs1() {
    d[1] = 1;
    queue<int> q;
    q.push(1);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        reps(x) {
            int y = ver[i];
            if (d[y])continue;
            d[y] = d[x] + 1;
            if (d[y] > d[a])a = y;
            q.push(y);
        }
    }
}

inline void bfs2() {
    ms(d), d[a] = 1;
    queue<int> q;
    q.push(a);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        reps(x) {
            int y = ver[i];
            if (d[y])continue;
            d[y] = d[x] + 1;
            pre[y] = x;
            if (d[y] > d[b])b = y;
            q.push(y);
        }
    }
    ans += d[b] - 1;
}

inline void bfs3() {
    ms(d);
    queue<int> q;
    int t = pre[b];
    while (t != a) q.push(t), d[t] = 1, t = pre[t];
    d[a] = 1, d[b] = 1;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        reps(x) {
            int y = ver[i];
            if (d[y])continue;
            d[y] = d[x] + 1;
            if (d[y] > d[c])c = y;
            q.push(y);
        }
    }
    if (!c)while (c == a || c == b || !c)c++;
    else ans += d[c] - 1;
}

int main() {
    read();
    bfs1();
    bfs2();
    bfs3();
    printf("%d\n%d %d %d\n", ans, a, b, c);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值