Submission #105887

#TimeUsernameProblemLanguageResultExecution timeMemory
105887KepperoniMergers (JOI19_mergers)C++14
100 / 100
1345 ms63452 KiB
#include <bits/stdc++.h> #define x first #define y second #define pb push_back #define all(x) x.begin(), x.end() using namespace std; typedef long long ll; typedef pair<ll, ll> pii; const int MAXN = 5e5 + 10; int n, kk; int gr[MAXN]; int le[MAXN], ri[MAXN]; int fi[MAXN], se[MAXN]; int cmi[MAXN], cma[MAXN], cnt[MAXN], tr[MAXN]; vector<int> k[MAXN]; int leaf = 1; int ccnt = 1; void prep(int v, int p = 0){ le[v] = ccnt; ri[v] = ccnt; fi[gr[v]] = min(fi[gr[v]], ccnt); se[gr[v]] = max(se[gr[v]], ccnt); ccnt++; for(auto x : k[v]){ if(x != p){ prep(x, v); ri[v] = ri[x]; } } } void go(int v, int p = 0){ cmi[v] = fi[gr[v]]; cma[v] = se[gr[v]]; for(auto x : k[v]){ if(x != p){ go(x, v); cmi[v] = min(cmi[v], cmi[x]); cma[v] = max(cma[v], cma[x]); cnt[v] += cnt[x]; } } if(cmi[v] >= le[v] && cma[v] <= ri[v]){ if(cnt[v] == 0 && v != 1) leaf++; if(cnt[v] == 1 && v == 1) leaf++; cnt[v] = 1; } } int main(){ scanf(" %d %d", &n, &kk); for(int i=0; i<n-1; i++){ int ca, cb; scanf(" %d %d", &ca, &cb); k[ca].pb(cb); k[cb].pb(ca); } for(int i=1; i<=n; i++) scanf(" %d", &gr[i]); for(int i=1; i<=kk; i++) fi[i] = 1e6; prep(1); go(1); printf("%d\n", leaf/2); }

Compilation message (stderr)

mergers.cpp: In function 'int main()':
mergers.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf(" %d %d", &n, &kk);
  ~~~~~^~~~~~~~~~~~~~~~~~~
mergers.cpp:64:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int ca, cb; scanf(" %d %d", &ca, &cb);
               ~~~~~^~~~~~~~~~~~~~~~~~~~
mergers.cpp:69:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1; i<=n; i++) scanf(" %d", &gr[i]);
                          ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...