#include <bits/stdc++.h>
using namespace std;
int c[500005], l[500005], r[500005], t, t2, ans;
vector<int> adj[500005];
void f(int u, int p)
{
t++;
if (!l[c[u]])
l[c[u]]=t;
for (int v:adj[u])
if (v!=p)
f(v, u);
r[c[u]]=t;
}
pair<pair<int, int>, int> f2(int u, int p)
{
t2++;
int t3=t2, x=l[c[u]], y=r[c[u]], z=0;
for (int v:adj[u])
{
if (v!=p)
{
pair<pair<int, int>, int> r=f2(v, u);
x=min(x, r.first.first);
y=max(y, r.first.second);
z+=r.second;
}
}
if (!p)
ans+=(z==1);
else if (x==t3 && y==t2)
{
ans+=!z;
z=1;
}
return {{x, y}, z};
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i=1; i<n; 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 >> c[i];
f(1, 0);
f2(1, 0);
cout << (ans+1)/2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
15004 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
15012 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14956 KB |
Output is correct |
15 |
Correct |
3 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
15016 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
15004 KB |
Output is correct |
21 |
Correct |
3 ms |
14952 KB |
Output is correct |
22 |
Correct |
3 ms |
15004 KB |
Output is correct |
23 |
Correct |
3 ms |
14936 KB |
Output is correct |
24 |
Correct |
3 ms |
14956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
15004 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
15012 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14956 KB |
Output is correct |
15 |
Correct |
3 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
15016 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
15004 KB |
Output is correct |
21 |
Correct |
3 ms |
14952 KB |
Output is correct |
22 |
Correct |
3 ms |
15004 KB |
Output is correct |
23 |
Correct |
3 ms |
14936 KB |
Output is correct |
24 |
Correct |
3 ms |
14956 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
15076 KB |
Output is correct |
28 |
Correct |
4 ms |
15192 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
15092 KB |
Output is correct |
31 |
Correct |
3 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
15284 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
5 ms |
14940 KB |
Output is correct |
36 |
Correct |
4 ms |
14940 KB |
Output is correct |
37 |
Correct |
4 ms |
15016 KB |
Output is correct |
38 |
Correct |
3 ms |
14940 KB |
Output is correct |
39 |
Correct |
4 ms |
14940 KB |
Output is correct |
40 |
Correct |
4 ms |
14940 KB |
Output is correct |
41 |
Correct |
4 ms |
14940 KB |
Output is correct |
42 |
Correct |
5 ms |
14940 KB |
Output is correct |
43 |
Correct |
5 ms |
15452 KB |
Output is correct |
44 |
Correct |
3 ms |
14940 KB |
Output is correct |
45 |
Correct |
4 ms |
15196 KB |
Output is correct |
46 |
Correct |
4 ms |
15124 KB |
Output is correct |
47 |
Correct |
3 ms |
15140 KB |
Output is correct |
48 |
Correct |
4 ms |
15116 KB |
Output is correct |
49 |
Correct |
4 ms |
14940 KB |
Output is correct |
50 |
Correct |
4 ms |
15192 KB |
Output is correct |
51 |
Correct |
4 ms |
14940 KB |
Output is correct |
52 |
Correct |
4 ms |
14940 KB |
Output is correct |
53 |
Correct |
4 ms |
14940 KB |
Output is correct |
54 |
Correct |
4 ms |
14948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
15004 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
15012 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14956 KB |
Output is correct |
15 |
Correct |
3 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
15016 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
15004 KB |
Output is correct |
21 |
Correct |
3 ms |
14952 KB |
Output is correct |
22 |
Correct |
3 ms |
15004 KB |
Output is correct |
23 |
Correct |
3 ms |
14936 KB |
Output is correct |
24 |
Correct |
3 ms |
14956 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
29 ms |
19948 KB |
Output is correct |
27 |
Correct |
33 ms |
19536 KB |
Output is correct |
28 |
Correct |
4 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14956 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
36 ms |
19600 KB |
Output is correct |
32 |
Correct |
4 ms |
15008 KB |
Output is correct |
33 |
Correct |
44 ms |
29744 KB |
Output is correct |
34 |
Correct |
36 ms |
19664 KB |
Output is correct |
35 |
Correct |
4 ms |
14952 KB |
Output is correct |
36 |
Correct |
39 ms |
20628 KB |
Output is correct |
37 |
Correct |
5 ms |
14940 KB |
Output is correct |
38 |
Correct |
4 ms |
14940 KB |
Output is correct |
39 |
Correct |
27 ms |
19788 KB |
Output is correct |
40 |
Correct |
5 ms |
15448 KB |
Output is correct |
41 |
Correct |
36 ms |
19540 KB |
Output is correct |
42 |
Correct |
37 ms |
22864 KB |
Output is correct |
43 |
Correct |
3 ms |
14936 KB |
Output is correct |
44 |
Correct |
42 ms |
30040 KB |
Output is correct |
45 |
Correct |
37 ms |
24664 KB |
Output is correct |
46 |
Correct |
4 ms |
14940 KB |
Output is correct |
47 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
18384 KB |
Output is correct |
2 |
Correct |
35 ms |
18796 KB |
Output is correct |
3 |
Correct |
4 ms |
14936 KB |
Output is correct |
4 |
Correct |
5 ms |
14936 KB |
Output is correct |
5 |
Correct |
3 ms |
15192 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
15108 KB |
Output is correct |
8 |
Correct |
41 ms |
20072 KB |
Output is correct |
9 |
Correct |
4 ms |
15024 KB |
Output is correct |
10 |
Correct |
34 ms |
19548 KB |
Output is correct |
11 |
Correct |
3 ms |
14936 KB |
Output is correct |
12 |
Correct |
36 ms |
19540 KB |
Output is correct |
13 |
Correct |
38 ms |
20100 KB |
Output is correct |
14 |
Correct |
36 ms |
20304 KB |
Output is correct |
15 |
Correct |
27 ms |
19792 KB |
Output is correct |
16 |
Correct |
4 ms |
15136 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
36 ms |
20592 KB |
Output is correct |
19 |
Correct |
47 ms |
25696 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
30 ms |
20176 KB |
Output is correct |
23 |
Correct |
4 ms |
14952 KB |
Output is correct |
24 |
Correct |
38 ms |
19796 KB |
Output is correct |
25 |
Correct |
42 ms |
23888 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
15196 KB |
Output is correct |
28 |
Correct |
4 ms |
15116 KB |
Output is correct |
29 |
Correct |
5 ms |
15020 KB |
Output is correct |
30 |
Correct |
4 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
15004 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
4 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
15012 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
4 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14956 KB |
Output is correct |
15 |
Correct |
3 ms |
14936 KB |
Output is correct |
16 |
Correct |
3 ms |
15016 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
15004 KB |
Output is correct |
21 |
Correct |
3 ms |
14952 KB |
Output is correct |
22 |
Correct |
3 ms |
15004 KB |
Output is correct |
23 |
Correct |
3 ms |
14936 KB |
Output is correct |
24 |
Correct |
3 ms |
14956 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
4 ms |
14940 KB |
Output is correct |
27 |
Correct |
4 ms |
15076 KB |
Output is correct |
28 |
Correct |
4 ms |
15192 KB |
Output is correct |
29 |
Correct |
4 ms |
14940 KB |
Output is correct |
30 |
Correct |
4 ms |
15092 KB |
Output is correct |
31 |
Correct |
3 ms |
14940 KB |
Output is correct |
32 |
Correct |
4 ms |
15284 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
5 ms |
14940 KB |
Output is correct |
35 |
Correct |
5 ms |
14940 KB |
Output is correct |
36 |
Correct |
4 ms |
14940 KB |
Output is correct |
37 |
Correct |
4 ms |
15016 KB |
Output is correct |
38 |
Correct |
3 ms |
14940 KB |
Output is correct |
39 |
Correct |
4 ms |
14940 KB |
Output is correct |
40 |
Correct |
4 ms |
14940 KB |
Output is correct |
41 |
Correct |
4 ms |
14940 KB |
Output is correct |
42 |
Correct |
5 ms |
14940 KB |
Output is correct |
43 |
Correct |
5 ms |
15452 KB |
Output is correct |
44 |
Correct |
3 ms |
14940 KB |
Output is correct |
45 |
Correct |
4 ms |
15196 KB |
Output is correct |
46 |
Correct |
4 ms |
15124 KB |
Output is correct |
47 |
Correct |
3 ms |
15140 KB |
Output is correct |
48 |
Correct |
4 ms |
15116 KB |
Output is correct |
49 |
Correct |
4 ms |
14940 KB |
Output is correct |
50 |
Correct |
4 ms |
15192 KB |
Output is correct |
51 |
Correct |
4 ms |
14940 KB |
Output is correct |
52 |
Correct |
4 ms |
14940 KB |
Output is correct |
53 |
Correct |
4 ms |
14940 KB |
Output is correct |
54 |
Correct |
4 ms |
14948 KB |
Output is correct |
55 |
Correct |
4 ms |
14940 KB |
Output is correct |
56 |
Correct |
29 ms |
19948 KB |
Output is correct |
57 |
Correct |
33 ms |
19536 KB |
Output is correct |
58 |
Correct |
4 ms |
14940 KB |
Output is correct |
59 |
Correct |
3 ms |
14956 KB |
Output is correct |
60 |
Correct |
3 ms |
14940 KB |
Output is correct |
61 |
Correct |
36 ms |
19600 KB |
Output is correct |
62 |
Correct |
4 ms |
15008 KB |
Output is correct |
63 |
Correct |
44 ms |
29744 KB |
Output is correct |
64 |
Correct |
36 ms |
19664 KB |
Output is correct |
65 |
Correct |
4 ms |
14952 KB |
Output is correct |
66 |
Correct |
39 ms |
20628 KB |
Output is correct |
67 |
Correct |
5 ms |
14940 KB |
Output is correct |
68 |
Correct |
4 ms |
14940 KB |
Output is correct |
69 |
Correct |
27 ms |
19788 KB |
Output is correct |
70 |
Correct |
5 ms |
15448 KB |
Output is correct |
71 |
Correct |
36 ms |
19540 KB |
Output is correct |
72 |
Correct |
37 ms |
22864 KB |
Output is correct |
73 |
Correct |
3 ms |
14936 KB |
Output is correct |
74 |
Correct |
42 ms |
30040 KB |
Output is correct |
75 |
Correct |
37 ms |
24664 KB |
Output is correct |
76 |
Correct |
4 ms |
14940 KB |
Output is correct |
77 |
Correct |
4 ms |
14940 KB |
Output is correct |
78 |
Correct |
26 ms |
18384 KB |
Output is correct |
79 |
Correct |
35 ms |
18796 KB |
Output is correct |
80 |
Correct |
4 ms |
14936 KB |
Output is correct |
81 |
Correct |
5 ms |
14936 KB |
Output is correct |
82 |
Correct |
3 ms |
15192 KB |
Output is correct |
83 |
Correct |
3 ms |
14940 KB |
Output is correct |
84 |
Correct |
4 ms |
15108 KB |
Output is correct |
85 |
Correct |
41 ms |
20072 KB |
Output is correct |
86 |
Correct |
4 ms |
15024 KB |
Output is correct |
87 |
Correct |
34 ms |
19548 KB |
Output is correct |
88 |
Correct |
3 ms |
14936 KB |
Output is correct |
89 |
Correct |
36 ms |
19540 KB |
Output is correct |
90 |
Correct |
38 ms |
20100 KB |
Output is correct |
91 |
Correct |
36 ms |
20304 KB |
Output is correct |
92 |
Correct |
27 ms |
19792 KB |
Output is correct |
93 |
Correct |
4 ms |
15136 KB |
Output is correct |
94 |
Correct |
4 ms |
14940 KB |
Output is correct |
95 |
Correct |
36 ms |
20592 KB |
Output is correct |
96 |
Correct |
47 ms |
25696 KB |
Output is correct |
97 |
Correct |
4 ms |
14940 KB |
Output is correct |
98 |
Correct |
3 ms |
14940 KB |
Output is correct |
99 |
Correct |
30 ms |
20176 KB |
Output is correct |
100 |
Correct |
4 ms |
14952 KB |
Output is correct |
101 |
Correct |
38 ms |
19796 KB |
Output is correct |
102 |
Correct |
42 ms |
23888 KB |
Output is correct |
103 |
Correct |
4 ms |
14940 KB |
Output is correct |
104 |
Correct |
4 ms |
15196 KB |
Output is correct |
105 |
Correct |
4 ms |
15116 KB |
Output is correct |
106 |
Correct |
5 ms |
15020 KB |
Output is correct |
107 |
Correct |
4 ms |
14940 KB |
Output is correct |
108 |
Correct |
204 ms |
44112 KB |
Output is correct |
109 |
Correct |
495 ms |
65472 KB |
Output is correct |
110 |
Correct |
496 ms |
80928 KB |
Output is correct |
111 |
Correct |
535 ms |
76672 KB |
Output is correct |
112 |
Correct |
422 ms |
63056 KB |
Output is correct |
113 |
Correct |
221 ms |
45364 KB |
Output is correct |
114 |
Correct |
311 ms |
39676 KB |
Output is correct |
115 |
Correct |
335 ms |
39828 KB |
Output is correct |
116 |
Correct |
356 ms |
42132 KB |
Output is correct |
117 |
Correct |
385 ms |
44184 KB |
Output is correct |
118 |
Correct |
349 ms |
40016 KB |
Output is correct |
119 |
Correct |
368 ms |
44152 KB |
Output is correct |
120 |
Correct |
477 ms |
64140 KB |
Output is correct |
121 |
Correct |
390 ms |
44188 KB |
Output is correct |
122 |
Correct |
350 ms |
43304 KB |
Output is correct |
123 |
Correct |
227 ms |
45504 KB |
Output is correct |
124 |
Correct |
477 ms |
43828 KB |
Output is correct |