Submission #630836

# Submission time Handle Problem Language Result Execution time Memory
630836 2022-08-17T07:53:42 Z codr0 Mergers (JOI19_mergers) C++14
10 / 100
96 ms 20712 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]);
		if(v==u) return v;
		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 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5460 KB Output is correct
27 Correct 4 ms 5460 KB Output is correct
28 Correct 4 ms 5588 KB Output is correct
29 Correct 4 ms 5524 KB Output is correct
30 Correct 4 ms 5460 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5716 KB Output is correct
33 Correct 2 ms 5076 KB Output is correct
34 Correct 4 ms 5460 KB Output is correct
35 Correct 4 ms 5460 KB Output is correct
36 Incorrect 4 ms 5460 KB Output isn't correct
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 2 ms 5088 KB Output is correct
26 Correct 51 ms 17720 KB Output is correct
27 Correct 77 ms 17748 KB Output is correct
28 Correct 4 ms 5460 KB Output is correct
29 Correct 2 ms 5076 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Incorrect 96 ms 17728 KB Output isn't correct
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 17792 KB Output is correct
2 Correct 55 ms 20712 KB Output is correct
3 Correct 4 ms 5460 KB Output is correct
4 Correct 5 ms 5548 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5460 KB Output is correct
8 Correct 92 ms 20628 KB Output is correct
9 Incorrect 4 ms 5460 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5460 KB Output is correct
27 Correct 4 ms 5460 KB Output is correct
28 Correct 4 ms 5588 KB Output is correct
29 Correct 4 ms 5524 KB Output is correct
30 Correct 4 ms 5460 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5716 KB Output is correct
33 Correct 2 ms 5076 KB Output is correct
34 Correct 4 ms 5460 KB Output is correct
35 Correct 4 ms 5460 KB Output is correct
36 Incorrect 4 ms 5460 KB Output isn't correct
37 Halted 0 ms 0 KB -