跳到正文

题解

题解:P13555 【MX-X15-T2】系绳绳

woshi_dabian约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
1
题目
P13555

版本

  1. v1当前

怎么想这道题?

思路很简单,大家开动脑筋想一想下面的话:

1.1. 任意两个度为 11 的点之间只有以其中一个作为根节点才可以连接。

2.2. 如果所有度为 11 的节点相互之间都被连接,那么整颗树上的点都被相互连接了。因为其他点一定在度为 11 的点之间的路径上。

3.3. 这题想到这就可以装逼说这是水题了。直接统计度为1的点的个数为 cntcnt,然后 输出 cnt−1cnt-1。这是所有度为1的点之间有绳子系的必要条件。

4.4. Charles Leclerc

5.5. 边提交边装逼然后95pts装逼失败。 因为你忘了处理 n=1n=1 时应输出0的边界。

上AC code

C++
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll N = 1e5 + 10;

ll D[N];

int main() {
	ll T;
	cin >> T;
	ll n;
	ll a, b;
	ll ans;
	while (T--) {
		cin >> n;
		for (int i = 1; i <= n; i++) {
			D[i] = 0;
		}
		for (int i = 1; i < n; i++) {
			cin >> a >> b;
			D[a]++;
			D[b]++;
		}
		if (n == 1) {
			cout << 0 << endl;
			continue;
		}
		ans = -1;
		for (int i = 1; i <= n; i++) {
			if (D[i] == 1) {
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
原文