Three Paths on a Tree(DFS树的直径)

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
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,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.
讲真,这个题比E题简单多了。。
找出三个点来,使得这三个点所连得边最多。
思路:有两个点肯定是直径的端点。我们把直径那条链想象成直线。那么连出去的那些分支我们挑选一个最大的就可以了。主要就是DFS的应用。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
struct edge{
	int next;
	int to;
}e[maxx<<1];
int vis[maxx];
int head[maxx];
int n,tot;
inline void mem()
{
	for(int i=0;i<=n+1;i++) vis[i]=0;
}
inline void add(int u,int v)
{
	e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
inline void dfs(int u,int &_max,int &pos,int cnt)
{
	if(vis[u]) return ;
	vis[u]=1;
	if(cnt>_max)
	{
		_max=cnt;
		pos=u;
	}
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		dfs(to,_max,pos,cnt+1);
	}
}
inline int getpath(int u,int mak,vector<int> &p)
{
	if(vis[u]) return 0;
	p.push_back(u);
	vis[u]=1;
	if(mak==u) return 1;
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(getpath(to,mak,p)) return 1;
	}
	p.erase(p.begin()+p.size()-1);
	return 0;
}
inline void getmax(int u,int &_max,int &pos,int cnt)
{
	if(vis[u]==1) return ;
	vis[u]=1;
	if(cnt>_max)
	{
		_max=cnt;
		pos=u;
	}
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(vis[to]==-1) continue;
		dfs(to,_max,pos,cnt+1);
	}
}
int main()
{
	scanf("%d",&n);
	int x,y;tot=0;
	memset(head,-1,sizeof(head));
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
	}
	mem();
	int _max=0,poi,poi1;
	dfs(1,_max,poi,0);
	mem();
	_max=0;
	dfs(poi,_max,poi1,0);
	vector<int> p;
	mem();
	getpath(poi,poi1,p);
	int max1=0,pos;
	mem();
	for(int i=0;i<p.size();i++) vis[p[i]]=-1;
	for(int i=0;i<p.size();i++)
	{
		x=0;
		getmax(p[i],x,y,0);
		if(x>max1)
		{
			max1=x;
			pos=y;
		}
	}
	if(max1==0) cout<<_max<<endl<<p[0]<<" "<<p[1]<<" "<<p[p.size()-1]<<endl;//没有分支的情况下,就直接输出直径上的三个点就行。
	else cout<<_max+max1<<endl<<poi<<" "<<poi1<<" "<<pos<<endl;
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值