Welcome to Liukejie's blog🎉|

liukejie

园龄:1年8个月粉丝:5关注:11

T513748 分形树

题目传送门

纪念 920 号, pyf 和 lkj....

思路,对于每一个 k 维树,我们可以发现,最后一定会有大树一条树的直径连接着 k1 维树

比如

1.41.21.11.3 这条大树的直径连接着两个 1 维树,我们可以构想如果下面还有树,那么也是树的直径 + 小树的直径。继续下去,那就是 k1 个小树的直径。由于大树的直径有两个端点,所以要 ×2

const int N = 2e5 + 10;
int n, k, ans1, ans2, max1i, max2i;
struct E{int son;};
vector<E> V[N];
void add(int x, int y) {V[x].push_back({y});}
void dfs(int x, int Fa, int dep)
{
	for(auto i:V[x])
	{
		if(i.son == Fa) continue;		
		if(ans1 < dep) ans1 = dep, max1i = i.son;
		dfs(i.son, x, dep + 1);
	}
}
void dfs1(int x, int Fa, int dep)
{
	for(auto i:V[x])
	{
		if(i.son == Fa) continue;		
		if(ans2 < dep) ans2 = dep, max2i = i.son;
		dfs1(i.son, x, dep + 1);
	}
}
signed main()
{
	read(n); read(k);
	rep(i, 2, n)
	{
		int x, y; read(x); read(y);
		add(x, y); add(y, x);
	}
	dfs(1, 0, 1);
	dfs1(max1i, 0, 1);
	cout << ans1 * (k - 1) * 2 + ans2 << '\n';
	return 0;
}
posted @   liukejie  阅读(5)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起