个人介绍
增加 2 行删除 77 行
14 行未改动1516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959614 行未改动600道题纪念 2026.8.14 19:23650道题纪念 2026.8.29 15:36700道题预告 2026.11.2 11:57```cpp#include <bits/stdc++.h>#define ll long long#define inl inlineusing namespace std;inl int read() {int s = 0;char ch = getchar();while (ch < '0' || ch > '9')ch = getchar();while (ch <= '9' && ch >= '0')s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();return s;}const int N = 100005;int n, k, ans, fa[N], sz[N], son[N], del[N];vector<int> e[N];void dfs1(int x) {sz[x] = 1, son[x] = 0;for (int i = 0, szz = e[x].size(); i < szz; ++i) {int y = e[x][i];if (y == fa[x])continue;fa[y] = x;dfs1(y);if (sz[y] >= k)++son[x];sz[x] += sz[y];}}void dfs2(int x) {del[x] = 0;int mx = -0x3f3f3f3f, mxx = -0x3f3f3f3f;for (int i = 0, szz = e[x].size(); i < szz; ++i) {int y = e[x][i];if (y == fa[x])continue;dfs2(y);if (max(del[y], son[y]) - (sz[y] >= k) > mx)mxx = mx, mx = max(del[y], son[y]) - (sz[y] >= k);else if (max(del[y], son[y]) - (sz[y] >= k) > mxx)mxx = max(del[y], son[y]) - (sz[y] >= k);del[x] = max(del[x], son[x] - (sz[y] >= k) + max(del[y], son[y]));}if (sz[x] > 1)ans = max(ans, del[x] + (sz[1] - sz[x] >= k));ans = max(ans, son[x] + mx + mxx + (sz[1] - sz[x] >= k));}void run() {n = read(), k = read();for (int i = 1; i <= n; ++i)e[i].clear(), fa[i] = 0;for (int i = 1, x, y; i < n; ++i) {x = read(), y = read();e[x].push_back(y), e[y].push_back(x);}dfs1(1);ans = 0;dfs2(1);printf("%d\n", ans);}int main() {freopen("tree.in", "r", stdin);freopen("tree.out", "w", stdout);int t;scanf("%d", &t);for (; t; --t)run();return 0;}```14 行未改动1516171819202114 行未改动600道题纪念 2026.8.14 19:23650道题纪念 2026.8.29 15:36700道题纪念 2026.11.2 11:57750道题纪念 2026.11.16 15:37
奖项
增加 1 行删除 0 行
12342 行未改动2026 NOI 女生竞赛 三等奖2025 CSP-S 二等奖2025 NOIP 二等奖2024 CSP-J 一等奖2 行未改动123452 行未改动2026 CSP-S 一等奖2026 NOI 女生竞赛 三等奖2025 CSP-S 二等奖2025 NOIP 二等奖2024 CSP-J 一等奖2 行未改动