Submission #894768

# Submission time Handle Problem Language Result Execution time Memory
894768 2023-12-28T23:28:58 Z amirhoseinfar1385 Mergers (JOI19_mergers) C++17
100 / 100
843 ms 140288 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=500000+10;
vector<int>adj[maxn];
map<int,int>mp[maxn];
int cntc[maxn],sz[maxn],n,k,val[maxn],cnt[maxn],res;

bool cmp(int a,int b){
	return sz[a]>sz[b];
}

void pre(int u=1,int par=0){
	if(u!=1){
		sort(adj[u].begin(),adj[u].end());
		adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),par));
	}
	sz[u]=1;
	for(auto x:adj[u]){
		pre(x,u);
		sz[u]+=sz[x];
	}
	sort(adj[u].begin(),adj[u].end(),cmp);
}
void solve(int u){
	if((int)adj[u].size()==0){
		if(cntc[val[u]]!=1){
			mp[u][val[u]]=1;
		}
		else{
			res++;
			cnt[u]=1;
		}
		return ;
	}
	solve(adj[u][0]);
	cnt[u]=cnt[adj[u][0]];
	swap(mp[adj[u][0]],mp[u]);
	for(int i=1;i<(int)adj[u].size();i++){
		solve(adj[u][i]);
		cnt[u]+=cnt[adj[u][i]];
		for(auto x:mp[adj[u][i]]){
			mp[u][x.first]+=x.second;
			if(cntc[x.first]==mp[u][x.first]){
				mp[u].erase(x.first);
			}
		}
	}
	mp[u][val[u]]++;
	if(cntc[val[u]]==mp[u][val[u]]){
		mp[u].erase(val[u]);
	}
	if((int)mp[u].size()==0){
		if(u!=1){
			if(cnt[u]==0){
				res++;
			}
			cnt[u]=1;
		}
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k;
	for(int i=0;i<n-1;i++){
		int u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for(int i=1;i<=n;i++){
		cin>>val[i];
		cntc[val[i]]++;
	}
	pre(1);
	solve(1);
	if(cnt[1]==1){
		res++;
	}
	if(n==1){
		cout<<0<<"\n";
		return 0;
	}
	cout<<((res+1)/2)<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 43096 KB Output is correct
2 Correct 8 ms 43100 KB Output is correct
3 Correct 8 ms 43100 KB Output is correct
4 Correct 8 ms 43096 KB Output is correct
5 Correct 8 ms 43100 KB Output is correct
6 Correct 9 ms 43100 KB Output is correct
7 Correct 8 ms 43100 KB Output is correct
8 Correct 10 ms 43096 KB Output is correct
9 Correct 8 ms 43096 KB Output is correct
10 Correct 8 ms 43100 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 9 ms 43344 KB Output is correct
13 Correct 9 ms 43100 KB Output is correct
14 Correct 9 ms 43100 KB Output is correct
15 Correct 9 ms 43100 KB Output is correct
16 Correct 8 ms 43100 KB Output is correct
17 Correct 9 ms 43100 KB Output is correct
18 Correct 9 ms 43300 KB Output is correct
19 Correct 9 ms 43100 KB Output is correct
20 Correct 8 ms 43100 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 8 ms 43096 KB Output is correct
23 Correct 9 ms 43100 KB Output is correct
24 Correct 9 ms 43100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 43096 KB Output is correct
2 Correct 8 ms 43100 KB Output is correct
3 Correct 8 ms 43100 KB Output is correct
4 Correct 8 ms 43096 KB Output is correct
5 Correct 8 ms 43100 KB Output is correct
6 Correct 9 ms 43100 KB Output is correct
7 Correct 8 ms 43100 KB Output is correct
8 Correct 10 ms 43096 KB Output is correct
9 Correct 8 ms 43096 KB Output is correct
10 Correct 8 ms 43100 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 9 ms 43344 KB Output is correct
13 Correct 9 ms 43100 KB Output is correct
14 Correct 9 ms 43100 KB Output is correct
15 Correct 9 ms 43100 KB Output is correct
16 Correct 8 ms 43100 KB Output is correct
17 Correct 9 ms 43100 KB Output is correct
18 Correct 9 ms 43300 KB Output is correct
19 Correct 9 ms 43100 KB Output is correct
20 Correct 8 ms 43100 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 8 ms 43096 KB Output is correct
23 Correct 9 ms 43100 KB Output is correct
24 Correct 9 ms 43100 KB Output is correct
25 Correct 9 ms 43100 KB Output is correct
26 Correct 11 ms 43352 KB Output is correct
27 Correct 10 ms 43520 KB Output is correct
28 Correct 10 ms 43612 KB Output is correct
29 Correct 10 ms 43356 KB Output is correct
30 Correct 11 ms 43612 KB Output is correct
31 Correct 9 ms 43100 KB Output is correct
32 Correct 12 ms 43612 KB Output is correct
33 Correct 9 ms 43300 KB Output is correct
34 Correct 10 ms 43612 KB Output is correct
35 Correct 10 ms 43356 KB Output is correct
36 Correct 10 ms 43608 KB Output is correct
37 Correct 10 ms 43612 KB Output is correct
38 Correct 9 ms 43596 KB Output is correct
39 Correct 11 ms 43772 KB Output is correct
40 Correct 10 ms 43352 KB Output is correct
41 Correct 10 ms 43472 KB Output is correct
42 Correct 13 ms 43612 KB Output is correct
43 Correct 12 ms 43856 KB Output is correct
44 Correct 9 ms 43096 KB Output is correct
45 Correct 10 ms 43356 KB Output is correct
46 Correct 11 ms 43608 KB Output is correct
47 Correct 9 ms 43352 KB Output is correct
48 Correct 10 ms 43512 KB Output is correct
49 Correct 10 ms 43608 KB Output is correct
50 Correct 11 ms 43544 KB Output is correct
51 Correct 10 ms 43608 KB Output is correct
52 Correct 10 ms 43612 KB Output is correct
53 Correct 10 ms 43620 KB Output is correct
54 Correct 12 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 43096 KB Output is correct
2 Correct 8 ms 43100 KB Output is correct
3 Correct 8 ms 43100 KB Output is correct
4 Correct 8 ms 43096 KB Output is correct
5 Correct 8 ms 43100 KB Output is correct
6 Correct 9 ms 43100 KB Output is correct
7 Correct 8 ms 43100 KB Output is correct
8 Correct 10 ms 43096 KB Output is correct
9 Correct 8 ms 43096 KB Output is correct
10 Correct 8 ms 43100 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 9 ms 43344 KB Output is correct
13 Correct 9 ms 43100 KB Output is correct
14 Correct 9 ms 43100 KB Output is correct
15 Correct 9 ms 43100 KB Output is correct
16 Correct 8 ms 43100 KB Output is correct
17 Correct 9 ms 43100 KB Output is correct
18 Correct 9 ms 43300 KB Output is correct
19 Correct 9 ms 43100 KB Output is correct
20 Correct 8 ms 43100 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 8 ms 43096 KB Output is correct
23 Correct 9 ms 43100 KB Output is correct
24 Correct 9 ms 43100 KB Output is correct
25 Correct 9 ms 43324 KB Output is correct
26 Correct 56 ms 52916 KB Output is correct
27 Correct 75 ms 55892 KB Output is correct
28 Correct 11 ms 43356 KB Output is correct
29 Correct 9 ms 43100 KB Output is correct
30 Correct 9 ms 43096 KB Output is correct
31 Correct 52 ms 50448 KB Output is correct
32 Correct 10 ms 43608 KB Output is correct
33 Correct 66 ms 61016 KB Output is correct
34 Correct 69 ms 54864 KB Output is correct
35 Correct 10 ms 43340 KB Output is correct
36 Correct 50 ms 49412 KB Output is correct
37 Correct 10 ms 43356 KB Output is correct
38 Correct 10 ms 43352 KB Output is correct
39 Correct 57 ms 52924 KB Output is correct
40 Correct 10 ms 43868 KB Output is correct
41 Correct 55 ms 50416 KB Output is correct
42 Correct 72 ms 52368 KB Output is correct
43 Correct 8 ms 43100 KB Output is correct
44 Correct 71 ms 61588 KB Output is correct
45 Correct 65 ms 57140 KB Output is correct
46 Correct 10 ms 43612 KB Output is correct
47 Correct 10 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 51400 KB Output is correct
2 Correct 51 ms 46804 KB Output is correct
3 Correct 10 ms 43352 KB Output is correct
4 Correct 11 ms 43488 KB Output is correct
5 Correct 8 ms 43100 KB Output is correct
6 Correct 10 ms 43612 KB Output is correct
7 Correct 10 ms 43608 KB Output is correct
8 Correct 84 ms 53124 KB Output is correct
9 Correct 10 ms 43356 KB Output is correct
10 Correct 53 ms 50560 KB Output is correct
11 Correct 9 ms 43100 KB Output is correct
12 Correct 49 ms 48180 KB Output is correct
13 Correct 58 ms 50260 KB Output is correct
14 Correct 59 ms 48300 KB Output is correct
15 Correct 65 ms 52896 KB Output is correct
16 Correct 10 ms 43608 KB Output is correct
17 Correct 9 ms 43100 KB Output is correct
18 Correct 60 ms 49512 KB Output is correct
19 Correct 71 ms 55432 KB Output is correct
20 Correct 10 ms 43600 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 77 ms 52428 KB Output is correct
23 Correct 10 ms 43356 KB Output is correct
24 Correct 50 ms 48848 KB Output is correct
25 Correct 64 ms 53048 KB Output is correct
26 Correct 10 ms 43356 KB Output is correct
27 Correct 10 ms 43596 KB Output is correct
28 Correct 9 ms 43356 KB Output is correct
29 Correct 11 ms 43652 KB Output is correct
30 Correct 10 ms 43612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 43096 KB Output is correct
2 Correct 8 ms 43100 KB Output is correct
3 Correct 8 ms 43100 KB Output is correct
4 Correct 8 ms 43096 KB Output is correct
5 Correct 8 ms 43100 KB Output is correct
6 Correct 9 ms 43100 KB Output is correct
7 Correct 8 ms 43100 KB Output is correct
8 Correct 10 ms 43096 KB Output is correct
9 Correct 8 ms 43096 KB Output is correct
10 Correct 8 ms 43100 KB Output is correct
11 Correct 9 ms 43356 KB Output is correct
12 Correct 9 ms 43344 KB Output is correct
13 Correct 9 ms 43100 KB Output is correct
14 Correct 9 ms 43100 KB Output is correct
15 Correct 9 ms 43100 KB Output is correct
16 Correct 8 ms 43100 KB Output is correct
17 Correct 9 ms 43100 KB Output is correct
18 Correct 9 ms 43300 KB Output is correct
19 Correct 9 ms 43100 KB Output is correct
20 Correct 8 ms 43100 KB Output is correct
21 Correct 9 ms 43100 KB Output is correct
22 Correct 8 ms 43096 KB Output is correct
23 Correct 9 ms 43100 KB Output is correct
24 Correct 9 ms 43100 KB Output is correct
25 Correct 9 ms 43100 KB Output is correct
26 Correct 11 ms 43352 KB Output is correct
27 Correct 10 ms 43520 KB Output is correct
28 Correct 10 ms 43612 KB Output is correct
29 Correct 10 ms 43356 KB Output is correct
30 Correct 11 ms 43612 KB Output is correct
31 Correct 9 ms 43100 KB Output is correct
32 Correct 12 ms 43612 KB Output is correct
33 Correct 9 ms 43300 KB Output is correct
34 Correct 10 ms 43612 KB Output is correct
35 Correct 10 ms 43356 KB Output is correct
36 Correct 10 ms 43608 KB Output is correct
37 Correct 10 ms 43612 KB Output is correct
38 Correct 9 ms 43596 KB Output is correct
39 Correct 11 ms 43772 KB Output is correct
40 Correct 10 ms 43352 KB Output is correct
41 Correct 10 ms 43472 KB Output is correct
42 Correct 13 ms 43612 KB Output is correct
43 Correct 12 ms 43856 KB Output is correct
44 Correct 9 ms 43096 KB Output is correct
45 Correct 10 ms 43356 KB Output is correct
46 Correct 11 ms 43608 KB Output is correct
47 Correct 9 ms 43352 KB Output is correct
48 Correct 10 ms 43512 KB Output is correct
49 Correct 10 ms 43608 KB Output is correct
50 Correct 11 ms 43544 KB Output is correct
51 Correct 10 ms 43608 KB Output is correct
52 Correct 10 ms 43612 KB Output is correct
53 Correct 10 ms 43620 KB Output is correct
54 Correct 12 ms 43612 KB Output is correct
55 Correct 9 ms 43324 KB Output is correct
56 Correct 56 ms 52916 KB Output is correct
57 Correct 75 ms 55892 KB Output is correct
58 Correct 11 ms 43356 KB Output is correct
59 Correct 9 ms 43100 KB Output is correct
60 Correct 9 ms 43096 KB Output is correct
61 Correct 52 ms 50448 KB Output is correct
62 Correct 10 ms 43608 KB Output is correct
63 Correct 66 ms 61016 KB Output is correct
64 Correct 69 ms 54864 KB Output is correct
65 Correct 10 ms 43340 KB Output is correct
66 Correct 50 ms 49412 KB Output is correct
67 Correct 10 ms 43356 KB Output is correct
68 Correct 10 ms 43352 KB Output is correct
69 Correct 57 ms 52924 KB Output is correct
70 Correct 10 ms 43868 KB Output is correct
71 Correct 55 ms 50416 KB Output is correct
72 Correct 72 ms 52368 KB Output is correct
73 Correct 8 ms 43100 KB Output is correct
74 Correct 71 ms 61588 KB Output is correct
75 Correct 65 ms 57140 KB Output is correct
76 Correct 10 ms 43612 KB Output is correct
77 Correct 10 ms 43612 KB Output is correct
78 Correct 55 ms 51400 KB Output is correct
79 Correct 51 ms 46804 KB Output is correct
80 Correct 10 ms 43352 KB Output is correct
81 Correct 11 ms 43488 KB Output is correct
82 Correct 8 ms 43100 KB Output is correct
83 Correct 10 ms 43612 KB Output is correct
84 Correct 10 ms 43608 KB Output is correct
85 Correct 84 ms 53124 KB Output is correct
86 Correct 10 ms 43356 KB Output is correct
87 Correct 53 ms 50560 KB Output is correct
88 Correct 9 ms 43100 KB Output is correct
89 Correct 49 ms 48180 KB Output is correct
90 Correct 58 ms 50260 KB Output is correct
91 Correct 59 ms 48300 KB Output is correct
92 Correct 65 ms 52896 KB Output is correct
93 Correct 10 ms 43608 KB Output is correct
94 Correct 9 ms 43100 KB Output is correct
95 Correct 60 ms 49512 KB Output is correct
96 Correct 71 ms 55432 KB Output is correct
97 Correct 10 ms 43600 KB Output is correct
98 Correct 9 ms 43100 KB Output is correct
99 Correct 77 ms 52428 KB Output is correct
100 Correct 10 ms 43356 KB Output is correct
101 Correct 50 ms 48848 KB Output is correct
102 Correct 64 ms 53048 KB Output is correct
103 Correct 10 ms 43356 KB Output is correct
104 Correct 10 ms 43596 KB Output is correct
105 Correct 9 ms 43356 KB Output is correct
106 Correct 11 ms 43652 KB Output is correct
107 Correct 10 ms 43612 KB Output is correct
108 Correct 497 ms 92308 KB Output is correct
109 Correct 577 ms 99996 KB Output is correct
110 Correct 608 ms 120360 KB Output is correct
111 Correct 614 ms 111580 KB Output is correct
112 Correct 549 ms 93780 KB Output is correct
113 Correct 316 ms 75836 KB Output is correct
114 Correct 440 ms 78928 KB Output is correct
115 Correct 449 ms 79016 KB Output is correct
116 Correct 742 ms 129876 KB Output is correct
117 Correct 401 ms 69456 KB Output is correct
118 Correct 460 ms 79112 KB Output is correct
119 Correct 434 ms 69384 KB Output is correct
120 Correct 520 ms 95168 KB Output is correct
121 Correct 435 ms 69616 KB Output is correct
122 Correct 843 ms 140288 KB Output is correct
123 Correct 270 ms 71036 KB Output is correct
124 Correct 569 ms 69368 KB Output is correct