Submission #102728

#TimeUsernameProblemLanguageResultExecution timeMemory
102728psmaoMergers (JOI19_mergers)C++14
100 / 100
1187 ms87288 KiB
#include <bits/stdc++.h> using namespace std; #define fo(i,s,t) for(int i = s; i <= t; ++ i) #define fd(i,s,t) for(int i = s; i >= t; -- i) #define bf(i,s) for(int i = head[s]; i; i = e[i].next) #define mp make_pair #define fi first #define se second #define pii pair<int,int> #define pb push_back #define VI vector<int> #define sf scanf #define pf printf #define fp freopen #define SZ(x) ((int)(x).size()) #ifdef MPS #define D(x...) printf(x) #else #define D(x...) #endif typedef long long ll; typedef double db; typedef unsigned long long ull; const int inf = 1<<30; const ll INF = 1ll<<60; const db Inf = 1e20; const db eps = 1e-9; void gmax(int &a,int b){a = (a > b ? a : b);} void gmin(int &a,int b){a = (a < b ? a : b);} const int maxn = 500050; int n, K, fa[maxn], par[maxn], dep[maxn], deg[maxn]; VI adj[maxn], col[maxn]; int getfa(int x) {return fa[x] == x ? x : fa[x] = getfa(fa[x]);} void merge(int u, int v) {u = getfa(u); v = getfa(v); fa[u] = v;} void combine(int u, int v) { while(u != v) { if(dep[u] > dep[v]) { merge(u, par[u]); u = getfa(par[u]); } else { merge(v, par[v]); v = getfa(par[v]); } } } void dfs(int u, int f = 0) { for(auto p : adj[u]) if(p != f) { par[p] = u; dep[p] = dep[u] + 1; dfs(p, u); } } int main() { #ifdef MPS fp("1.in","r",stdin); fp("1.out","w",stdout); #endif sf("%d%d",&n,&K); fo(i,2,n) { int u, v; sf("%d%d",&u,&v); adj[u].pb(v); adj[v].pb(u); } fo(i,1,n) fa[i] = i; fo(i,1,n) {int x; sf("%d",&x); col[x].pb(i);} dfs(1); fo(i,1,K) fo(j,1,SZ(col[i])-1) combine(col[i][j], col[i][0]); fo(i,1,n) for(auto p : adj[i]) if(getfa(i) != getfa(p) && dep[i] + 1 == dep[p]) { ++ deg[getfa(i)]; ++ deg[getfa(p)]; } int leaves = 0; fo(i,1,n) if(getfa(i) == i && deg[i] == 1) ++ leaves; pf("%d\n",(int)ceil(leaves/2.0)); return 0; }

Compilation message (stderr)

mergers.cpp: In function 'int main()':
mergers.cpp:71:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d%d",&n,&K);
    ^
mergers.cpp:74:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; sf("%d%d",&u,&v);
               ^
mergers.cpp:78:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fo(i,1,n) {int x; sf("%d",&x); col[x].pb(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...