怎么想这道题?
思路很简单,大家开动脑筋想一想下面的话:
任意两个度为 的点之间只有以其中一个作为根节点才可以连接。
如果所有度为 的节点相互之间都被连接,那么整颗树上的点都被相互连接了。因为其他点一定在度为 的点之间的路径上。
这题想到这就可以装逼说这是水题了。直接统计度为1的点的个数为 ,然后 输出 。这是所有度为1的点之间有绳子系的必要条件。
Charles Leclerc
边提交边装逼然后95pts装逼失败。 因为你忘了处理 时应输出0的边界。
上AC code
#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;
}