답안 #287578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
287578 2020-08-31T20:16:00 Z YJU Mergers (JOI19_mergers) C++14
0 / 100
159 ms 63828 KB
#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<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=5e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
 
ll dir[N],d[N],anc[N][21],x,y,n,k,s[N],vis[N],ans,dg[N];
vector<ll> v[N],node[N],col[N];
vector<pll> ed;
 
ll f(ll id){return (dir[id]==id?id:dir[id]=f(dir[id]));}
 
void DFS(ll nd,ll pa){
	anc[nd][0]=pa;
	REP1(i,20)anc[nd][i]=anc[anc[nd][i-1]][i-1];
	for(auto i:v[nd]){
		if(i==pa)continue;
		d[i]=d[nd]+1;
		DFS(i,nd);
	}
}
 
ll LCA(ll nda,ll ndb){
	if(d[nda]<d[ndb])swap(nda,ndb);
	for(int i=20;i>=0;i--)if(d[anc[nda][i]]>=d[ndb])nda=anc[nda][i];
	if(nda==ndb)return nda;
	for(int i=20;i>=0;i--){
		if(anc[nda][i]!=anc[ndb][i])nda=anc[nda][i],ndb=anc[ndb][i];
	}
	return anc[nda][0];
}
 
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n>>k;
	REP1(i,n)dir[i]=i;
	REP(i,n-1){
		cin>>x>>y;
		v[x].pb(y);v[y].pb(x);
		ed.pb(mp(x,y));
	}
	DFS(1,0);
	REP1(i,n)cin>>s[i],node[s[i]].pb(i);
	REP1(i,k){
		if(!SZ(node[i]))continue;
		ll lca=node[i][0];
		for(auto j:node[i])lca=LCA(j,lca);
		lca=f(lca);
		for(auto j:node[i]){
			x=f(j);
			while(d[x]>d[lca]){
				dir[x]=f(anc[x][0]);
				x=f(anc[x][0]);
			}
		}
	}
	for(auto i:ed){
		if(f(s[i.X])!=f(s[i.Y])){
			++dg[f(s[i.X])];++dg[f(s[i.Y])];
		}
	}
	REP1(i,n){
		if(dg[i]==1)++ans;
	}
	cout<<(ans==1?0:(ans+1)/2)<<"\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 35548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 35548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 35548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 60948 KB Output is correct
2 Correct 159 ms 63828 KB Output is correct
3 Incorrect 27 ms 36472 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 35548 KB Output isn't correct
2 Halted 0 ms 0 KB -