Submission #1114228

#TimeUsernameProblemLanguageResultExecution timeMemory
1114228Dan4LifeMergers (JOI19_mergers)C++17
48 / 100
3039 ms91572 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define sz(a) (int)a.size() #define all(a) begin(a),end(a) #define int long long using ar2 = array<int,2>; const int N = (int)5e5+10; const int LINF = (int)1e18; int n, k; vector<int> adj[N]; int p[N], sz[N]; int a[N], tot[N], deg[N]; set<ar2> S[N]; int findSet(int i){return i==p[i]?i:p[i]=findSet(p[i]);} void unionSet(int i, int j){ int x = findSet(i), y = findSet(j); if(x==y) return; if(sz[x]<sz[y])swap(x,y); p[y] = x, sz[x]+=sz[y]; } void dfs(int s, int p){ S[s].insert({a[s],1}); for(auto u : adj[s]){ if(u==p) continue; dfs(u,s); if(sz(S[s]) < sz(S[u])) S[s].swap(S[u]); for(auto [v,w] : S[u]){ auto itr = S[s].lower_bound({v,-1}); int cnt = 0; if(itr!=end(S[s]) and (*itr)[0]==v) cnt = ((*itr)[1]), S[s].erase(itr); S[s].insert({v,cnt+w}); } } int ok = 1; for(auto [u,v] : S[s]) ok&=(tot[u]==v); if(p and !ok) unionSet(s,p); } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n; i++) p[i]=i,sz[i]=1; for(int i = 1; i < n; i++){ int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } for(int i = 1; i <= n; i++) cin >> a[i], tot[a[i]]++; dfs(1,0); for(int i = 1; i <= n; i++) for(int j : adj[i]) if(findSet(i)!=findSet(j)) deg[findSet(i)]++; int cnt = 1; for(int i = 1; i <= n; i++) cnt+=(deg[i]==1); cout << cnt/2 << "\n"; }
#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...