Submission #220191

#TimeUsernameProblemLanguageResultExecution timeMemory
220191fedoseevtimofeyCapital City (JOI20_capital_city)C++14
11 / 100
524 ms8952 KiB
#include <iostream> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <random> #include <iomanip> #include <functional> #include <cassert> using namespace std; typedef long long ll; const int N = 3000 + 7; vector <int> g[N]; vector <int> gr[N]; vector <int> rg[N]; int c[N]; bool bad[N][N]; vector <int> path; void dfs(int u, int p = -1) { path.push_back(u); if (c[path.back()] == c[path.front()]) { for (int v : path) { if (c[v] != c[u]) { bad[c[u]][c[v]] = 1; } } } for (auto v : g[u]) { if (v != p) { dfs(v, u); } } path.pop_back(); } vector <int> t; bool used[N]; int cmp[N]; bool ok[N]; int sz[N]; void get_topsort(int u) { used[u] = true; for (auto v : gr[u]) { if (!used[v]) { get_topsort(v); } } t.push_back(u); } int cnt = 0; void jhfs(int u) { used[u] = true; cmp[u] = cnt; ++sz[cnt]; for (auto v : rg[u]) { if (!used[v]) { jhfs(v); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int n, k; cin >> n >> k; for (int i = 0; i + 1 < n; ++i) { int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); g[v].push_back(u); } for (int i = 0; i < n; ++i) { cin >> c[i]; --c[i]; } for (int i = 0; i < n; ++i) { dfs(i, -1); } for (int i = 0; i < k; ++i) { for (int j = 0; j < k; ++j) { if (bad[i][j]) { gr[i].push_back(j); rg[j].push_back(i); } } } for (int i = 0; i < k; ++i) { if (!used[i]) { get_topsort(i); } } reverse(t.begin(), t.end()); fill(used, used + k, false); for (int u : t) { if (!used[u]) { jhfs(u); ++cnt; } } fill(ok, ok + cnt, true); for (int i = 0; i < k; ++i) { for (int j : gr[i]) { if (cmp[i] != cmp[j]) { ok[cmp[i]] = false; } } } int ans = n; for (int i = 0; i < cnt; ++i) { if (ok[i]) ans = min(ans, sz[i]); } cout << ans - 1 << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...