Submission #428696

#TimeUsernameProblemLanguageResultExecution timeMemory
428696talant117408Capital City (JOI20_capital_city)C++17
100 / 100
1002 ms43584 KiB
/* Code written by Talant I.D. */ #include <bits/stdc++.h> //~ #include <ext/pb_ds/assoc_container.hpp> //~ using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; //~ typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define precision(n) fixed << setprecision(n) #define pb push_back #define ub upper_bound #define lb lower_bound #define mp make_pair #define eps (double)1e-9 #define PI 2*acos(0.0) #define endl "\n" #define sz(v) int((v).size()) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define OK cout << "OK" << endl; const int mod = 1e9+7; ll mode(ll a) { while (a < 0) { a += mod; } return a % mod; } ll subt(ll a, ll b) { return mode(mode(a)-mode(b)); } ll add(ll a, ll b) { return mode(mode(a)+mode(b)); } ll mult(ll a, ll b) { return mode(mode(a)*mode(b)); } ll binpow(ll a, ll b) { ll res = 1; while (b) { if (b&1) res = mult(res, a); a = mult(a, a); b >>= 1; } return res; } const int N = 2e5+7; vector <int> graph[N], byColor[N]; int par[N], color[N], ind[N], timer, saizu[N], vis[N]; int ans, root_size, n; void find_size(int v, int p) { saizu[v] = 1; for (auto to : graph[v]) { if (vis[to] || to == p) continue; find_size(to, v); saizu[v] += saizu[to]; } } int find_centroid(int v, int p) { for (auto to : graph[v]) { if (vis[to] || to == p) continue; if (saizu[to]*2 > root_size) { return find_centroid(to, v); } } return v; } void create_tree(int v, int p) { ind[v] = timer; par[v] = p; for (auto to : graph[v]) { if (vis[to] || to == p) continue; create_tree(to, v); } } void init_centroid(int u) { find_size(u, u); root_size = saizu[u]; int centroid = find_centroid(u, u); vis[centroid] = 1; create_tree(centroid, centroid); queue <int> K; set <int> colors; bool outside = 1; colors.insert(color[centroid]); for (auto to : byColor[color[centroid]]) { K.push(to); if (ind[to] != timer) { outside = 0; break; } } while (sz(K)) { if (!outside) { break; } int v = K.front(); K.pop(); if (ind[v] != timer) { outside = 0; break; } if (color[par[v]] != color[v]) { auto it = colors.lb(color[par[v]]); if (it == colors.end() || *it != color[par[v]]) { colors.insert(color[par[v]]); for (auto to : byColor[color[par[v]]]) { K.push(to); if (ind[to] != timer) { outside = 0; break; } } } } } if (outside) { ans = min(ans, sz(colors)-1); } for (auto to : graph[centroid]) { if (!vis[to]) { timer++; init_centroid(to); } } } int main() { do_not_disturb int k; cin >> n >> k; ans = k+1; for (int i = 0; i < n-1; i++) { int x, y; cin >> x >> y; graph[x].pb(y); graph[y].pb(x); } for (int i = 1; i <= n; i++) { cin >> color[i]; byColor[color[i]].pb(i); } init_centroid(1); cout << ans; 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...