Submission #287506

# Submission time Handle Problem Language Result Execution time Memory
287506 2020-08-31T18:21:15 Z YJU Mergers (JOI19_mergers) C++14
0 / 100
130 ms 43444 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=2e5+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;
vector<ll> v[N],node[N],col[N];

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;
	REP(i,n-1){
		cin>>x>>y;
		v[x].pb(y);v[y].pb(x);
	}
	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);
		for(auto j:node[i]){
			x=j;
			while(vis[x]!=i&&x!=lca){
				col[x].pb(i);
				vis[x]=i;
				x=anc[x][0];
			}
		}
		col[lca].pb(i);
	}
	REP1(i,n){
		REP(j,SZ(col[i])-1){
			d[f(col[i][j])]=f(col[i][j+1]);
		}
	}
	memset(vis,0,sizeof(vis));
	REP1(i,n){
		if(SZ(v[i])==1&&vis[f(s[i])]==0){
			++ans;vis[f(s[i])]=1;
		}
	}
	cout<<(ans+1)/2<<"\n";
	return 0;
}

# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 16000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 16000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 16000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 130 ms 43444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 16000 KB Output isn't correct
2 Halted 0 ms 0 KB -