#include <bits/stdc++.h>
#define int long long int
using namespace std;
const int maxn = 1e5 + 100;
int n ,k, a[maxn], num[maxn], sz[maxn], cnt[maxn] ,ans;
bool mark[maxn];
vector<int> adj[maxn];
vector<int> states[maxn];
void dfs1(int v, int mpar = 0){
sz[v] = 1;
states[v][a[v]] = true;
for(auto u : adj[v]){
if(u != mpar){
dfs1(u, v);
sz[v] += sz[u];
cnt[v] += cnt[u];
for(int i = 1; i <= k; i++){
states[v][i] |= states[u][i];
}
}
}
int tedad = 0;
for(int i = 1; i <= k ;i ++){
if(states[v][i]){
tedad += num[i];
}
}
if((v != 1) and (tedad == sz[v])){
mark[v] = true;
cnt[v]++;
}
}
void dfs2(int v ,int mpar = 0){
if(mark[v]){
if(cnt[v] == 1 or cnt[v] == cnt[1]){
ans++;
}
}
for(auto u : adj[v]){
if(u != mpar){
dfs2(u ,v);
}
}
}
int32_t main(){
ios::sync_with_stdio(0) ,cout.tie(0) ,cin.tie(0);
cin >> n >> k;
for(int i = 0 ;i < n - 1 ;i++){
int x ,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 1; i <= n; i++){
cin >> a[i];
num[a[i]]++;
}
for(int i = 1 ;i <= n; i++){
states[i] = vector<int> (k + 100, 0);
}
dfs1(1), dfs2(1);
cout << (ans + 1) / 2 << endl;
system("pause");
return 0;
}
Compilation message
mergers.cpp: In function 'int32_t main()':
mergers.cpp:78:11: warning: ignoring return value of 'int system(const char*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | system("pause");
| ~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
3 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
2 ms |
6736 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
2 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6904 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6736 KB |
Output is correct |
21 |
Correct |
2 ms |
6736 KB |
Output is correct |
22 |
Correct |
2 ms |
6904 KB |
Output is correct |
23 |
Correct |
2 ms |
6904 KB |
Output is correct |
24 |
Correct |
2 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
3 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
2 ms |
6736 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
2 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6904 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6736 KB |
Output is correct |
21 |
Correct |
2 ms |
6736 KB |
Output is correct |
22 |
Correct |
2 ms |
6904 KB |
Output is correct |
23 |
Correct |
2 ms |
6904 KB |
Output is correct |
24 |
Correct |
2 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
6904 KB |
Output is correct |
26 |
Correct |
61 ms |
56144 KB |
Output is correct |
27 |
Correct |
10 ms |
10388 KB |
Output is correct |
28 |
Correct |
95 ms |
79760 KB |
Output is correct |
29 |
Correct |
87 ms |
79616 KB |
Output is correct |
30 |
Correct |
17 ms |
13904 KB |
Output is correct |
31 |
Correct |
2 ms |
6904 KB |
Output is correct |
32 |
Correct |
89 ms |
79900 KB |
Output is correct |
33 |
Correct |
2 ms |
6736 KB |
Output is correct |
34 |
Correct |
13 ms |
10392 KB |
Output is correct |
35 |
Correct |
82 ms |
79680 KB |
Output is correct |
36 |
Correct |
10 ms |
10320 KB |
Output is correct |
37 |
Correct |
50 ms |
44588 KB |
Output is correct |
38 |
Correct |
2 ms |
6904 KB |
Output is correct |
39 |
Correct |
56 ms |
44368 KB |
Output is correct |
40 |
Correct |
4 ms |
9296 KB |
Output is correct |
41 |
Correct |
4 ms |
9552 KB |
Output is correct |
42 |
Correct |
52 ms |
44368 KB |
Output is correct |
43 |
Correct |
4 ms |
9552 KB |
Output is correct |
44 |
Correct |
3 ms |
6992 KB |
Output is correct |
45 |
Correct |
88 ms |
77384 KB |
Output is correct |
46 |
Correct |
70 ms |
43740 KB |
Output is correct |
47 |
Correct |
2 ms |
6736 KB |
Output is correct |
48 |
Correct |
42 ms |
23432 KB |
Output is correct |
49 |
Correct |
87 ms |
79720 KB |
Output is correct |
50 |
Correct |
85 ms |
79944 KB |
Output is correct |
51 |
Correct |
33 ms |
23376 KB |
Output is correct |
52 |
Correct |
10 ms |
10320 KB |
Output is correct |
53 |
Correct |
51 ms |
44448 KB |
Output is correct |
54 |
Correct |
11 ms |
10320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
3 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
2 ms |
6736 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
2 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6904 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6736 KB |
Output is correct |
21 |
Correct |
2 ms |
6736 KB |
Output is correct |
22 |
Correct |
2 ms |
6904 KB |
Output is correct |
23 |
Correct |
2 ms |
6904 KB |
Output is correct |
24 |
Correct |
2 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
6736 KB |
Output is correct |
26 |
Correct |
157 ms |
130496 KB |
Output is correct |
27 |
Correct |
367 ms |
130128 KB |
Output is correct |
28 |
Correct |
11 ms |
10576 KB |
Output is correct |
29 |
Correct |
2 ms |
6736 KB |
Output is correct |
30 |
Correct |
2 ms |
6832 KB |
Output is correct |
31 |
Correct |
362 ms |
130296 KB |
Output is correct |
32 |
Correct |
10 ms |
10320 KB |
Output is correct |
33 |
Correct |
414 ms |
136776 KB |
Output is correct |
34 |
Correct |
342 ms |
131016 KB |
Output is correct |
35 |
Correct |
10 ms |
10488 KB |
Output is correct |
36 |
Correct |
350 ms |
130376 KB |
Output is correct |
37 |
Correct |
5 ms |
9296 KB |
Output is correct |
38 |
Correct |
5 ms |
9552 KB |
Output is correct |
39 |
Correct |
145 ms |
130360 KB |
Output is correct |
40 |
Correct |
5 ms |
9552 KB |
Output is correct |
41 |
Correct |
104 ms |
91208 KB |
Output is correct |
42 |
Correct |
393 ms |
133084 KB |
Output is correct |
43 |
Correct |
2 ms |
6736 KB |
Output is correct |
44 |
Correct |
109 ms |
98012 KB |
Output is correct |
45 |
Correct |
398 ms |
134628 KB |
Output is correct |
46 |
Correct |
12 ms |
10320 KB |
Output is correct |
47 |
Correct |
11 ms |
10320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
130488 KB |
Output is correct |
2 |
Runtime error |
209 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
3 ms |
6736 KB |
Output is correct |
4 |
Correct |
2 ms |
6736 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
2 ms |
6736 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
2 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6904 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
2 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6736 KB |
Output is correct |
21 |
Correct |
2 ms |
6736 KB |
Output is correct |
22 |
Correct |
2 ms |
6904 KB |
Output is correct |
23 |
Correct |
2 ms |
6904 KB |
Output is correct |
24 |
Correct |
2 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
6904 KB |
Output is correct |
26 |
Correct |
61 ms |
56144 KB |
Output is correct |
27 |
Correct |
10 ms |
10388 KB |
Output is correct |
28 |
Correct |
95 ms |
79760 KB |
Output is correct |
29 |
Correct |
87 ms |
79616 KB |
Output is correct |
30 |
Correct |
17 ms |
13904 KB |
Output is correct |
31 |
Correct |
2 ms |
6904 KB |
Output is correct |
32 |
Correct |
89 ms |
79900 KB |
Output is correct |
33 |
Correct |
2 ms |
6736 KB |
Output is correct |
34 |
Correct |
13 ms |
10392 KB |
Output is correct |
35 |
Correct |
82 ms |
79680 KB |
Output is correct |
36 |
Correct |
10 ms |
10320 KB |
Output is correct |
37 |
Correct |
50 ms |
44588 KB |
Output is correct |
38 |
Correct |
2 ms |
6904 KB |
Output is correct |
39 |
Correct |
56 ms |
44368 KB |
Output is correct |
40 |
Correct |
4 ms |
9296 KB |
Output is correct |
41 |
Correct |
4 ms |
9552 KB |
Output is correct |
42 |
Correct |
52 ms |
44368 KB |
Output is correct |
43 |
Correct |
4 ms |
9552 KB |
Output is correct |
44 |
Correct |
3 ms |
6992 KB |
Output is correct |
45 |
Correct |
88 ms |
77384 KB |
Output is correct |
46 |
Correct |
70 ms |
43740 KB |
Output is correct |
47 |
Correct |
2 ms |
6736 KB |
Output is correct |
48 |
Correct |
42 ms |
23432 KB |
Output is correct |
49 |
Correct |
87 ms |
79720 KB |
Output is correct |
50 |
Correct |
85 ms |
79944 KB |
Output is correct |
51 |
Correct |
33 ms |
23376 KB |
Output is correct |
52 |
Correct |
10 ms |
10320 KB |
Output is correct |
53 |
Correct |
51 ms |
44448 KB |
Output is correct |
54 |
Correct |
11 ms |
10320 KB |
Output is correct |
55 |
Correct |
2 ms |
6736 KB |
Output is correct |
56 |
Correct |
157 ms |
130496 KB |
Output is correct |
57 |
Correct |
367 ms |
130128 KB |
Output is correct |
58 |
Correct |
11 ms |
10576 KB |
Output is correct |
59 |
Correct |
2 ms |
6736 KB |
Output is correct |
60 |
Correct |
2 ms |
6832 KB |
Output is correct |
61 |
Correct |
362 ms |
130296 KB |
Output is correct |
62 |
Correct |
10 ms |
10320 KB |
Output is correct |
63 |
Correct |
414 ms |
136776 KB |
Output is correct |
64 |
Correct |
342 ms |
131016 KB |
Output is correct |
65 |
Correct |
10 ms |
10488 KB |
Output is correct |
66 |
Correct |
350 ms |
130376 KB |
Output is correct |
67 |
Correct |
5 ms |
9296 KB |
Output is correct |
68 |
Correct |
5 ms |
9552 KB |
Output is correct |
69 |
Correct |
145 ms |
130360 KB |
Output is correct |
70 |
Correct |
5 ms |
9552 KB |
Output is correct |
71 |
Correct |
104 ms |
91208 KB |
Output is correct |
72 |
Correct |
393 ms |
133084 KB |
Output is correct |
73 |
Correct |
2 ms |
6736 KB |
Output is correct |
74 |
Correct |
109 ms |
98012 KB |
Output is correct |
75 |
Correct |
398 ms |
134628 KB |
Output is correct |
76 |
Correct |
12 ms |
10320 KB |
Output is correct |
77 |
Correct |
11 ms |
10320 KB |
Output is correct |
78 |
Correct |
142 ms |
130488 KB |
Output is correct |
79 |
Runtime error |
209 ms |
262144 KB |
Execution killed with signal 9 |
80 |
Halted |
0 ms |
0 KB |
- |