//khodaya khodet komak kon
# pragma GCC target ("avx2")
# pragma GCC optimization ("Ofast")
# pragma GCC optimization ("unroll-loops")
# include <bits/stdc++.h>
/*
// ordered_set
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define lc id << 1
# define rc id << 1 | 1
# define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 5e5 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int mod = 1e9 + 7;
const int base = 257;
int n, k, a[xn], ans, sz[xn], arshad[xn];
int cnt[xn], c[xn], sum, ted;
vector <int> adj[xn];
bool mark[xn];
void preDFS(int v, int p = - 1){
sz[v] = 1;
for (int u : adj[v]){
if (u == p)
continue;
preDFS(u, v);
sz[v] += sz[u];
if (sz[u] > sz[arshad[v]])
arshad[v] = u;
}
}
void upd(int v, int f, int p){
if (c[a[v]] == cnt[a[v]])
sum -= cnt[a[v]];
c[a[v]] += f;
if (c[a[v]] == cnt[a[v]])
sum += cnt[a[v]];
for (int u : adj[v])
if (u != p)
upd(u, f, v);
}
void DFS(int v, int p = - 1){
for (int u : adj[v])
if (u != p && u != arshad[v])
DFS(u, v), upd(u, - 1, v);
if (arshad[v])
DFS(arshad[v], v);
for (int u : adj[v])
if (u != p && u != arshad[v])
upd(u, 1, v);
++ c[a[v]];
if (c[a[v]] == cnt[a[v]])
sum += c[a[v]];
if (sum == sz[v] && v != 1)
mark[v] = true, ++ ted;
}
void DFS2(int v, int p = - 1){
sz[v] = mark[v];
for (int u : adj[v])
if (u != p)
DFS2(u, v), sz[v] += sz[u];
ans += (mark[v] && (sz[v] == 1 || sz[v] == ted));
}
int main(){
InTheNameOfGod;
cin >> n >> k;
for (int i = 0; i < n - 1; ++ i){
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 1; i <= n; ++ i)
cin >> a[i], ++ cnt[a[i]];
preDFS(1), DFS(1), DFS2(1);
cout << (ans + 1) / 2 << endl;
return 0;
}
Compilation message
mergers.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | # pragma GCC optimization ("Ofast")
|
mergers.cpp:4: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
4 | # pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
11 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
10 ms |
12140 KB |
Output is correct |
12 |
Correct |
10 ms |
12140 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12140 KB |
Output is correct |
16 |
Correct |
9 ms |
12140 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
9 ms |
12140 KB |
Output is correct |
19 |
Correct |
9 ms |
12140 KB |
Output is correct |
20 |
Correct |
9 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
9 ms |
12140 KB |
Output is correct |
23 |
Correct |
9 ms |
12140 KB |
Output is correct |
24 |
Correct |
10 ms |
12140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
11 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
10 ms |
12140 KB |
Output is correct |
12 |
Correct |
10 ms |
12140 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12140 KB |
Output is correct |
16 |
Correct |
9 ms |
12140 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
9 ms |
12140 KB |
Output is correct |
19 |
Correct |
9 ms |
12140 KB |
Output is correct |
20 |
Correct |
9 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
9 ms |
12140 KB |
Output is correct |
23 |
Correct |
9 ms |
12140 KB |
Output is correct |
24 |
Correct |
10 ms |
12140 KB |
Output is correct |
25 |
Correct |
9 ms |
12140 KB |
Output is correct |
26 |
Correct |
11 ms |
12268 KB |
Output is correct |
27 |
Correct |
11 ms |
12416 KB |
Output is correct |
28 |
Correct |
11 ms |
12416 KB |
Output is correct |
29 |
Correct |
11 ms |
12268 KB |
Output is correct |
30 |
Correct |
11 ms |
12268 KB |
Output is correct |
31 |
Correct |
10 ms |
12140 KB |
Output is correct |
32 |
Correct |
11 ms |
12524 KB |
Output is correct |
33 |
Correct |
9 ms |
12140 KB |
Output is correct |
34 |
Correct |
10 ms |
12268 KB |
Output is correct |
35 |
Correct |
13 ms |
12268 KB |
Output is correct |
36 |
Correct |
11 ms |
12396 KB |
Output is correct |
37 |
Correct |
11 ms |
12268 KB |
Output is correct |
38 |
Correct |
10 ms |
12140 KB |
Output is correct |
39 |
Correct |
11 ms |
12268 KB |
Output is correct |
40 |
Correct |
11 ms |
12268 KB |
Output is correct |
41 |
Correct |
11 ms |
12396 KB |
Output is correct |
42 |
Correct |
10 ms |
12268 KB |
Output is correct |
43 |
Correct |
10 ms |
12524 KB |
Output is correct |
44 |
Correct |
9 ms |
12140 KB |
Output is correct |
45 |
Correct |
11 ms |
12396 KB |
Output is correct |
46 |
Correct |
11 ms |
12396 KB |
Output is correct |
47 |
Correct |
9 ms |
12140 KB |
Output is correct |
48 |
Correct |
11 ms |
12268 KB |
Output is correct |
49 |
Correct |
10 ms |
12268 KB |
Output is correct |
50 |
Correct |
11 ms |
12396 KB |
Output is correct |
51 |
Correct |
11 ms |
12268 KB |
Output is correct |
52 |
Correct |
12 ms |
12268 KB |
Output is correct |
53 |
Correct |
10 ms |
12268 KB |
Output is correct |
54 |
Correct |
11 ms |
12268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
11 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
10 ms |
12140 KB |
Output is correct |
12 |
Correct |
10 ms |
12140 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12140 KB |
Output is correct |
16 |
Correct |
9 ms |
12140 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
9 ms |
12140 KB |
Output is correct |
19 |
Correct |
9 ms |
12140 KB |
Output is correct |
20 |
Correct |
9 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
9 ms |
12140 KB |
Output is correct |
23 |
Correct |
9 ms |
12140 KB |
Output is correct |
24 |
Correct |
10 ms |
12140 KB |
Output is correct |
25 |
Correct |
9 ms |
12140 KB |
Output is correct |
26 |
Correct |
65 ms |
17892 KB |
Output is correct |
27 |
Correct |
159 ms |
18304 KB |
Output is correct |
28 |
Correct |
11 ms |
12268 KB |
Output is correct |
29 |
Correct |
9 ms |
12140 KB |
Output is correct |
30 |
Correct |
9 ms |
12140 KB |
Output is correct |
31 |
Correct |
146 ms |
18284 KB |
Output is correct |
32 |
Correct |
12 ms |
12268 KB |
Output is correct |
33 |
Correct |
131 ms |
25196 KB |
Output is correct |
34 |
Correct |
127 ms |
18028 KB |
Output is correct |
35 |
Correct |
11 ms |
12268 KB |
Output is correct |
36 |
Correct |
171 ms |
18796 KB |
Output is correct |
37 |
Correct |
11 ms |
12268 KB |
Output is correct |
38 |
Correct |
11 ms |
12268 KB |
Output is correct |
39 |
Correct |
70 ms |
17892 KB |
Output is correct |
40 |
Correct |
11 ms |
12524 KB |
Output is correct |
41 |
Correct |
127 ms |
17900 KB |
Output is correct |
42 |
Correct |
134 ms |
20332 KB |
Output is correct |
43 |
Correct |
9 ms |
12140 KB |
Output is correct |
44 |
Correct |
134 ms |
25324 KB |
Output is correct |
45 |
Correct |
134 ms |
21740 KB |
Output is correct |
46 |
Correct |
10 ms |
12396 KB |
Output is correct |
47 |
Correct |
11 ms |
12268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
17892 KB |
Output is correct |
2 |
Correct |
71 ms |
19044 KB |
Output is correct |
3 |
Correct |
11 ms |
12268 KB |
Output is correct |
4 |
Correct |
11 ms |
12268 KB |
Output is correct |
5 |
Correct |
10 ms |
12140 KB |
Output is correct |
6 |
Correct |
10 ms |
12140 KB |
Output is correct |
7 |
Correct |
10 ms |
12268 KB |
Output is correct |
8 |
Correct |
138 ms |
18924 KB |
Output is correct |
9 |
Correct |
11 ms |
12268 KB |
Output is correct |
10 |
Correct |
145 ms |
18156 KB |
Output is correct |
11 |
Correct |
9 ms |
12140 KB |
Output is correct |
12 |
Correct |
162 ms |
18156 KB |
Output is correct |
13 |
Correct |
163 ms |
18756 KB |
Output is correct |
14 |
Correct |
143 ms |
19180 KB |
Output is correct |
15 |
Correct |
61 ms |
17892 KB |
Output is correct |
16 |
Correct |
10 ms |
12268 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
74 ms |
19044 KB |
Output is correct |
19 |
Correct |
154 ms |
23020 KB |
Output is correct |
20 |
Correct |
12 ms |
12268 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
66 ms |
18532 KB |
Output is correct |
23 |
Correct |
12 ms |
12268 KB |
Output is correct |
24 |
Correct |
170 ms |
18376 KB |
Output is correct |
25 |
Correct |
137 ms |
21740 KB |
Output is correct |
26 |
Correct |
10 ms |
12268 KB |
Output is correct |
27 |
Correct |
11 ms |
12396 KB |
Output is correct |
28 |
Correct |
11 ms |
12268 KB |
Output is correct |
29 |
Correct |
13 ms |
12268 KB |
Output is correct |
30 |
Correct |
10 ms |
12268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
11 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12268 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
10 ms |
12140 KB |
Output is correct |
12 |
Correct |
10 ms |
12140 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12140 KB |
Output is correct |
16 |
Correct |
9 ms |
12140 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
9 ms |
12140 KB |
Output is correct |
19 |
Correct |
9 ms |
12140 KB |
Output is correct |
20 |
Correct |
9 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
9 ms |
12140 KB |
Output is correct |
23 |
Correct |
9 ms |
12140 KB |
Output is correct |
24 |
Correct |
10 ms |
12140 KB |
Output is correct |
25 |
Correct |
9 ms |
12140 KB |
Output is correct |
26 |
Correct |
11 ms |
12268 KB |
Output is correct |
27 |
Correct |
11 ms |
12416 KB |
Output is correct |
28 |
Correct |
11 ms |
12416 KB |
Output is correct |
29 |
Correct |
11 ms |
12268 KB |
Output is correct |
30 |
Correct |
11 ms |
12268 KB |
Output is correct |
31 |
Correct |
10 ms |
12140 KB |
Output is correct |
32 |
Correct |
11 ms |
12524 KB |
Output is correct |
33 |
Correct |
9 ms |
12140 KB |
Output is correct |
34 |
Correct |
10 ms |
12268 KB |
Output is correct |
35 |
Correct |
13 ms |
12268 KB |
Output is correct |
36 |
Correct |
11 ms |
12396 KB |
Output is correct |
37 |
Correct |
11 ms |
12268 KB |
Output is correct |
38 |
Correct |
10 ms |
12140 KB |
Output is correct |
39 |
Correct |
11 ms |
12268 KB |
Output is correct |
40 |
Correct |
11 ms |
12268 KB |
Output is correct |
41 |
Correct |
11 ms |
12396 KB |
Output is correct |
42 |
Correct |
10 ms |
12268 KB |
Output is correct |
43 |
Correct |
10 ms |
12524 KB |
Output is correct |
44 |
Correct |
9 ms |
12140 KB |
Output is correct |
45 |
Correct |
11 ms |
12396 KB |
Output is correct |
46 |
Correct |
11 ms |
12396 KB |
Output is correct |
47 |
Correct |
9 ms |
12140 KB |
Output is correct |
48 |
Correct |
11 ms |
12268 KB |
Output is correct |
49 |
Correct |
10 ms |
12268 KB |
Output is correct |
50 |
Correct |
11 ms |
12396 KB |
Output is correct |
51 |
Correct |
11 ms |
12268 KB |
Output is correct |
52 |
Correct |
12 ms |
12268 KB |
Output is correct |
53 |
Correct |
10 ms |
12268 KB |
Output is correct |
54 |
Correct |
11 ms |
12268 KB |
Output is correct |
55 |
Correct |
9 ms |
12140 KB |
Output is correct |
56 |
Correct |
65 ms |
17892 KB |
Output is correct |
57 |
Correct |
159 ms |
18304 KB |
Output is correct |
58 |
Correct |
11 ms |
12268 KB |
Output is correct |
59 |
Correct |
9 ms |
12140 KB |
Output is correct |
60 |
Correct |
9 ms |
12140 KB |
Output is correct |
61 |
Correct |
146 ms |
18284 KB |
Output is correct |
62 |
Correct |
12 ms |
12268 KB |
Output is correct |
63 |
Correct |
131 ms |
25196 KB |
Output is correct |
64 |
Correct |
127 ms |
18028 KB |
Output is correct |
65 |
Correct |
11 ms |
12268 KB |
Output is correct |
66 |
Correct |
171 ms |
18796 KB |
Output is correct |
67 |
Correct |
11 ms |
12268 KB |
Output is correct |
68 |
Correct |
11 ms |
12268 KB |
Output is correct |
69 |
Correct |
70 ms |
17892 KB |
Output is correct |
70 |
Correct |
11 ms |
12524 KB |
Output is correct |
71 |
Correct |
127 ms |
17900 KB |
Output is correct |
72 |
Correct |
134 ms |
20332 KB |
Output is correct |
73 |
Correct |
9 ms |
12140 KB |
Output is correct |
74 |
Correct |
134 ms |
25324 KB |
Output is correct |
75 |
Correct |
134 ms |
21740 KB |
Output is correct |
76 |
Correct |
10 ms |
12396 KB |
Output is correct |
77 |
Correct |
11 ms |
12268 KB |
Output is correct |
78 |
Correct |
65 ms |
17892 KB |
Output is correct |
79 |
Correct |
71 ms |
19044 KB |
Output is correct |
80 |
Correct |
11 ms |
12268 KB |
Output is correct |
81 |
Correct |
11 ms |
12268 KB |
Output is correct |
82 |
Correct |
10 ms |
12140 KB |
Output is correct |
83 |
Correct |
10 ms |
12140 KB |
Output is correct |
84 |
Correct |
10 ms |
12268 KB |
Output is correct |
85 |
Correct |
138 ms |
18924 KB |
Output is correct |
86 |
Correct |
11 ms |
12268 KB |
Output is correct |
87 |
Correct |
145 ms |
18156 KB |
Output is correct |
88 |
Correct |
9 ms |
12140 KB |
Output is correct |
89 |
Correct |
162 ms |
18156 KB |
Output is correct |
90 |
Correct |
163 ms |
18756 KB |
Output is correct |
91 |
Correct |
143 ms |
19180 KB |
Output is correct |
92 |
Correct |
61 ms |
17892 KB |
Output is correct |
93 |
Correct |
10 ms |
12268 KB |
Output is correct |
94 |
Correct |
9 ms |
12140 KB |
Output is correct |
95 |
Correct |
74 ms |
19044 KB |
Output is correct |
96 |
Correct |
154 ms |
23020 KB |
Output is correct |
97 |
Correct |
12 ms |
12268 KB |
Output is correct |
98 |
Correct |
9 ms |
12140 KB |
Output is correct |
99 |
Correct |
66 ms |
18532 KB |
Output is correct |
100 |
Correct |
12 ms |
12268 KB |
Output is correct |
101 |
Correct |
170 ms |
18376 KB |
Output is correct |
102 |
Correct |
137 ms |
21740 KB |
Output is correct |
103 |
Correct |
10 ms |
12268 KB |
Output is correct |
104 |
Correct |
11 ms |
12396 KB |
Output is correct |
105 |
Correct |
11 ms |
12268 KB |
Output is correct |
106 |
Correct |
13 ms |
12268 KB |
Output is correct |
107 |
Correct |
10 ms |
12268 KB |
Output is correct |
108 |
Correct |
498 ms |
45664 KB |
Output is correct |
109 |
Correct |
982 ms |
60012 KB |
Output is correct |
110 |
Correct |
895 ms |
71148 KB |
Output is correct |
111 |
Correct |
1031 ms |
71660 KB |
Output is correct |
112 |
Correct |
976 ms |
62004 KB |
Output is correct |
113 |
Correct |
526 ms |
47700 KB |
Output is correct |
114 |
Correct |
1072 ms |
41964 KB |
Output is correct |
115 |
Correct |
1072 ms |
41964 KB |
Output is correct |
116 |
Correct |
1188 ms |
45164 KB |
Output is correct |
117 |
Correct |
1217 ms |
48556 KB |
Output is correct |
118 |
Correct |
1080 ms |
42840 KB |
Output is correct |
119 |
Correct |
1192 ms |
48620 KB |
Output is correct |
120 |
Correct |
972 ms |
62656 KB |
Output is correct |
121 |
Correct |
1258 ms |
48620 KB |
Output is correct |
122 |
Correct |
1182 ms |
46956 KB |
Output is correct |
123 |
Correct |
535 ms |
48092 KB |
Output is correct |
124 |
Correct |
1363 ms |
47828 KB |
Output is correct |