Submission #486580

#TimeUsernameProblemLanguageResultExecution timeMemory
486580i_am_noobMergers (JOI19_mergers)C++17
100 / 100
1056 ms129992 KiB
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,popcnt,tune=native") #define ll long long //#define int ll #define ull unsigned ll #define ld long double #define rep(a) rep1(i,a) #define rep1(i,a) rep2(i,0,a) #define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++) #define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--) #define chkmin(a,b) (a=min(a,b)) #define chkmax(a,b) (a=max(a,b)) #define all(a) a.begin(),a.end() #define pii pair<int,int> #define pb push_back #define inf 1010000000 //#define inf 4000000000000000000 #define eps 1e-9 #define sz(a) ((int)a.size()) #define pow2(x) (1ll<<(x)) #define ceiling(a,b) (((a)+(b)-1)/(b)) #define print0(a) cout << (a) << ' ' #define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #ifdef i_am_noob #define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__) template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";} template<typename T> void _do(T && x) {cerr << x << endl;} template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);} #else #define bug(...) 777771449 #endif template<typename T> void print(T && x) {cout << x << "\n";} template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);} const int Mod=1000000007,Mod2=998244353; const int MOD=Mod; const int maxn=500005,maxm=7777714,maxk=7777714; //i_am_noob //#define wiwihorz int n,k,id[maxn],dep[maxn],par[maxn][20],siz[maxn],val[maxn],stamp,comp[maxn],deg[maxn]; vector<int> adj[maxn],vec[maxn]; void dfs(int u, int fa){ id[u]=stamp++; siz[u]=1; par[u][0]=fa; dep[u]=fa==-1?0:dep[fa]+1; rep2(i,1,20) par[u][i]=par[u][i-1]==-1?-1:par[par[u][i-1]][i-1]; for(auto v: adj[u]) if(v!=fa){ dfs(v,u); siz[u]+=siz[v]; } } int lca(int u, int v){ if(dep[u]>dep[v]) swap(u,v); rep(20) if((dep[v]-dep[u])>>i&1) v=par[v][i]; if(u==v) return u; rep3(i,19,0) if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i]; return par[u][0]; } void dfs2(int u, int fa){ for(auto v: adj[u]) if(v!=fa){ dfs2(v,u); val[u]+=val[v]; } } void dfs3(int u, int fa){ if(fa!=-1&&siz[u]==val[u]){ comp[u]=++stamp; deg[comp[u]]++; deg[comp[fa]]++; } else comp[u]=fa==-1?0:comp[fa]; for(auto v: adj[u]) if(v!=fa){ dfs3(v,u); } } void orzck(){ cin >> n >> k; rep(n-1){ int u,v; cin >> u >> v; u--,v--; adj[u].pb(v),adj[v].pb(u); } rep(n){ int x; cin >> x; x--; vec[x].pb(i); } dfs(0,-1); rep(k) if(sz(vec[i])){ int x=vec[i][0]; for(auto u: vec[i]) x=lca(x,u); val[x]+=sz(vec[i]); /* sort(all(vec[i]),[](int i1, int i2){return id[i1]<id[i2];}); vector<int> stk; stk.pb(0); val[0]+=sz(vec[i]); for(auto u: vec[i]){ int x=lca(stk.back(),u); val[u]+=sz(vec[i]); val[x]-=sz(vec[i]); while(dep[stk.back()]>dep[x]) stk.pop_back(); if(stk.back()!=x) stk.pb(x); stk.pb(u); } */ } dfs2(0,-1); stamp=0; dfs3(0,-1); int res=0; rep(stamp+1) if(deg[i]==1) res++; print((res+1)/2); } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); #ifdef i_am_noob freopen("input1.txt","r",stdin); freopen("output1.txt","w",stdout); freopen("output2.txt","w",stderr); #endif cout << fixed << setprecision(15); int t; #ifdef wiwihorz cin >> t; #else t=1; #endif while(t--) orzck(); 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...
#Verdict Execution timeMemoryGrader output
Fetching results...