#include<bits/stdc++.h> using ll = longlong; usingnamespace std;
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int t; cin >> t;
while (t--) { int n; cin >> n;
vector<vector<int>> E(n); vector<int> deg(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--, v--; E[u].push_back(v); E[v].push_back(u); deg[u]++; deg[v]++; }
int res = 0; multiset<int> s; for (int u = 0; u < n; u++) { s.insert(deg[u]); } for (int u = 0; u < n; u++) { s.extract(deg[u]); for (auto v : E[u]) { s.extract(deg[v]); } if (!s.empty()) { res = max(res, deg[u] + *s.rbegin() - 1); } for (auto v : E[u]) { s.insert(deg[v]); } s.insert(deg[u]); for (auto v : E[u]) { res = max(res, deg[u] + deg[v] - 2); } }
#include<bits/stdc++.h> using ll = longlong; usingnamespace std;
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int t; cin >> t;
while (t--) { int n, m; cin >> n >> m;
deque<ll> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end());
int kmax = min({ (n + m) / 3, n, m }); cout << kmax << endl;
ll res = 0; stack<pair<ll, ll>> A, B; int cntA = n, cntB = m; auto adda = [&] { assert(cntA >= 2 && cntB >= 1); auto al = a.front(); a.pop_front(); auto ar = a.back(); a.pop_back(); res += ar - al; cntA -= 2; cntB -= 1; A.push({ al, ar }); }; auto addb = [&] { assert(cntB >= 2 && cntA >= 1); auto bl = b.front(); b.pop_front(); auto br = b.back(); b.pop_back(); res += br - bl; cntA -= 1; cntB -= 2; B.push({ bl, br }); }; auto dela = [&] { auto [al, ar] = A.top(); res -= ar - al; cntA += 2; cntB += 1; A.pop(); }; auto delb = [&] { auto [bl, br] = B.top(); res -= br - bl; cntA += 1; cntB += 2; B.pop(); }; for (int k = 0; k < kmax; k++) { if (cntA >= 2 && cntB >= 2 && a.back() - a.front() > b.back() - b.front() || cntA >= 2 && cntB == 1) { adda(); } elseif (cntB >= 2 && cntA >= 2 && a.back() - a.front() <= b.back() - b.front() || cntB >= 2 && cntA == 1) { addb(); } elseif (cntB == 0) { delb();[ // 反悔 adda(); adda(); } elseif (cntA == 0) { dela(); addb(); addb(); } else { assert(false); } cout << res << " "; } cout << endl; }