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(3n2105)n (3≤n≤2⋅10^5) — the number of vertices in the tree.

Next n1n−1 lines describe the edges of the tree in form ai,bi(1ai,bin,aibi)a_i,b_i (1≤a_i, b_i≤n, a_i≠b_i). 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 1a,b,cn1≤a,b,c≤n and a,bc,aca≠,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三個點中的其中兩個點之間的路徑一定是樹的直徑。
利用反證法證明:
三個點兩兩之間的路徑會經過同一個點vv,假設存在三個點a,b,c,使得連接三個點的路徑長度最長,且任意兩點之間的路徑不是樹的直徑。如果三個點的公共點vv在樹的直徑上,顯然存在矛盾;如果三個點的公共點vv不在樹的直徑上,那麼存在一條長度大於0的路徑連接點vv和樹的直徑,首先,樹的直徑一定大於三個點任意兩點之間的路徑長度,因此a,b,c中任意一點到vv的路徑加上vv到樹的直徑的長度加上樹的直徑總長度一定大於原來三個點之間的距離,與最優假設矛盾。綜上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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章