#include <bits/stdc++.h>
#define pb emplace_back
using namespace std;
#define debug(args...) kout("[ " + string(#args) + " ]", args)
void kout() { cerr << endl; }
template <class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ',kout(b...); }
template <class T> void pary(T L, T R) { while (L != R) cerr << *L << " \n"[++L==R]; }
const int MAXN = 500000;
int N, K;
int a, b;
vector <int> path[MAXN+1];
vector <int> G[MAXN+1];
int v[MAXN+1];
int dsu[MAXN+1];
int sze[MAXN+1];
bool vis[MAXN+1];
int k[MAXN+1];
int ans = 0;
int fnd(int a) {
return dsu[a] == a ? a : dsu[a] = fnd(dsu[a]);
}
void Merge(int a, int b) {
a = fnd(a), b = fnd(b);
if (a == b) return;
if (sze[a] > sze[b]) {
dsu[b] = a;
sze[a] += sze[b];
} else {
dsu[a] = b;
sze[b] += sze[a];
}
}
void BFS(int st) {
for (int i = 1; i <= N; i++)
k[i] = 0;
k[st] = st;
queue <int> nxt;
nxt.push(st);
while (nxt.size()) {
int now = nxt.front();
nxt.pop();
for (int i : path[now]) {
if (i == k[now]) continue;
k[i] = now;
nxt.push(i);
}
}
for (int i = 1; i <= N; i++) {
if (v[i] != v[st]) continue;
int t = i;
while (k[t] != t) {
if (fnd(t) == fnd(st)) break;
Merge(t, k[t]);
t = k[t];
}
}
return;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> K;
for (int i = 1; i < N; i++) {
cin >> a >> b;
path[a].pb(b);
path[b].pb(a);
}
for (int i = 1; i <= N; i++) {
cin >> v[i];
dsu[i] = i;
sze[i] = 1;
}
for (int i = 1; i <= N; i++) {
if (vis[v[i]]) continue;
BFS(i);
vis[v[i]] = true;
}
for (int i = 1; i <= N; i++) {
for (int j : path[i]) {
if (fnd(i) == fnd(j)) continue;
G[fnd(i)].pb(fnd(j));
}
}
for (int i = 1; i <= N; i++) {
if (fnd(i) != i || !G[i].size()) continue;
bool flag = true;
for (int j : G[i])
flag &= (j == G[i][0]);
if (flag) ans++;
}
cout << (ans+1)/2 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
16 ms |
23788 KB |
Output is correct |
6 |
Correct |
17 ms |
23916 KB |
Output is correct |
7 |
Correct |
17 ms |
23916 KB |
Output is correct |
8 |
Correct |
17 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23936 KB |
Output is correct |
11 |
Correct |
17 ms |
23916 KB |
Output is correct |
12 |
Correct |
17 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23932 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
17 ms |
23936 KB |
Output is correct |
18 |
Correct |
17 ms |
23924 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
17 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
16 ms |
23788 KB |
Output is correct |
6 |
Correct |
17 ms |
23916 KB |
Output is correct |
7 |
Correct |
17 ms |
23916 KB |
Output is correct |
8 |
Correct |
17 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23936 KB |
Output is correct |
11 |
Correct |
17 ms |
23916 KB |
Output is correct |
12 |
Correct |
17 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23932 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
17 ms |
23936 KB |
Output is correct |
18 |
Correct |
17 ms |
23924 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
17 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
18 ms |
23888 KB |
Output is correct |
26 |
Correct |
160 ms |
24044 KB |
Output is correct |
27 |
Correct |
22 ms |
24064 KB |
Output is correct |
28 |
Correct |
211 ms |
24192 KB |
Output is correct |
29 |
Correct |
213 ms |
24172 KB |
Output is correct |
30 |
Correct |
31 ms |
24044 KB |
Output is correct |
31 |
Correct |
17 ms |
23916 KB |
Output is correct |
32 |
Correct |
242 ms |
24172 KB |
Output is correct |
33 |
Correct |
19 ms |
23916 KB |
Output is correct |
34 |
Correct |
19 ms |
24044 KB |
Output is correct |
35 |
Correct |
213 ms |
24172 KB |
Output is correct |
36 |
Correct |
23 ms |
24044 KB |
Output is correct |
37 |
Correct |
131 ms |
24172 KB |
Output is correct |
38 |
Correct |
19 ms |
23916 KB |
Output is correct |
39 |
Correct |
143 ms |
24044 KB |
Output is correct |
40 |
Correct |
18 ms |
24044 KB |
Output is correct |
41 |
Correct |
19 ms |
24044 KB |
Output is correct |
42 |
Correct |
66 ms |
24128 KB |
Output is correct |
43 |
Correct |
18 ms |
24044 KB |
Output is correct |
44 |
Correct |
17 ms |
24044 KB |
Output is correct |
45 |
Correct |
251 ms |
24172 KB |
Output is correct |
46 |
Correct |
121 ms |
24172 KB |
Output is correct |
47 |
Correct |
17 ms |
23916 KB |
Output is correct |
48 |
Correct |
60 ms |
24044 KB |
Output is correct |
49 |
Correct |
119 ms |
24172 KB |
Output is correct |
50 |
Correct |
202 ms |
24172 KB |
Output is correct |
51 |
Correct |
65 ms |
24044 KB |
Output is correct |
52 |
Correct |
20 ms |
24064 KB |
Output is correct |
53 |
Correct |
81 ms |
24044 KB |
Output is correct |
54 |
Correct |
25 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
16 ms |
23788 KB |
Output is correct |
6 |
Correct |
17 ms |
23916 KB |
Output is correct |
7 |
Correct |
17 ms |
23916 KB |
Output is correct |
8 |
Correct |
17 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23936 KB |
Output is correct |
11 |
Correct |
17 ms |
23916 KB |
Output is correct |
12 |
Correct |
17 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23932 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
17 ms |
23936 KB |
Output is correct |
18 |
Correct |
17 ms |
23924 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
17 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
17 ms |
23916 KB |
Output is correct |
26 |
Correct |
183 ms |
30324 KB |
Output is correct |
27 |
Correct |
609 ms |
29420 KB |
Output is correct |
28 |
Correct |
22 ms |
24044 KB |
Output is correct |
29 |
Correct |
17 ms |
23916 KB |
Output is correct |
30 |
Correct |
18 ms |
23916 KB |
Output is correct |
31 |
Correct |
543 ms |
29420 KB |
Output is correct |
32 |
Correct |
20 ms |
24076 KB |
Output is correct |
33 |
Correct |
856 ms |
29228 KB |
Output is correct |
34 |
Correct |
597 ms |
29548 KB |
Output is correct |
35 |
Correct |
21 ms |
24044 KB |
Output is correct |
36 |
Correct |
879 ms |
29036 KB |
Output is correct |
37 |
Correct |
19 ms |
24044 KB |
Output is correct |
38 |
Correct |
21 ms |
24044 KB |
Output is correct |
39 |
Correct |
214 ms |
29744 KB |
Output is correct |
40 |
Correct |
19 ms |
24004 KB |
Output is correct |
41 |
Correct |
93 ms |
29164 KB |
Output is correct |
42 |
Correct |
889 ms |
29008 KB |
Output is correct |
43 |
Correct |
17 ms |
24064 KB |
Output is correct |
44 |
Correct |
106 ms |
28908 KB |
Output is correct |
45 |
Correct |
544 ms |
29036 KB |
Output is correct |
46 |
Correct |
20 ms |
24044 KB |
Output is correct |
47 |
Correct |
21 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
29492 KB |
Output is correct |
2 |
Execution timed out |
3083 ms |
30852 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
17 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23788 KB |
Output is correct |
5 |
Correct |
16 ms |
23788 KB |
Output is correct |
6 |
Correct |
17 ms |
23916 KB |
Output is correct |
7 |
Correct |
17 ms |
23916 KB |
Output is correct |
8 |
Correct |
17 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
19 ms |
23936 KB |
Output is correct |
11 |
Correct |
17 ms |
23916 KB |
Output is correct |
12 |
Correct |
17 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23932 KB |
Output is correct |
15 |
Correct |
17 ms |
23936 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
17 ms |
23936 KB |
Output is correct |
18 |
Correct |
17 ms |
23924 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
17 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
18 ms |
23888 KB |
Output is correct |
26 |
Correct |
160 ms |
24044 KB |
Output is correct |
27 |
Correct |
22 ms |
24064 KB |
Output is correct |
28 |
Correct |
211 ms |
24192 KB |
Output is correct |
29 |
Correct |
213 ms |
24172 KB |
Output is correct |
30 |
Correct |
31 ms |
24044 KB |
Output is correct |
31 |
Correct |
17 ms |
23916 KB |
Output is correct |
32 |
Correct |
242 ms |
24172 KB |
Output is correct |
33 |
Correct |
19 ms |
23916 KB |
Output is correct |
34 |
Correct |
19 ms |
24044 KB |
Output is correct |
35 |
Correct |
213 ms |
24172 KB |
Output is correct |
36 |
Correct |
23 ms |
24044 KB |
Output is correct |
37 |
Correct |
131 ms |
24172 KB |
Output is correct |
38 |
Correct |
19 ms |
23916 KB |
Output is correct |
39 |
Correct |
143 ms |
24044 KB |
Output is correct |
40 |
Correct |
18 ms |
24044 KB |
Output is correct |
41 |
Correct |
19 ms |
24044 KB |
Output is correct |
42 |
Correct |
66 ms |
24128 KB |
Output is correct |
43 |
Correct |
18 ms |
24044 KB |
Output is correct |
44 |
Correct |
17 ms |
24044 KB |
Output is correct |
45 |
Correct |
251 ms |
24172 KB |
Output is correct |
46 |
Correct |
121 ms |
24172 KB |
Output is correct |
47 |
Correct |
17 ms |
23916 KB |
Output is correct |
48 |
Correct |
60 ms |
24044 KB |
Output is correct |
49 |
Correct |
119 ms |
24172 KB |
Output is correct |
50 |
Correct |
202 ms |
24172 KB |
Output is correct |
51 |
Correct |
65 ms |
24044 KB |
Output is correct |
52 |
Correct |
20 ms |
24064 KB |
Output is correct |
53 |
Correct |
81 ms |
24044 KB |
Output is correct |
54 |
Correct |
25 ms |
24044 KB |
Output is correct |
55 |
Correct |
17 ms |
23916 KB |
Output is correct |
56 |
Correct |
183 ms |
30324 KB |
Output is correct |
57 |
Correct |
609 ms |
29420 KB |
Output is correct |
58 |
Correct |
22 ms |
24044 KB |
Output is correct |
59 |
Correct |
17 ms |
23916 KB |
Output is correct |
60 |
Correct |
18 ms |
23916 KB |
Output is correct |
61 |
Correct |
543 ms |
29420 KB |
Output is correct |
62 |
Correct |
20 ms |
24076 KB |
Output is correct |
63 |
Correct |
856 ms |
29228 KB |
Output is correct |
64 |
Correct |
597 ms |
29548 KB |
Output is correct |
65 |
Correct |
21 ms |
24044 KB |
Output is correct |
66 |
Correct |
879 ms |
29036 KB |
Output is correct |
67 |
Correct |
19 ms |
24044 KB |
Output is correct |
68 |
Correct |
21 ms |
24044 KB |
Output is correct |
69 |
Correct |
214 ms |
29744 KB |
Output is correct |
70 |
Correct |
19 ms |
24004 KB |
Output is correct |
71 |
Correct |
93 ms |
29164 KB |
Output is correct |
72 |
Correct |
889 ms |
29008 KB |
Output is correct |
73 |
Correct |
17 ms |
24064 KB |
Output is correct |
74 |
Correct |
106 ms |
28908 KB |
Output is correct |
75 |
Correct |
544 ms |
29036 KB |
Output is correct |
76 |
Correct |
20 ms |
24044 KB |
Output is correct |
77 |
Correct |
21 ms |
24044 KB |
Output is correct |
78 |
Correct |
199 ms |
29492 KB |
Output is correct |
79 |
Execution timed out |
3083 ms |
30852 KB |
Time limit exceeded |
80 |
Halted |
0 ms |
0 KB |
- |