Submission #630834

# Submission time Handle Problem Language Result Execution time Memory
630834 2022-08-17T07:36:43 Z codr0 Mergers (JOI19_mergers) C++14
0 / 100
65 ms 20664 KB
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
 
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define F first
#define S second
#define err(x) cerr<<#x<<" : "<<x<<'\n';
#define dmid int mid=(r+l)/2
#define lc 2*id
#define rc 2*id+1
#define pb push_back
#define all(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)

using namespace std;
const int N=1e5+4;
const int lg=20;
vector<int> adj[N];
vector<int> vc[N];
int par[lg][N];
int h[N];
bool ok[N];
int dp[N],up[N];
int n,k;

	void dfs(int v,int p){
		for(int u:adj[v]) if(u!=p){
			dfs(u,v);
			dp[v]+=dp[u];
			maxm(up[v],up[u]-1);
		}
		ok[v]=(up[v]>0);
		if(dp[v]==0&&v!=p&&!ok[v]){
			dp[v]=1;
		}
	}

	void DFS(int v,int p){
		h[v]=h[p]+1;
		par[0][v]=p;
		for(int u:adj[v]) if(u!=p) DFS(u,v);
	}

	int PAR(int v,int kk){
		while(kk>0){
			v=par[__builtin_ctz(kk)][v];
			kk-=(kk&(-kk));
		}
		return v;
	}

	int LCA(int u,int v){
		if(h[u]>h[v]) swap(u,v);
		v=PAR(v,h[v]-h[u]);
		FORR(j,lg-1,0) if(par[j][u]!=par[j][v]){
			u=par[j][u];
			v=par[j][v];
		}
		return (par[0][v]);
	}

	int main(){
	iostream::sync_with_stdio(0); cin.tie(0);

	cin>>n>>k;
	FOR(_,1,n-1){
		int u,v; cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	if(n<=2){
		int w0,w1; cin>>w0;
		if(n==1){
			cout<<"0\n";
			return 0;
		}
		if(n==2){
			cin>>w1;
			cout<<(w0!=w1)<<'\n';
			return 0;
		}
		assert(1==2);
	}
	FOR(i,1,n){
		int w; cin>>w;
		vc[w].pb(i);
	}
	int root=1;
	FOR(i,1,n) if(SZ(adj[i])>1) root=i;
	DFS(root,root);	
	FOR(j,1,lg-1) FOR(i,1,n) par[j][i]=par[j-1][par[j-1][i]];
	FOR(i,1,k) if(!vc[i].empty()){
		int H=h[vc[i][0]];
		FOR(j,1,SZ(vc[i])-1) minm(H,h[LCA(vc[i][j],vc[i][j-1])]);
		for(int v:vc[i]) up[v]=h[v]-H;
	}

	dfs(root,root);
	FOR(i,1,n) if(i!=root&&!ok[i]&&dp[i]==dp[root]){
		dp[root]++;
		break;
	}
	cout<<(dp[root]/2+(dp[root]&1))<<'\n';

	return 0;
	}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Incorrect 3 ms 5076 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Incorrect 3 ms 5076 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Incorrect 3 ms 5076 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 65 ms 17740 KB Output is correct
2 Correct 59 ms 20664 KB Output is correct
3 Incorrect 5 ms 5516 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Incorrect 3 ms 5076 KB Output isn't correct
3 Halted 0 ms 0 KB -