Submission #140830

# Submission time Handle Problem Language Result Execution time Memory
140830 2019-08-05T13:37:24 Z Utaha Mergers (JOI19_mergers) C++14
0 / 100
130 ms 31612 KB
/*input
5 4
1 2
2 3
3 4
4 5
1 2 3 2 4
*/
#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];

int cnt=-1;
map<int,int> mp[maxn];
vector<int> num;
int dfs(int u,int par){
	int mx=-1;
	int ret=0;
	for(int v:edge[u]) if(v!=par){
		ret+=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){
		cnt++;
		num.pb(ret);
		// cout<<u<<' '<<ret<<'\n';
		ret++;
	}
	return ret;
}

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);
	int ans=0;
	for(int i:num) ans+=(i==tmp-1)+(i==0);
	if(ans) ans--;
	cout<<ans<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 21496 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 21496 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 21496 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 130 ms 31612 KB Output is correct
2 Incorrect 107 ms 28112 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 21496 KB Output isn't correct
2 Halted 0 ms 0 KB -