Submission #630838

# Submission time Handle Problem Language Result Execution time Memory
630838 2022-08-17T07:55:32 Z codr0 Mergers (JOI19_mergers) C++14
70 / 100
97 ms 24448 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;
		}
	}
	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 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 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 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 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 2 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 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 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 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 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 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 2 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 4 ms 5512 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 5588 KB Output is correct
30 Correct 4 ms 5512 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5716 KB Output is correct
33 Correct 4 ms 5088 KB Output is correct
34 Correct 3 ms 5460 KB Output is correct
35 Correct 6 ms 5588 KB Output is correct
36 Correct 4 ms 5500 KB Output is correct
37 Correct 5 ms 5460 KB Output is correct
38 Correct 2 ms 5076 KB Output is correct
39 Correct 4 ms 5460 KB Output is correct
40 Correct 4 ms 5460 KB Output is correct
41 Correct 4 ms 5460 KB Output is correct
42 Correct 4 ms 5460 KB Output is correct
43 Correct 4 ms 5588 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5588 KB Output is correct
46 Correct 4 ms 5460 KB Output is correct
47 Correct 2 ms 5020 KB Output is correct
48 Correct 4 ms 5460 KB Output is correct
49 Correct 4 ms 5588 KB Output is correct
50 Correct 4 ms 5588 KB Output is correct
51 Correct 4 ms 5460 KB Output is correct
52 Correct 4 ms 5460 KB Output is correct
53 Correct 4 ms 5460 KB Output is correct
54 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 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 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 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 2 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 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 51 ms 17724 KB Output is correct
27 Correct 77 ms 17872 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 Correct 68 ms 17732 KB Output is correct
32 Correct 4 ms 5460 KB Output is correct
33 Correct 97 ms 22448 KB Output is correct
34 Correct 70 ms 17808 KB Output is correct
35 Correct 4 ms 5460 KB Output is correct
36 Correct 74 ms 18512 KB Output is correct
37 Correct 4 ms 5460 KB Output is correct
38 Correct 4 ms 5460 KB Output is correct
39 Correct 48 ms 17804 KB Output is correct
40 Correct 4 ms 5588 KB Output is correct
41 Correct 71 ms 17816 KB Output is correct
42 Correct 92 ms 20812 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 93 ms 21828 KB Output is correct
45 Correct 88 ms 21664 KB Output is correct
46 Correct 4 ms 5460 KB Output is correct
47 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 17864 KB Output is correct
2 Correct 52 ms 20752 KB Output is correct
3 Correct 4 ms 5460 KB Output is correct
4 Correct 4 ms 5460 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5460 KB Output is correct
8 Correct 84 ms 18892 KB Output is correct
9 Correct 4 ms 5460 KB Output is correct
10 Correct 70 ms 17820 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 77 ms 17792 KB Output is correct
13 Correct 75 ms 18764 KB Output is correct
14 Correct 72 ms 20456 KB Output is correct
15 Correct 49 ms 17688 KB Output is correct
16 Correct 4 ms 5460 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 60 ms 20420 KB Output is correct
19 Correct 79 ms 24448 KB Output is correct
20 Correct 4 ms 5460 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 60 ms 19116 KB Output is correct
23 Correct 4 ms 5460 KB Output is correct
24 Correct 72 ms 18176 KB Output is correct
25 Correct 82 ms 23476 KB Output is correct
26 Correct 4 ms 5588 KB Output is correct
27 Correct 4 ms 5680 KB Output is correct
28 Correct 4 ms 5460 KB Output is correct
29 Correct 4 ms 5508 KB Output is correct
30 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 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 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 2 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 2 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 4 ms 5512 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 5588 KB Output is correct
30 Correct 4 ms 5512 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 4 ms 5716 KB Output is correct
33 Correct 4 ms 5088 KB Output is correct
34 Correct 3 ms 5460 KB Output is correct
35 Correct 6 ms 5588 KB Output is correct
36 Correct 4 ms 5500 KB Output is correct
37 Correct 5 ms 5460 KB Output is correct
38 Correct 2 ms 5076 KB Output is correct
39 Correct 4 ms 5460 KB Output is correct
40 Correct 4 ms 5460 KB Output is correct
41 Correct 4 ms 5460 KB Output is correct
42 Correct 4 ms 5460 KB Output is correct
43 Correct 4 ms 5588 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5588 KB Output is correct
46 Correct 4 ms 5460 KB Output is correct
47 Correct 2 ms 5020 KB Output is correct
48 Correct 4 ms 5460 KB Output is correct
49 Correct 4 ms 5588 KB Output is correct
50 Correct 4 ms 5588 KB Output is correct
51 Correct 4 ms 5460 KB Output is correct
52 Correct 4 ms 5460 KB Output is correct
53 Correct 4 ms 5460 KB Output is correct
54 Correct 4 ms 5460 KB Output is correct
55 Correct 3 ms 5076 KB Output is correct
56 Correct 51 ms 17724 KB Output is correct
57 Correct 77 ms 17872 KB Output is correct
58 Correct 4 ms 5460 KB Output is correct
59 Correct 2 ms 5076 KB Output is correct
60 Correct 3 ms 5076 KB Output is correct
61 Correct 68 ms 17732 KB Output is correct
62 Correct 4 ms 5460 KB Output is correct
63 Correct 97 ms 22448 KB Output is correct
64 Correct 70 ms 17808 KB Output is correct
65 Correct 4 ms 5460 KB Output is correct
66 Correct 74 ms 18512 KB Output is correct
67 Correct 4 ms 5460 KB Output is correct
68 Correct 4 ms 5460 KB Output is correct
69 Correct 48 ms 17804 KB Output is correct
70 Correct 4 ms 5588 KB Output is correct
71 Correct 71 ms 17816 KB Output is correct
72 Correct 92 ms 20812 KB Output is correct
73 Correct 3 ms 5076 KB Output is correct
74 Correct 93 ms 21828 KB Output is correct
75 Correct 88 ms 21664 KB Output is correct
76 Correct 4 ms 5460 KB Output is correct
77 Correct 4 ms 5460 KB Output is correct
78 Correct 52 ms 17864 KB Output is correct
79 Correct 52 ms 20752 KB Output is correct
80 Correct 4 ms 5460 KB Output is correct
81 Correct 4 ms 5460 KB Output is correct
82 Correct 2 ms 5076 KB Output is correct
83 Correct 3 ms 5076 KB Output is correct
84 Correct 3 ms 5460 KB Output is correct
85 Correct 84 ms 18892 KB Output is correct
86 Correct 4 ms 5460 KB Output is correct
87 Correct 70 ms 17820 KB Output is correct
88 Correct 3 ms 5076 KB Output is correct
89 Correct 77 ms 17792 KB Output is correct
90 Correct 75 ms 18764 KB Output is correct
91 Correct 72 ms 20456 KB Output is correct
92 Correct 49 ms 17688 KB Output is correct
93 Correct 4 ms 5460 KB Output is correct
94 Correct 2 ms 5076 KB Output is correct
95 Correct 60 ms 20420 KB Output is correct
96 Correct 79 ms 24448 KB Output is correct
97 Correct 4 ms 5460 KB Output is correct
98 Correct 3 ms 5076 KB Output is correct
99 Correct 60 ms 19116 KB Output is correct
100 Correct 4 ms 5460 KB Output is correct
101 Correct 72 ms 18176 KB Output is correct
102 Correct 82 ms 23476 KB Output is correct
103 Correct 4 ms 5588 KB Output is correct
104 Correct 4 ms 5680 KB Output is correct
105 Correct 4 ms 5460 KB Output is correct
106 Correct 4 ms 5508 KB Output is correct
107 Correct 4 ms 5460 KB Output is correct
108 Runtime error 7 ms 9940 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -