Codeforces Gym102460 B-The Power Monitor System (树上
Posted cminus
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Gym102460 B-The Power Monitor System (树上相关的知识,希望对你有一定的参考价值。
我无法形容出这道题的题解,实在是太麻烦了,但是自己开的题,跪着也要做完,放个std仅供对拍
#include <cstdio> #include <algorithm> #include <vector> #include <cstring> using namespace std; const int N = 100010; vector < int > E[N]; int fa[N], d[N][3][3]; void dfs(int u) { int tot1 = 0, tot2 = 0, maxn = -N, maxn2 = -N, maxn3 = -N; if (E[u].size() == 1 && u != 1) { d[u][0][0] = 0; d[u][1][1] = 1; d[u][1][0] = 1; return ; } if (u == 1 && E[u].size() == 1) { fa[E[u][0]] = u; dfs(E[u][0]); d[u][1][0] = d[u][1][1] = min(d[E[u][0]][1][1], d[E[u][0]][0][0] + 1); return ; } if (E[u].size() == 2 && u != 1) { int v; if (fa[u] == E[u][0]) v = E[u][1]; else v = E[u][0]; fa[v] = u; dfs(v); d[u][1][0] = d[u][1][1] = min(d[v][1][1], d[v][0][0] + 1); d[u][0][0] = min(d[v][1][0], d[v][0][0]); return ; } for (int i = 0; i < E[u].size(); i++) { int v = E[u][i]; if (v == fa[u]) continue; fa[v] = u; dfs(v); tot1 += d[v][1][0]; tot2 += d[v][0][0]; maxn = max(maxn, d[v][1][0] - d[v][1][1] + maxn2); maxn = max(maxn, d[v][1][0] - d[v][0][0] + maxn3); maxn2 = max(maxn2, d[v][1][0] - d[v][0][0]); maxn3 = max(maxn3, d[v][1][0] - d[v][1][1]); } d[u][0][0] = tot1 - maxn2; d[u][1][0] = tot1 - maxn; d[u][1][1] = min(tot2 + 1, tot1 - maxn3); d[u][1][0] = min(d[u][1][0], d[u][1][1]); d[u][0][0] = min(d[u][0][0], d[u][1][0]); } int main() { int n; scanf("%d", &n); memset(d, 0x3f, sizeof(d)); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); E[u].push_back(v); E[v].push_back(u); } dfs(1); //for (int i = 1; i <= n; i++) // printf("%d %d %d ", d[i][1][0], d[i][1][1], d[i][0][0]); printf("%d", min(d[1][1][0], d[1][1][1])); return 0; }
以上是关于Codeforces Gym102460 B-The Power Monitor System (树上的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces-gym-101020 problem C. Rectangles