CF1905B Begginer‘s Zelda 题解

题面翻译

给你一棵 n n n 个点的树,操作是对于 u , v u,v u,v,将 u u u v v v 的路径上的点全都缩成一个点。问最少几次操作使得整棵树变为一个点。

题目描述

You are given a tree $ ^{\dagger} $ . In one zelda-operation you can do follows:

  • Choose two vertices of the tree $ u $ and $ v $ ;
  • Compress all the vertices on the path from $ u $ to $ v $ into one vertex. In other words, all the vertices on path from $ u $ to $ v $ will be erased from the tree, a new vertex $ w $ will be created. Then every vertex $ s $ that had an edge to some vertex on the path from $ u $ to $ v $ will have an edge to the vertex $ w $ .

Illustration of a zelda-operation performed for vertices $ 1 $ and $ 5 $ .Determine the minimum number of zelda-operations required for the tree to have only one vertex.

$ ^{\dagger} $ A tree is a connected acyclic undirected graph.

输入格式

Each test consists of multiple test cases. The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $ n $ ( $ 2 \le n \le 10^5 $ ) — the number of vertices.

$ i $ -th of the next $ n − 1 $ lines contains two integers $ u_i $ and $ v_i $ ( $ 1 \le u_i, v_i \le n, u_i \ne v_i $ ) — the numbers of vertices connected by the $ i $ -th edge.

It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .

输出格式

For each test case, output a single integer — the minimum number of zelda-operations required for the tree to have only one vertex.

样例 #1

样例输入 #1

4
4
1 2
1 3
3 4
9
3 1
3 5
3 2
5 6
6 7
7 8
7 9
6 4
7
1 2
1 3
2 4
4 5
3 6
2 7
6
1 2
1 3
1 4
4 5
2 6

样例输出 #1

1
3
2
2

提示

In the first test case, it’s enough to perform one zelda-operation for vertices $ 2 $ and $ 4 $ .

In the second test case, we can perform the following zelda-operations:

  1. $ u = 2, v = 1 $ . Let the resulting added vertex be labeled as $ w = 10 $ ;
  2. $ u = 4, v = 9 $ . Let the resulting added vertex be labeled as $ w = 11 $ ;
  3. $ u = 8, v = 10 $ . After this operation, the tree consists of a single vertex.

题目思路

根据题目,每次操作可以将一条链缩为一个点。

观察发现,每条链最多可以覆盖 2 2 2 个叶子结点,在这种情况下是最优的。

因此,若叶子结点的数量为 x x x,则答案为 x + 1 2 \frac{x+1}{2} 2x+1

AC 代码

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
ll t,n,u,v,a[100010],ans;
int main()
{
	cin >> t;
	while(t--){
		memset(a, 0, sizeof(a));
		ans = 0;
		cin >> n;
		for(int i = 1;i <= n - 1;i++){
			cin >> u >> v;
			a[u]++,a[v]++;
		}
		for(int i = 1;i <= n;i++){
			if(a[i] == 1) ans++;
		}
		cout << (ans + 1) / 2 << endl;
	}
	return 0;
}

创作不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,如果喜欢我的文章,给个关注吧!

冰焰狼 | 文

如果本篇博客有任何错误,请批评指教,不胜感激 !

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值