跳到正文

个人记录

/bangbangt

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

文章信息

发布
2026年8月1日
收录
2026年8月1日
更新
2026年11月17日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100005

vector<ll> e[MAXN];   // 大小 MAXN 足够
struct node {
	ll du, id;
} asd[MAXN];

bool cmp(node &x, node &y) {
	if (x.du != y.du) return x.du > y.du;
	return x.id < y.id;   // 相等时返回 false(不交换)
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int tt;
	cin >> tt;
	while (tt--) {
		int n;
		cin >> n;
		
		// ---------- 清空 ----------
		for (int i = 1; i <= n; i++) {
			e[i].clear();
			asd[i].du = 0;
			asd[i].id = i;
		}
		
		for (int i = 1; i <= n - 1; i++) {
			int u, v;
			cin >> u >> v;
			e[u].push_back(v);
			e[v].push_back(u);
			asd[u].du++;
			asd[v].du++;
		}
		
		sort(asd + 1, asd + 1 + n, cmp);
		int ans = 0;
		ans += asd[1].du;
		asd[1].du = -MAXN;
		for (auto c : e[asd[1].id]) {
			asd[c].du--;
		}
		
		sort(asd + 1, asd + 1 + n, cmp);
		ans += asd[1].du;
		cout << ans << '\n';
	}
	return 0;
}
原文