版本比较
woshnd
个人介绍
增加 2 行删除 77 行
14 行未改动15161718191920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495962114 行未改动600道题纪念 2026.8.14 19:23
650道题纪念 2026.8.29 15:36
700道题预告 2026.11.2 11:57700道题纪念 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;}```750道题纪念 2026.11.16 15:37奖项
增加 1 行删除 0 行
123452 行未改动2026 CSP-S 一等奖2026 NOI 女生竞赛 三等奖2025 CSP-S 二等奖2025 NOIP 二等奖2024 CSP-J 一等奖2 行未改动