#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 500'010;
int col[N];
int cnt_col[N];
bool is_in_sub[N];
vector<int> A[N];
int n, k;
class s2l {
private:
int cnt_unfin = 0;
map<int, int> mp;
public:
void insert(int x, int cnt=1)
{
int &val = mp[x];
cnt_unfin += val == 0;
val += cnt;
cnt_unfin -= val == cnt_col[x];
}
void merge(s2l *that)
{
if (this->mp.size() < that->mp.size()) {
this->mp.swap(that->mp);
swap(this->cnt_unfin, that->cnt_unfin);
}
for (auto [x, y] : that->mp)
this->insert(x, y);
delete(that);
}
bool has_unfin()
{
return cnt_unfin;
}
};
s2l *dfs1(int v, int p)
{
s2l *obj = new s2l;
obj->insert(col[v]);
for (int u : A[v]) {
if (u != p)
obj->merge(dfs1(u, v));
}
if (!obj->has_unfin() && p != -1) {
is_in_sub[v] = 1;
is_in_sub[p] = 1;
}
return obj;
}
bool dfs2(int v, int p)
{
for (int u : A[v]) {
if (u != p)
if (dfs2(u, v))
is_in_sub[v] = 1;
}
return is_in_sub[v];
}
int dfs3(int v, int p)
{
int nbr = 0;
int leaf = 0;
for (int u : A[v]) {
if (!is_in_sub[u])
continue;
nbr++;
if (u == p)
continue;
leaf += dfs3(u, v);
}
leaf += nbr == 1;
return leaf;
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> k;
Loop (i,1,n) {
int v, u;
cin >> v >> u;
--v; --u;
A[v].push_back(u);
A[u].push_back(v);
}
Loop (i,0,n) {
int c;
cin >> c;
col[i] = c-1;
++cnt_col[c-1];
}
dfs1(0, -1); // I CAN LEAK ANYTHING!
int rt;
for (rt = 0; rt < n && !is_in_sub[rt]; ++rt);
if (rt == n) {
cout << "0\n";
return 0;
}
dfs2(rt, -1);
int leaves = dfs3(rt, -1);
cout << (leaves+1)/2 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
12068 KB |
Output is correct |
3 |
Correct |
8 ms |
12008 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12072 KB |
Output is correct |
6 |
Correct |
9 ms |
12172 KB |
Output is correct |
7 |
Correct |
9 ms |
12072 KB |
Output is correct |
8 |
Correct |
9 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
12080 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
9 ms |
12108 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
9 ms |
12076 KB |
Output is correct |
14 |
Correct |
8 ms |
11988 KB |
Output is correct |
15 |
Correct |
10 ms |
12072 KB |
Output is correct |
16 |
Correct |
7 ms |
12076 KB |
Output is correct |
17 |
Correct |
8 ms |
12076 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
12076 KB |
Output is correct |
20 |
Correct |
8 ms |
12040 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
7 ms |
12092 KB |
Output is correct |
23 |
Correct |
8 ms |
12080 KB |
Output is correct |
24 |
Correct |
7 ms |
12076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
12068 KB |
Output is correct |
3 |
Correct |
8 ms |
12008 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12072 KB |
Output is correct |
6 |
Correct |
9 ms |
12172 KB |
Output is correct |
7 |
Correct |
9 ms |
12072 KB |
Output is correct |
8 |
Correct |
9 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
12080 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
9 ms |
12108 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
9 ms |
12076 KB |
Output is correct |
14 |
Correct |
8 ms |
11988 KB |
Output is correct |
15 |
Correct |
10 ms |
12072 KB |
Output is correct |
16 |
Correct |
7 ms |
12076 KB |
Output is correct |
17 |
Correct |
8 ms |
12076 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
12076 KB |
Output is correct |
20 |
Correct |
8 ms |
12040 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
7 ms |
12092 KB |
Output is correct |
23 |
Correct |
8 ms |
12080 KB |
Output is correct |
24 |
Correct |
7 ms |
12076 KB |
Output is correct |
25 |
Correct |
8 ms |
12092 KB |
Output is correct |
26 |
Correct |
12 ms |
12336 KB |
Output is correct |
27 |
Correct |
9 ms |
12244 KB |
Output is correct |
28 |
Correct |
9 ms |
12628 KB |
Output is correct |
29 |
Correct |
11 ms |
12420 KB |
Output is correct |
30 |
Correct |
10 ms |
12212 KB |
Output is correct |
31 |
Correct |
8 ms |
12076 KB |
Output is correct |
32 |
Correct |
11 ms |
12680 KB |
Output is correct |
33 |
Correct |
8 ms |
11988 KB |
Output is correct |
34 |
Correct |
10 ms |
12232 KB |
Output is correct |
35 |
Correct |
11 ms |
12292 KB |
Output is correct |
36 |
Correct |
9 ms |
12196 KB |
Output is correct |
37 |
Correct |
10 ms |
12244 KB |
Output is correct |
38 |
Correct |
7 ms |
11988 KB |
Output is correct |
39 |
Correct |
10 ms |
12340 KB |
Output is correct |
40 |
Correct |
9 ms |
12160 KB |
Output is correct |
41 |
Correct |
9 ms |
12152 KB |
Output is correct |
42 |
Correct |
9 ms |
12340 KB |
Output is correct |
43 |
Correct |
10 ms |
12852 KB |
Output is correct |
44 |
Correct |
8 ms |
11988 KB |
Output is correct |
45 |
Correct |
10 ms |
12500 KB |
Output is correct |
46 |
Correct |
11 ms |
12244 KB |
Output is correct |
47 |
Correct |
10 ms |
12076 KB |
Output is correct |
48 |
Correct |
10 ms |
12212 KB |
Output is correct |
49 |
Correct |
10 ms |
12372 KB |
Output is correct |
50 |
Correct |
10 ms |
12500 KB |
Output is correct |
51 |
Correct |
10 ms |
12244 KB |
Output is correct |
52 |
Correct |
12 ms |
12244 KB |
Output is correct |
53 |
Correct |
11 ms |
12212 KB |
Output is correct |
54 |
Correct |
11 ms |
12128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
12068 KB |
Output is correct |
3 |
Correct |
8 ms |
12008 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12072 KB |
Output is correct |
6 |
Correct |
9 ms |
12172 KB |
Output is correct |
7 |
Correct |
9 ms |
12072 KB |
Output is correct |
8 |
Correct |
9 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
12080 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
9 ms |
12108 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
9 ms |
12076 KB |
Output is correct |
14 |
Correct |
8 ms |
11988 KB |
Output is correct |
15 |
Correct |
10 ms |
12072 KB |
Output is correct |
16 |
Correct |
7 ms |
12076 KB |
Output is correct |
17 |
Correct |
8 ms |
12076 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
12076 KB |
Output is correct |
20 |
Correct |
8 ms |
12040 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
7 ms |
12092 KB |
Output is correct |
23 |
Correct |
8 ms |
12080 KB |
Output is correct |
24 |
Correct |
7 ms |
12076 KB |
Output is correct |
25 |
Correct |
9 ms |
12072 KB |
Output is correct |
26 |
Correct |
46 ms |
17344 KB |
Output is correct |
27 |
Correct |
83 ms |
17172 KB |
Output is correct |
28 |
Correct |
8 ms |
12244 KB |
Output is correct |
29 |
Correct |
7 ms |
11988 KB |
Output is correct |
30 |
Correct |
8 ms |
12008 KB |
Output is correct |
31 |
Correct |
61 ms |
17232 KB |
Output is correct |
32 |
Correct |
7 ms |
12216 KB |
Output is correct |
33 |
Correct |
96 ms |
38860 KB |
Output is correct |
34 |
Correct |
87 ms |
17148 KB |
Output is correct |
35 |
Correct |
8 ms |
12244 KB |
Output is correct |
36 |
Correct |
107 ms |
19660 KB |
Output is correct |
37 |
Correct |
8 ms |
12212 KB |
Output is correct |
38 |
Correct |
8 ms |
12212 KB |
Output is correct |
39 |
Correct |
53 ms |
17292 KB |
Output is correct |
40 |
Correct |
8 ms |
12884 KB |
Output is correct |
41 |
Correct |
76 ms |
16980 KB |
Output is correct |
42 |
Correct |
110 ms |
24216 KB |
Output is correct |
43 |
Correct |
7 ms |
11988 KB |
Output is correct |
44 |
Correct |
92 ms |
39648 KB |
Output is correct |
45 |
Correct |
82 ms |
29612 KB |
Output is correct |
46 |
Correct |
8 ms |
12216 KB |
Output is correct |
47 |
Correct |
9 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
17356 KB |
Output is correct |
2 |
Correct |
94 ms |
22804 KB |
Output is correct |
3 |
Correct |
9 ms |
12248 KB |
Output is correct |
4 |
Correct |
8 ms |
12168 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
8 ms |
12196 KB |
Output is correct |
8 |
Correct |
128 ms |
20264 KB |
Output is correct |
9 |
Correct |
8 ms |
12244 KB |
Output is correct |
10 |
Correct |
88 ms |
17292 KB |
Output is correct |
11 |
Correct |
9 ms |
11988 KB |
Output is correct |
12 |
Correct |
83 ms |
17436 KB |
Output is correct |
13 |
Correct |
118 ms |
20420 KB |
Output is correct |
14 |
Correct |
151 ms |
23760 KB |
Output is correct |
15 |
Correct |
45 ms |
17348 KB |
Output is correct |
16 |
Correct |
8 ms |
12244 KB |
Output is correct |
17 |
Correct |
8 ms |
12008 KB |
Output is correct |
18 |
Correct |
111 ms |
22328 KB |
Output is correct |
19 |
Correct |
145 ms |
32600 KB |
Output is correct |
20 |
Correct |
8 ms |
12244 KB |
Output is correct |
21 |
Correct |
7 ms |
12076 KB |
Output is correct |
22 |
Correct |
70 ms |
20164 KB |
Output is correct |
23 |
Correct |
8 ms |
12244 KB |
Output is correct |
24 |
Correct |
75 ms |
17996 KB |
Output is correct |
25 |
Correct |
142 ms |
28828 KB |
Output is correct |
26 |
Correct |
8 ms |
12284 KB |
Output is correct |
27 |
Correct |
9 ms |
12500 KB |
Output is correct |
28 |
Correct |
8 ms |
12244 KB |
Output is correct |
29 |
Correct |
8 ms |
12148 KB |
Output is correct |
30 |
Correct |
8 ms |
12312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
8 ms |
12068 KB |
Output is correct |
3 |
Correct |
8 ms |
12008 KB |
Output is correct |
4 |
Correct |
7 ms |
11988 KB |
Output is correct |
5 |
Correct |
8 ms |
12072 KB |
Output is correct |
6 |
Correct |
9 ms |
12172 KB |
Output is correct |
7 |
Correct |
9 ms |
12072 KB |
Output is correct |
8 |
Correct |
9 ms |
11988 KB |
Output is correct |
9 |
Correct |
8 ms |
12080 KB |
Output is correct |
10 |
Correct |
7 ms |
11988 KB |
Output is correct |
11 |
Correct |
9 ms |
12108 KB |
Output is correct |
12 |
Correct |
7 ms |
11988 KB |
Output is correct |
13 |
Correct |
9 ms |
12076 KB |
Output is correct |
14 |
Correct |
8 ms |
11988 KB |
Output is correct |
15 |
Correct |
10 ms |
12072 KB |
Output is correct |
16 |
Correct |
7 ms |
12076 KB |
Output is correct |
17 |
Correct |
8 ms |
12076 KB |
Output is correct |
18 |
Correct |
9 ms |
11988 KB |
Output is correct |
19 |
Correct |
7 ms |
12076 KB |
Output is correct |
20 |
Correct |
8 ms |
12040 KB |
Output is correct |
21 |
Correct |
8 ms |
11988 KB |
Output is correct |
22 |
Correct |
7 ms |
12092 KB |
Output is correct |
23 |
Correct |
8 ms |
12080 KB |
Output is correct |
24 |
Correct |
7 ms |
12076 KB |
Output is correct |
25 |
Correct |
8 ms |
12092 KB |
Output is correct |
26 |
Correct |
12 ms |
12336 KB |
Output is correct |
27 |
Correct |
9 ms |
12244 KB |
Output is correct |
28 |
Correct |
9 ms |
12628 KB |
Output is correct |
29 |
Correct |
11 ms |
12420 KB |
Output is correct |
30 |
Correct |
10 ms |
12212 KB |
Output is correct |
31 |
Correct |
8 ms |
12076 KB |
Output is correct |
32 |
Correct |
11 ms |
12680 KB |
Output is correct |
33 |
Correct |
8 ms |
11988 KB |
Output is correct |
34 |
Correct |
10 ms |
12232 KB |
Output is correct |
35 |
Correct |
11 ms |
12292 KB |
Output is correct |
36 |
Correct |
9 ms |
12196 KB |
Output is correct |
37 |
Correct |
10 ms |
12244 KB |
Output is correct |
38 |
Correct |
7 ms |
11988 KB |
Output is correct |
39 |
Correct |
10 ms |
12340 KB |
Output is correct |
40 |
Correct |
9 ms |
12160 KB |
Output is correct |
41 |
Correct |
9 ms |
12152 KB |
Output is correct |
42 |
Correct |
9 ms |
12340 KB |
Output is correct |
43 |
Correct |
10 ms |
12852 KB |
Output is correct |
44 |
Correct |
8 ms |
11988 KB |
Output is correct |
45 |
Correct |
10 ms |
12500 KB |
Output is correct |
46 |
Correct |
11 ms |
12244 KB |
Output is correct |
47 |
Correct |
10 ms |
12076 KB |
Output is correct |
48 |
Correct |
10 ms |
12212 KB |
Output is correct |
49 |
Correct |
10 ms |
12372 KB |
Output is correct |
50 |
Correct |
10 ms |
12500 KB |
Output is correct |
51 |
Correct |
10 ms |
12244 KB |
Output is correct |
52 |
Correct |
12 ms |
12244 KB |
Output is correct |
53 |
Correct |
11 ms |
12212 KB |
Output is correct |
54 |
Correct |
11 ms |
12128 KB |
Output is correct |
55 |
Correct |
9 ms |
12072 KB |
Output is correct |
56 |
Correct |
46 ms |
17344 KB |
Output is correct |
57 |
Correct |
83 ms |
17172 KB |
Output is correct |
58 |
Correct |
8 ms |
12244 KB |
Output is correct |
59 |
Correct |
7 ms |
11988 KB |
Output is correct |
60 |
Correct |
8 ms |
12008 KB |
Output is correct |
61 |
Correct |
61 ms |
17232 KB |
Output is correct |
62 |
Correct |
7 ms |
12216 KB |
Output is correct |
63 |
Correct |
96 ms |
38860 KB |
Output is correct |
64 |
Correct |
87 ms |
17148 KB |
Output is correct |
65 |
Correct |
8 ms |
12244 KB |
Output is correct |
66 |
Correct |
107 ms |
19660 KB |
Output is correct |
67 |
Correct |
8 ms |
12212 KB |
Output is correct |
68 |
Correct |
8 ms |
12212 KB |
Output is correct |
69 |
Correct |
53 ms |
17292 KB |
Output is correct |
70 |
Correct |
8 ms |
12884 KB |
Output is correct |
71 |
Correct |
76 ms |
16980 KB |
Output is correct |
72 |
Correct |
110 ms |
24216 KB |
Output is correct |
73 |
Correct |
7 ms |
11988 KB |
Output is correct |
74 |
Correct |
92 ms |
39648 KB |
Output is correct |
75 |
Correct |
82 ms |
29612 KB |
Output is correct |
76 |
Correct |
8 ms |
12216 KB |
Output is correct |
77 |
Correct |
9 ms |
12244 KB |
Output is correct |
78 |
Correct |
51 ms |
17356 KB |
Output is correct |
79 |
Correct |
94 ms |
22804 KB |
Output is correct |
80 |
Correct |
9 ms |
12248 KB |
Output is correct |
81 |
Correct |
8 ms |
12168 KB |
Output is correct |
82 |
Correct |
6 ms |
11988 KB |
Output is correct |
83 |
Correct |
6 ms |
11988 KB |
Output is correct |
84 |
Correct |
8 ms |
12196 KB |
Output is correct |
85 |
Correct |
128 ms |
20264 KB |
Output is correct |
86 |
Correct |
8 ms |
12244 KB |
Output is correct |
87 |
Correct |
88 ms |
17292 KB |
Output is correct |
88 |
Correct |
9 ms |
11988 KB |
Output is correct |
89 |
Correct |
83 ms |
17436 KB |
Output is correct |
90 |
Correct |
118 ms |
20420 KB |
Output is correct |
91 |
Correct |
151 ms |
23760 KB |
Output is correct |
92 |
Correct |
45 ms |
17348 KB |
Output is correct |
93 |
Correct |
8 ms |
12244 KB |
Output is correct |
94 |
Correct |
8 ms |
12008 KB |
Output is correct |
95 |
Correct |
111 ms |
22328 KB |
Output is correct |
96 |
Correct |
145 ms |
32600 KB |
Output is correct |
97 |
Correct |
8 ms |
12244 KB |
Output is correct |
98 |
Correct |
7 ms |
12076 KB |
Output is correct |
99 |
Correct |
70 ms |
20164 KB |
Output is correct |
100 |
Correct |
8 ms |
12244 KB |
Output is correct |
101 |
Correct |
75 ms |
17996 KB |
Output is correct |
102 |
Correct |
142 ms |
28828 KB |
Output is correct |
103 |
Correct |
8 ms |
12284 KB |
Output is correct |
104 |
Correct |
9 ms |
12500 KB |
Output is correct |
105 |
Correct |
8 ms |
12244 KB |
Output is correct |
106 |
Correct |
8 ms |
12148 KB |
Output is correct |
107 |
Correct |
8 ms |
12312 KB |
Output is correct |
108 |
Correct |
561 ms |
52284 KB |
Output is correct |
109 |
Correct |
656 ms |
92972 KB |
Output is correct |
110 |
Correct |
558 ms |
126520 KB |
Output is correct |
111 |
Correct |
1220 ms |
118076 KB |
Output is correct |
112 |
Correct |
1047 ms |
98164 KB |
Output is correct |
113 |
Correct |
665 ms |
65144 KB |
Output is correct |
114 |
Correct |
552 ms |
37968 KB |
Output is correct |
115 |
Correct |
509 ms |
37956 KB |
Output is correct |
116 |
Correct |
762 ms |
46852 KB |
Output is correct |
117 |
Correct |
1137 ms |
69160 KB |
Output is correct |
118 |
Correct |
517 ms |
38752 KB |
Output is correct |
119 |
Correct |
1052 ms |
68380 KB |
Output is correct |
120 |
Correct |
1035 ms |
98136 KB |
Output is correct |
121 |
Correct |
1176 ms |
66996 KB |
Output is correct |
122 |
Correct |
1040 ms |
57852 KB |
Output is correct |
123 |
Correct |
655 ms |
67600 KB |
Output is correct |
124 |
Correct |
1108 ms |
67824 KB |
Output is correct |