제출 #140820

#제출 시각아이디문제언어결과실행 시간메모리
140820UtahaMergers (JOI19_mergers)C++14
0 / 100
108 ms31592 KiB
/*input 2 2 1 2 1 2 */ #include <bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pdd; #define IOS ios_base::sync_with_stdio(0); cin.tie(0) #define ALL(a) a.begin(),a.end() #define SZ(a) ((int)a.size()) #define F first #define S second #define REP(i,n) for(int i=0;i<((int)n);i++) #define pb emplace_back #define MP(a,b) make_pair(a,b) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define EL cout<<'\n' template<typename T1,typename T2> ostream& operator<<(ostream& out,pair<T1,T2> P){ out<<'('<<P.F<<','<<P.S<<')'; return out; } template<typename T> ostream& operator<<(ostream& out,vector<T> V){ REP(i,SZ(V)) out<<V[i]<<((i!=SZ(V)-1)?" ":""); return out; } #define version 20190726 //}}} const ll maxn=300005; const ll maxlg=20; const ll INF64=1e18; const int INF=0x3f3f3f3f; const ll MOD=ll(1e9+7); const ld PI=acos(-1); const ld eps=1e-9; //const ll p=880301; //const ll P=31; ll mypow(ll a,ll b){ ll res=1LL; while(b){ if(b&1) res=res*a%MOD; a=a*a%MOD; b>>=1; } return res; } int n,k; vector<int> edge[maxn]; int c[maxn]; int tt[maxn]; bool tg[maxn]; map<int,int> mp[maxn]; int mxmeow=0; int ans=0; int dfs(int u,int par){ int mx=-1; int meow=0; for(int v:edge[u]) if(v!=par){ meow+=dfs(v,u); if(mx==-1||SZ(mp[mx])<SZ(mp[v])) mx=v; } for(int v:edge[u]) if(v!=par&&v!=mx){ for(pii j:mp[v]){ mp[mx][j.F]+=j.S; if(mp[mx][j.F]==tt[j.F]){ mp[mx].erase(j.F); } } } if(mx!=-1) mp[mx].swap(mp[u]); mp[u][c[u]]++; if(mp[u][c[u]]==tt[c[u]]) mp[u].erase(c[u]); if(u&&SZ(mp[u])==0){ tg[u]=1; if(meow==0) ans++; meow++; mxmeow=max(mxmeow,meow); } return meow; } int main(){ IOS; cin>>n>>k; REP(i,n-1){ int u,v; cin>>u>>v; u--;v--; edge[u].pb(v); edge[v].pb(u); } REP(i,n) cin>>c[i],c[i]--,tt[c[i]]++; int tmp=dfs(0,-1); if(tmp==mxmeow) ans++; cout<<ans-1<<'\n'; 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...