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

F. Three Paths on a Tree
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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⋅105) — the number of vertices in the tree.

Next n−1 lines describe the edges of the tree in form 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 and a≠,b≠c,a≠c.

If there are several answers, you can print any.

Example
inputCopy
8
1 2
2 3
3 4
4 5
4 6
3 7
3 8
outputCopy
5
1 8 6

1、结论:ans=(dis(s,i)+dis(t,i)+dis(s,t))/2,(i!=s,i!=t);
2、找数的直径的两个端点s、t,可用dfs或bfs,同时预处理出以s|t为起始点到其它点的距离
3、枚举第三点,不断更新答案

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int N=2e5+9;
const int M=2e5+9;
#define line '\n'
struct edge{int v,w,pre;}E[M<<1];
int dis[N],ds[N],dt[N],vis[N],k=-1,H[N];
inline int read(){int op=1,x=0;char c=getchar();while(!isdigit(c)){if(c=='-')op=-1;c=getchar();}while(isdigit(c))x=x*10+c-48,c=getchar();return x*op;}
inline void add(int u,int v,int w){E[++k].pre=H[u];E[k].v=v;E[k].w=w;H[u]=k;}
/*int bfs(int s)
{
    int t=0;
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    queue<int> Q;Q.push(s);
    vis[s]=1;
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        int i=H[u];
        while(i+1){
            int v=E[i].v,w=E[i].w;
            if(!vis[v]){
                dis[v]=dis[u]+w;
                if(dis[v]>dis[t])t=v;
                Q.push(v);
                vis[v]=1;
            }
            i=E[i].pre;
        }
    }
    return t;
}
int main()
{
    memset(H,-1,sizeof(H));
    int n=read(),u,v;
    for(int i=1;i<n;++i)u=read(),v=read(),add(u,v,1),add(v,u,1);
    int t=bfs(1);
    int s=bfs(t);memcpy(dt,dis,sizeof(dis));
    //cout<<"t: "<<t<<" s: "<<s<<line;
    bfs(s);memcpy(ds,dis,sizeof(dis));
    int id=0,ans=0;
    for(int i=1;i<=n;++i){
        if(i==s||i==t)continue;
        int res=(ds[i]+dt[i]+ds[t])/2;
        if(res>ans)ans=res,id=i;
    }
    cout<<ans<<line<<s<<" "<<t<<" "<<id<<line;
    return 0;
}
*/
int pos;
void dfs(int u)
{
    vis[u]=1;int i=H[u];
    while(i+1){
        int v=E[i].v,w=E[i].w;
        if(!vis[v]){
            dis[v]=dis[u]+w;
            if(dis[v]>dis[pos])pos=v;
            dfs(v);
        }
        i=E[i].pre;
    }
}
int main()
{
    memset(H,-1,sizeof(H));
    int n=read(),u,v;
    for(int i=1;i<n;++i)u=read(),v=read(),add(u,v,1),add(v,u,1);
    int s,t;
    dfs(1);t=pos;
    pos=0,memset(vis,0,sizeof(vis));memset(dis,0,sizeof(dis));
    dfs(t);s=pos;memcpy(dt,dis,sizeof(dis));
    pos=0,memset(vis,0,sizeof(vis));memset(dis,0,sizeof(dis));
    dfs(s);memcpy(ds,dis,sizeof(dis));
    int id=0,ans=0;
    for(int i=1;i<=n;++i){
        if(i==s||i==t)continue;
        int res=(ds[i]+dt[i]+ds[t])/2;
        if(res>ans)ans=res,id=i;
    }
    cout<<ans<<line<<s<<" "<<t<<" "<<id<<line;
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/ZeroOne-World/p/12383567.html
今日推荐