Submission #405100

#TimeUsernameProblemLanguageResultExecution timeMemory
405100ryanseeCapital City (JOI20_capital_city)C++14
100 / 100
1087 ms42468 KiB
#include "bits/stdc++.h" using namespace std; #define FAST ios_base::sync_with_stdio(false); cin.tie(0); #define pb push_back #define eb emplace_back #define ins insert #define f first #define s second #define cbr cerr<<"hi\n" #define mmst(x, v) memset((x), v, sizeof ((x))) #define siz(x) ll(x.size()) #define all(x) (x).begin(), (x).end() #define lbd(x,y) (lower_bound(all(x),y)-x.begin()) #define ubd(x,y) (upper_bound(all(x),y)-x.begin()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusive string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); } using ll=long long; using ld=long double; #define FOR(i,s,e) for(ll i=s;i<=ll(e);++i) #define DEC(i,s,e) for(ll i=s;i>=ll(e);--i) using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>; long long LLINF = 1e18; int INF = 1e9+1e6; #define MAXN (200006) ll n, k, C[MAXN], ans, legit[MAXN]; vector<int> v[MAXN]; int total, sz[MAXN]; bitset<MAXN> done; void dfs1(int x,int p) { ++ total, sz[x] = 1; for(auto i:v[x]) if(i^p && !done[i]) dfs1(i, x), sz[x] += sz[i]; } int dfs2(int x,int p) { for(auto i:v[x]) if(i^p && !done[i] && sz[i]>total/2) return dfs2(i, x); return x; } vector<int> ppl[MAXN]; bitset<MAXN> vis; int P[MAXN]; void solve(int x) { function<void(int,int)>dfs=[&](int x,int p) { P[x]=p, ppl[C[x]].eb(x); for(auto i:v[x]) if(i^p && !done[i]) dfs(i, x); }; dfs(x, x); queue<int> q; ll sum = 0; if(ppl[C[x]].size() != legit[C[x]]) sum = LLINF; for(auto i:ppl[C[x]]) vis[i]=1, q.emplace(i); ppl[C[x]].clear(); while(q.size()) { int x = q.front(); q.pop(); sum += !ppl[C[x]].empty(); if(ppl[C[x]].size() && ppl[C[x]].size() != legit[C[x]]) sum = LLINF; while(ppl[C[x]].size()) { if(!vis[ppl[C[x]].back()]) vis[ppl[C[x]].back()]=1, q.emplace(ppl[C[x]].back()); ppl[C[x]].pop_back(); } if(!vis[P[x]]) vis[P[x]]=1, q.emplace(P[x]); } function<void(int,int)>dfs2=[&](int x,int p) { vis[x] = 0, ppl[C[x]].clear(); for(auto i:v[x]) if(i^p && !done[i]) dfs2(i, x); }; dfs2(x, x); ans = min(ans, sum); } void dfs3(int x) { total = 0; dfs1(x, x); x = dfs2(x, x); solve(x); done[x] = 1; for(auto i:v[x]) if(!done[i]) dfs3(i); } int main() { FAST cin>>n>>k, ans=k-1; FOR(i,1,n-1) { int a, b; cin>>a>>b; v[a].eb(b), v[b].eb(a); } FOR(i,1,n) cin>>C[i], ++legit[C[i]]; dfs3(1); cout<<ans<<'\n'; }

Compilation message (stderr)

capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:54:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   54 |  if(ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
capital_city.cpp:61:43: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   61 |   if(ppl[C[x]].size() && ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
      |                          ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...