/*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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
21624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
21624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
21624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
31592 KB |
Output is correct |
2 |
Incorrect |
97 ms |
27584 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
21624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |