Submission #1082758

#TimeUsernameProblemLanguageResultExecution timeMemory
1082758EvirirCapital City (JOI20_capital_city)C++17
11 / 100
3053 ms42780 KiB
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> using namespace std; //using namespace __gnu_pbds; // #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2") #define setp(x) cout<<fixed<<setprecision(x) #define sz(x) (int)(x).size() #define all(x) begin(x), end(x) #define forn(i,a,b) for(int i=(a);i<(b);i++) #define fore(i,a,b) for(int i=(a);i<=(b);i++) #define pb push_back #define F first #define S second #define fbo find_by_order #define ook order_of_key typedef long long ll; typedef long double ld; typedef pair<ll,ll> ii; typedef vector<ll> vi; typedef vector<ii> vii; //template<typename T> //using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; void SD(int t=0){ cout<<"PASSED "<<t<<endl; } template<typename T> void amax(T &a, T b){ a=max(a,b); } template<typename T> void amin(T &a, T b){ a=min(a,b); } struct Hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; const ll INF = ll(1e18); const int MOD = 998244353; const bool DEBUG = 0; const int MAXN = 100005; const int LG = 21; int n,K; vector<vector<int>> adj; vector<int> c, sub, cnt; vector<bool> cvst; void dfs_sz(int u, int p) { sub[u] = 1; for (const auto v : adj[u]) { if (v == p || cvst[v]) continue; dfs_sz(v, u); sub[u] += sub[v]; } } int centroid(int u, int tot) { int p = -1; while (true) { bool ok = 1; for (const auto v : adj[u]) { if (v == p || cvst[v]) continue; if (sub[v] * 2 > tot) { p = u; u = v; ok = 0; break; } } if (ok) return u; } } unordered_map<int, int> s; unordered_map<int, vector<int>> prt; void dfs(int u, int p) { s[c[u]]++; if (p != -1) prt[c[u]].pb(c[p]); for (const auto v : adj[u]) { if (v == p || cvst[u]) continue; dfs(v, u); } } int ans = 1e9; void solve(int u) { dfs_sz(u, -1); int tot = sub[u]; u = centroid(u, tot); s.clear(); prt.clear(); dfs(u, -1); unordered_set<int> need{c[u]}; vector<int> q{c[u]}; while (!q.empty()) { int curc = q.back(); q.pop_back(); for (const auto x : prt[curc]) { if (need.count(x)) continue; need.insert(x); q.pb(x); } } bool ok = 1; for (const auto x : need) { if (cnt[x] != s[x]) { ok = 0; break; } } if (ok) amin(ans, sz(need) - 1); cvst[u] = 1; for (const auto v : adj[u]) { if (cvst[v]) continue; solve(v); } } int main() { cin.tie(0)->sync_with_stdio(0); cin>>n>>K; adj.resize(n); c.resize(n); cvst.resize(n); sub.resize(n); cnt.resize(K); forn(i,0,n-1) { int u,v; cin>>u>>v; u--; v--; adj[u].pb(v); adj[v].pb(u); } forn(i,0,n) { cin>>c[i]; c[i]--; cnt[c[i]]++; } solve(0); 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...