Submission #967014

#TimeUsernameProblemLanguageResultExecution timeMemory
967014blackslexCapital City (JOI20_capital_city)C++17
100 / 100
239 ms58448 KiB
#include<bits/stdc++.h> using namespace std; int n, k, x, y; int main() { scanf("%d %d", &n, &k); vector<int> c(n + 5), st(k + 5), ca(k + 5); vector<vector<int>> v(n + 5, vector<int>()), v2(k + 5, vector<int>()), rev(k + 5, vector<int>()); for (int i = 1; i < n; i++) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x); for (int i = 1; i <= n; i++) scanf("%d", &c[i]), ca[c[i]]++; int t = 0; vector<int> sz(n + 5), hv(n + 5), pos(n + 5), tin(n + 5), tout(n + 5); function<void(int, int)> dfs = [&] (int cur, int par) { tin[cur] = ++t; pos[t] = cur; sz[cur] = 1; for (auto &e: v[cur]) { if (par == e) continue; dfs(e, cur); sz[cur] += sz[e]; if (sz[e] > sz[hv[cur]]) hv[cur] = e; } tout[cur] = t; }; vector<int> col(n + 5), cnt(k + 5); auto add = [&] (int idx) { cnt[c[idx]]++; }; auto del = [&] (int idx) { cnt[c[idx]]--; }; function<void(int, int, bool)> dfs2 = [&] (int cur, int par, bool ck) { for (auto &e: v[cur]) if (par != e && hv[cur] != e) dfs2(e, cur, 1); if (hv[cur]) dfs2(hv[cur], cur, 0); add(cur); for (auto &e: v[cur]) if (par != e && hv[cur] != e) for (int i = tin[e]; i <= tout[e]; i++) add(pos[i]); col[cur] = cnt[c[cur]]; if (ck) for (int i = tin[cur]; i <= tout[cur]; i++) del(pos[i]); }; dfs(1, 0); dfs2(1, 0, 0); int ans = 1e9; vector<int> indeg(k + 5), outdeg(k + 5); function<void(int, int)> dfs3 = [&] (int cur, int par) { if (par && c[cur] != c[par]) { // cerr << cur << ' ' << ca[c[cur]] << ' ' << col[cur] << '\n'; if (ca[c[cur]] - col[cur]) v2[c[cur]].emplace_back(c[par]), indeg[c[par]]++, outdeg[c[cur]]++, rev[c[par]].emplace_back(c[cur]); } for (auto &e: v[cur]) { if (par == e) continue; dfs3(e, cur); } }; dfs3(1, 0); queue<int> q; for (int i = 1; i <= k; i++) if (!indeg[i]) q.emplace(i); while (!q.empty()) { int cur = q.front(); q.pop(); for (auto &e: v2[cur]) if (!(--indeg[e])) q.emplace(e); } stack<int> cst; int csz = 0; vector<bool> f(k + 5), f2(k + 5); function<void(int, int)> dfs4 = [&] (int cur, int par) { if (f[cur]) return; f[cur] = 1; for (auto &e: v2[cur]) { if (par == e) continue; dfs4(e, cur); } cst.emplace(cur); }; function<void(int, int)> dfs5 = [&] (int cur, int par) { if (f2[cur]) return; f2[cur] = 1; csz++; for (auto &e: rev[cur]) { if (par == e) continue; dfs5(e, cur); } }; for (int i = 1; i <= k; i++) if (!outdeg[i]) ans = 0; for (int i = 1; i <= k; i++) if (!indeg[i]) f[i] = f2[i] = 1; for (int i = 1; i <= k; i++) if (!f[i]) dfs4(i, 0); while (!cst.empty()) { int cur = cst.top(); cst.pop(); if (f2[cur]) continue; csz = 0; dfs5(cur, 0); ans = min(ans, csz - 1); } printf("%d", ans); }

Compilation message (stderr)

capital_city.cpp: In lambda function:
capital_city.cpp:33:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   33 |         if (hv[cur]) dfs2(hv[cur], cur, 0); add(cur);
      |         ^~
capital_city.cpp:33:45: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   33 |         if (hv[cur]) dfs2(hv[cur], cur, 0); add(cur);
      |                                             ^~~
capital_city.cpp: In lambda function:
capital_city.cpp:63:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   63 |         if (f[cur]) return; f[cur] = 1;
      |         ^~
capital_city.cpp:63:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   63 |         if (f[cur]) return; f[cur] = 1;
      |                             ^
capital_city.cpp: In lambda function:
capital_city.cpp:71:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   71 |         if (f2[cur]) return; f2[cur] = 1; csz++;
      |         ^~
capital_city.cpp:71:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   71 |         if (f2[cur]) return; f2[cur] = 1; csz++;
      |                              ^~
capital_city.cpp: In function 'int main()':
capital_city.cpp:82:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   82 |         if (f2[cur]) continue; csz = 0;
      |         ^~
capital_city.cpp:82:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   82 |         if (f2[cur]) continue; csz = 0;
      |                                ^~~
capital_city.cpp:8:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:11:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |     for (int i = 1; i < n; i++) scanf("%d %d", &x, &y), v[x].emplace_back(y), v[y].emplace_back(x);
      |                                 ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:12:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     for (int i = 1; i <= n; i++) scanf("%d", &c[i]), ca[c[i]]++;
      |                                  ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...