Submission #537339

#TimeUsernameProblemLanguageResultExecution timeMemory
537339wiwihoCapital City (JOI20_capital_city)C++14
11 / 100
3055 ms32256 KiB
#include <bits/stdc++.h> #define mp make_pair #define F first #define S second #define eb emplace_back #define iter(a) a.begin(), a.end() #define lsort(a) sort(iter(a)) #define gsort(a) sort(iter(a), greater<>()) using namespace std; using pii = pair<int, int>; int n, m; vector<int> C; vector<vector<int>> g; vector<vector<int>> cv; vector<int> pr; void dfs(int now, int p){ pr[now] = p; for(int i : g[now]){ if(i == p) continue; dfs(i, now); } } int solve(int s){ dfs(s, s); vector<bool> ok(n + 1), use(m + 1); queue<int> q; int clr = C[s]; use[clr] = true; for(int i : cv[clr]) q.push(i); ok[s] = true; while(!q.empty()){ int v = q.front(); q.pop(); while(!ok[v]){ ok[v] = true; if(!use[C[v]]){ use[C[v]] = true; for(int i : cv[C[v]]) q.push(i); } v = pr[v]; } } int ans = 0; for(int i = 1; i <= m; i++) ans += use[i]; return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; C.resize(n + 1); g.resize(n + 1); pr.resize(n + 1); cv.resize(m + 1); for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; g[u].eb(v); g[v].eb(u); } for(int i = 1; i <= n; i++){ cin >> C[i]; cv[C[i]].eb(i); } int ans = n; for(int i = 1; i <= n; i++){ ans = min(ans, solve(i) - 1); } cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...