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(3≤n≤2⋅105) — the number of vertices in the tree.
Next n − 1 n−1 n−1 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(1≤ai,bi≤n,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 1≤a,b,c≤n 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;
}