#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, k;
int dp[20][maxn];
int where[maxn];
int tym = 0;
vector<int> g[maxn], state[maxn];
int s[maxn];
int lvl[maxn];
int p[maxn], sz[maxn];
using ii = pair<int, int>;
vector<ii> bridges;
int cnt[maxn];
int find(int u) {
return p[u] == u ? u : p[u] = find(p[u]);
}
void unite(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return;
if(sz[u] > sz[v]) swap(u, v);
sz[v] += sz[u];
p[u] = v;
}
void dfs(int u, int par) {
for(int v : g[u]) if(v != par) {
dfs(v, u);
if(s[v]) unite(u, v);
else bridges.emplace_back(u, v);
s[u] += s[v];
}
}
void dfs0(int u, int par) {
lvl[u] = par + 1 ? lvl[par] + 1 : 0;
where[u] = ++ tym;
dp[0][u] = par;
for(int i = 1; i <= 19; i++) {
int mid = dp[i - 1][u];
if(mid != -1) dp[i][u] = dp[i - 1][mid];
}
for(int v : g[u]) if(v != par) {
dfs0(v, u);
}
}
int __lca(int u, int v) {
if(lvl[u] > lvl[v]) swap(u, v);
int d = lvl[v] - lvl[u];
for(int i = 19; i >= 0; i--) if(d >> i & 1) {
v = dp[i][v];
}
if(u == v) return u;
for(int i = 19; i >= 0; i--) {
if(dp[i][u] != dp[i][v]) {
u = dp[i][u];
v = dp[i][v];
}
}
return dp[0][u];
}
int main(int argc, char const *argv[])
{
// freopen("in", "r", stdin);
memset(dp, -1, sizeof dp);
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
for(int i = 1; i <= n; i++) {
int x; cin >> x;
state[x].emplace_back(i);
}
dfs0(1, -1);
for(int i = 1; i <= k; i++) {
sort(state[i].begin(), state[i].end(), [&](int x, int y){ return where[x] < where[y]; });
for(int j = 1; j < state[i].size(); j++) {
int u = state[i][j - 1], v = state[i][j];
s[u]++, s[v]++, ----s[__lca(u, v)];
}
}
for(int i = 1; i <= n; i++){
p[i] = i, sz[i] = 1;
}
dfs(1, -1);
vector<int> tmp;
for(ii e : bridges) {
int u = e.first;
int v = e.second;
u = find(u);
v = find(v);
cnt[u]++;
cnt[v]++;
tmp.emplace_back(u);
tmp.emplace_back(v);
}
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
int ans = 0;
for(int u : tmp) {
if(cnt[u] == 1) ans++;
}
ans = (ans + 1) / 2;
cout << ans << endl;
return 0;
}
Compilation message
mergers.cpp: In function 'int main(int, const char**)':
mergers.cpp:85:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; j < state[i].size(); j++) {
~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
62968 KB |
Output is correct |
2 |
Correct |
49 ms |
62968 KB |
Output is correct |
3 |
Correct |
49 ms |
62968 KB |
Output is correct |
4 |
Correct |
49 ms |
62968 KB |
Output is correct |
5 |
Correct |
48 ms |
62940 KB |
Output is correct |
6 |
Correct |
48 ms |
62968 KB |
Output is correct |
7 |
Correct |
49 ms |
62968 KB |
Output is correct |
8 |
Correct |
51 ms |
62968 KB |
Output is correct |
9 |
Correct |
48 ms |
62968 KB |
Output is correct |
10 |
Correct |
49 ms |
62972 KB |
Output is correct |
11 |
Correct |
49 ms |
62968 KB |
Output is correct |
12 |
Correct |
50 ms |
62968 KB |
Output is correct |
13 |
Correct |
50 ms |
63096 KB |
Output is correct |
14 |
Correct |
48 ms |
62968 KB |
Output is correct |
15 |
Correct |
49 ms |
62968 KB |
Output is correct |
16 |
Correct |
49 ms |
63016 KB |
Output is correct |
17 |
Correct |
50 ms |
62944 KB |
Output is correct |
18 |
Correct |
59 ms |
62968 KB |
Output is correct |
19 |
Correct |
51 ms |
62968 KB |
Output is correct |
20 |
Correct |
50 ms |
62968 KB |
Output is correct |
21 |
Correct |
48 ms |
62968 KB |
Output is correct |
22 |
Correct |
49 ms |
62968 KB |
Output is correct |
23 |
Correct |
50 ms |
62968 KB |
Output is correct |
24 |
Correct |
50 ms |
63068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
62968 KB |
Output is correct |
2 |
Correct |
49 ms |
62968 KB |
Output is correct |
3 |
Correct |
49 ms |
62968 KB |
Output is correct |
4 |
Correct |
49 ms |
62968 KB |
Output is correct |
5 |
Correct |
48 ms |
62940 KB |
Output is correct |
6 |
Correct |
48 ms |
62968 KB |
Output is correct |
7 |
Correct |
49 ms |
62968 KB |
Output is correct |
8 |
Correct |
51 ms |
62968 KB |
Output is correct |
9 |
Correct |
48 ms |
62968 KB |
Output is correct |
10 |
Correct |
49 ms |
62972 KB |
Output is correct |
11 |
Correct |
49 ms |
62968 KB |
Output is correct |
12 |
Correct |
50 ms |
62968 KB |
Output is correct |
13 |
Correct |
50 ms |
63096 KB |
Output is correct |
14 |
Correct |
48 ms |
62968 KB |
Output is correct |
15 |
Correct |
49 ms |
62968 KB |
Output is correct |
16 |
Correct |
49 ms |
63016 KB |
Output is correct |
17 |
Correct |
50 ms |
62944 KB |
Output is correct |
18 |
Correct |
59 ms |
62968 KB |
Output is correct |
19 |
Correct |
51 ms |
62968 KB |
Output is correct |
20 |
Correct |
50 ms |
62968 KB |
Output is correct |
21 |
Correct |
48 ms |
62968 KB |
Output is correct |
22 |
Correct |
49 ms |
62968 KB |
Output is correct |
23 |
Correct |
50 ms |
62968 KB |
Output is correct |
24 |
Correct |
50 ms |
63068 KB |
Output is correct |
25 |
Correct |
49 ms |
62968 KB |
Output is correct |
26 |
Correct |
53 ms |
63324 KB |
Output is correct |
27 |
Correct |
52 ms |
63224 KB |
Output is correct |
28 |
Correct |
53 ms |
63480 KB |
Output is correct |
29 |
Correct |
54 ms |
63352 KB |
Output is correct |
30 |
Correct |
54 ms |
63352 KB |
Output is correct |
31 |
Correct |
50 ms |
62968 KB |
Output is correct |
32 |
Correct |
55 ms |
63480 KB |
Output is correct |
33 |
Correct |
49 ms |
62972 KB |
Output is correct |
34 |
Correct |
52 ms |
63224 KB |
Output is correct |
35 |
Correct |
52 ms |
63352 KB |
Output is correct |
36 |
Correct |
53 ms |
63224 KB |
Output is correct |
37 |
Correct |
53 ms |
63224 KB |
Output is correct |
38 |
Correct |
50 ms |
63000 KB |
Output is correct |
39 |
Correct |
52 ms |
63228 KB |
Output is correct |
40 |
Correct |
52 ms |
63224 KB |
Output is correct |
41 |
Correct |
54 ms |
63224 KB |
Output is correct |
42 |
Correct |
52 ms |
63224 KB |
Output is correct |
43 |
Correct |
54 ms |
63480 KB |
Output is correct |
44 |
Correct |
49 ms |
62968 KB |
Output is correct |
45 |
Correct |
53 ms |
63480 KB |
Output is correct |
46 |
Correct |
55 ms |
63352 KB |
Output is correct |
47 |
Correct |
49 ms |
62968 KB |
Output is correct |
48 |
Correct |
51 ms |
63224 KB |
Output is correct |
49 |
Correct |
52 ms |
63352 KB |
Output is correct |
50 |
Correct |
53 ms |
63456 KB |
Output is correct |
51 |
Correct |
52 ms |
63224 KB |
Output is correct |
52 |
Correct |
55 ms |
63224 KB |
Output is correct |
53 |
Correct |
52 ms |
63352 KB |
Output is correct |
54 |
Correct |
51 ms |
63224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
62968 KB |
Output is correct |
2 |
Correct |
49 ms |
62968 KB |
Output is correct |
3 |
Correct |
49 ms |
62968 KB |
Output is correct |
4 |
Correct |
49 ms |
62968 KB |
Output is correct |
5 |
Correct |
48 ms |
62940 KB |
Output is correct |
6 |
Correct |
48 ms |
62968 KB |
Output is correct |
7 |
Correct |
49 ms |
62968 KB |
Output is correct |
8 |
Correct |
51 ms |
62968 KB |
Output is correct |
9 |
Correct |
48 ms |
62968 KB |
Output is correct |
10 |
Correct |
49 ms |
62972 KB |
Output is correct |
11 |
Correct |
49 ms |
62968 KB |
Output is correct |
12 |
Correct |
50 ms |
62968 KB |
Output is correct |
13 |
Correct |
50 ms |
63096 KB |
Output is correct |
14 |
Correct |
48 ms |
62968 KB |
Output is correct |
15 |
Correct |
49 ms |
62968 KB |
Output is correct |
16 |
Correct |
49 ms |
63016 KB |
Output is correct |
17 |
Correct |
50 ms |
62944 KB |
Output is correct |
18 |
Correct |
59 ms |
62968 KB |
Output is correct |
19 |
Correct |
51 ms |
62968 KB |
Output is correct |
20 |
Correct |
50 ms |
62968 KB |
Output is correct |
21 |
Correct |
48 ms |
62968 KB |
Output is correct |
22 |
Correct |
49 ms |
62968 KB |
Output is correct |
23 |
Correct |
50 ms |
62968 KB |
Output is correct |
24 |
Correct |
50 ms |
63068 KB |
Output is correct |
25 |
Correct |
49 ms |
62968 KB |
Output is correct |
26 |
Correct |
199 ms |
70652 KB |
Output is correct |
27 |
Correct |
240 ms |
70236 KB |
Output is correct |
28 |
Correct |
53 ms |
63224 KB |
Output is correct |
29 |
Correct |
50 ms |
62968 KB |
Output is correct |
30 |
Correct |
49 ms |
62968 KB |
Output is correct |
31 |
Correct |
246 ms |
70264 KB |
Output is correct |
32 |
Correct |
57 ms |
63224 KB |
Output is correct |
33 |
Correct |
268 ms |
78712 KB |
Output is correct |
34 |
Correct |
242 ms |
70264 KB |
Output is correct |
35 |
Correct |
53 ms |
63224 KB |
Output is correct |
36 |
Correct |
250 ms |
71160 KB |
Output is correct |
37 |
Correct |
56 ms |
63224 KB |
Output is correct |
38 |
Correct |
53 ms |
63224 KB |
Output is correct |
39 |
Correct |
215 ms |
70384 KB |
Output is correct |
40 |
Correct |
52 ms |
63480 KB |
Output is correct |
41 |
Correct |
237 ms |
70104 KB |
Output is correct |
42 |
Correct |
254 ms |
72996 KB |
Output is correct |
43 |
Correct |
50 ms |
62968 KB |
Output is correct |
44 |
Correct |
263 ms |
79032 KB |
Output is correct |
45 |
Correct |
274 ms |
74820 KB |
Output is correct |
46 |
Correct |
52 ms |
63224 KB |
Output is correct |
47 |
Correct |
53 ms |
63352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
70512 KB |
Output is correct |
2 |
Correct |
219 ms |
75760 KB |
Output is correct |
3 |
Correct |
53 ms |
63224 KB |
Output is correct |
4 |
Correct |
51 ms |
63224 KB |
Output is correct |
5 |
Correct |
49 ms |
62972 KB |
Output is correct |
6 |
Correct |
48 ms |
62916 KB |
Output is correct |
7 |
Correct |
51 ms |
63176 KB |
Output is correct |
8 |
Correct |
266 ms |
72312 KB |
Output is correct |
9 |
Correct |
53 ms |
63224 KB |
Output is correct |
10 |
Correct |
237 ms |
70664 KB |
Output is correct |
11 |
Correct |
51 ms |
62968 KB |
Output is correct |
12 |
Correct |
248 ms |
70744 KB |
Output is correct |
13 |
Correct |
271 ms |
72356 KB |
Output is correct |
14 |
Correct |
258 ms |
75756 KB |
Output is correct |
15 |
Correct |
199 ms |
70384 KB |
Output is correct |
16 |
Correct |
53 ms |
63352 KB |
Output is correct |
17 |
Correct |
50 ms |
62968 KB |
Output is correct |
18 |
Correct |
232 ms |
75620 KB |
Output is correct |
19 |
Correct |
295 ms |
80660 KB |
Output is correct |
20 |
Correct |
54 ms |
63224 KB |
Output is correct |
21 |
Correct |
50 ms |
62968 KB |
Output is correct |
22 |
Correct |
230 ms |
72656 KB |
Output is correct |
23 |
Correct |
56 ms |
63224 KB |
Output is correct |
24 |
Correct |
262 ms |
71544 KB |
Output is correct |
25 |
Correct |
302 ms |
78956 KB |
Output is correct |
26 |
Correct |
54 ms |
63480 KB |
Output is correct |
27 |
Correct |
53 ms |
63480 KB |
Output is correct |
28 |
Correct |
54 ms |
63224 KB |
Output is correct |
29 |
Correct |
52 ms |
63224 KB |
Output is correct |
30 |
Correct |
53 ms |
63352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
62968 KB |
Output is correct |
2 |
Correct |
49 ms |
62968 KB |
Output is correct |
3 |
Correct |
49 ms |
62968 KB |
Output is correct |
4 |
Correct |
49 ms |
62968 KB |
Output is correct |
5 |
Correct |
48 ms |
62940 KB |
Output is correct |
6 |
Correct |
48 ms |
62968 KB |
Output is correct |
7 |
Correct |
49 ms |
62968 KB |
Output is correct |
8 |
Correct |
51 ms |
62968 KB |
Output is correct |
9 |
Correct |
48 ms |
62968 KB |
Output is correct |
10 |
Correct |
49 ms |
62972 KB |
Output is correct |
11 |
Correct |
49 ms |
62968 KB |
Output is correct |
12 |
Correct |
50 ms |
62968 KB |
Output is correct |
13 |
Correct |
50 ms |
63096 KB |
Output is correct |
14 |
Correct |
48 ms |
62968 KB |
Output is correct |
15 |
Correct |
49 ms |
62968 KB |
Output is correct |
16 |
Correct |
49 ms |
63016 KB |
Output is correct |
17 |
Correct |
50 ms |
62944 KB |
Output is correct |
18 |
Correct |
59 ms |
62968 KB |
Output is correct |
19 |
Correct |
51 ms |
62968 KB |
Output is correct |
20 |
Correct |
50 ms |
62968 KB |
Output is correct |
21 |
Correct |
48 ms |
62968 KB |
Output is correct |
22 |
Correct |
49 ms |
62968 KB |
Output is correct |
23 |
Correct |
50 ms |
62968 KB |
Output is correct |
24 |
Correct |
50 ms |
63068 KB |
Output is correct |
25 |
Correct |
49 ms |
62968 KB |
Output is correct |
26 |
Correct |
53 ms |
63324 KB |
Output is correct |
27 |
Correct |
52 ms |
63224 KB |
Output is correct |
28 |
Correct |
53 ms |
63480 KB |
Output is correct |
29 |
Correct |
54 ms |
63352 KB |
Output is correct |
30 |
Correct |
54 ms |
63352 KB |
Output is correct |
31 |
Correct |
50 ms |
62968 KB |
Output is correct |
32 |
Correct |
55 ms |
63480 KB |
Output is correct |
33 |
Correct |
49 ms |
62972 KB |
Output is correct |
34 |
Correct |
52 ms |
63224 KB |
Output is correct |
35 |
Correct |
52 ms |
63352 KB |
Output is correct |
36 |
Correct |
53 ms |
63224 KB |
Output is correct |
37 |
Correct |
53 ms |
63224 KB |
Output is correct |
38 |
Correct |
50 ms |
63000 KB |
Output is correct |
39 |
Correct |
52 ms |
63228 KB |
Output is correct |
40 |
Correct |
52 ms |
63224 KB |
Output is correct |
41 |
Correct |
54 ms |
63224 KB |
Output is correct |
42 |
Correct |
52 ms |
63224 KB |
Output is correct |
43 |
Correct |
54 ms |
63480 KB |
Output is correct |
44 |
Correct |
49 ms |
62968 KB |
Output is correct |
45 |
Correct |
53 ms |
63480 KB |
Output is correct |
46 |
Correct |
55 ms |
63352 KB |
Output is correct |
47 |
Correct |
49 ms |
62968 KB |
Output is correct |
48 |
Correct |
51 ms |
63224 KB |
Output is correct |
49 |
Correct |
52 ms |
63352 KB |
Output is correct |
50 |
Correct |
53 ms |
63456 KB |
Output is correct |
51 |
Correct |
52 ms |
63224 KB |
Output is correct |
52 |
Correct |
55 ms |
63224 KB |
Output is correct |
53 |
Correct |
52 ms |
63352 KB |
Output is correct |
54 |
Correct |
51 ms |
63224 KB |
Output is correct |
55 |
Correct |
49 ms |
62968 KB |
Output is correct |
56 |
Correct |
199 ms |
70652 KB |
Output is correct |
57 |
Correct |
240 ms |
70236 KB |
Output is correct |
58 |
Correct |
53 ms |
63224 KB |
Output is correct |
59 |
Correct |
50 ms |
62968 KB |
Output is correct |
60 |
Correct |
49 ms |
62968 KB |
Output is correct |
61 |
Correct |
246 ms |
70264 KB |
Output is correct |
62 |
Correct |
57 ms |
63224 KB |
Output is correct |
63 |
Correct |
268 ms |
78712 KB |
Output is correct |
64 |
Correct |
242 ms |
70264 KB |
Output is correct |
65 |
Correct |
53 ms |
63224 KB |
Output is correct |
66 |
Correct |
250 ms |
71160 KB |
Output is correct |
67 |
Correct |
56 ms |
63224 KB |
Output is correct |
68 |
Correct |
53 ms |
63224 KB |
Output is correct |
69 |
Correct |
215 ms |
70384 KB |
Output is correct |
70 |
Correct |
52 ms |
63480 KB |
Output is correct |
71 |
Correct |
237 ms |
70104 KB |
Output is correct |
72 |
Correct |
254 ms |
72996 KB |
Output is correct |
73 |
Correct |
50 ms |
62968 KB |
Output is correct |
74 |
Correct |
263 ms |
79032 KB |
Output is correct |
75 |
Correct |
274 ms |
74820 KB |
Output is correct |
76 |
Correct |
52 ms |
63224 KB |
Output is correct |
77 |
Correct |
53 ms |
63352 KB |
Output is correct |
78 |
Correct |
204 ms |
70512 KB |
Output is correct |
79 |
Correct |
219 ms |
75760 KB |
Output is correct |
80 |
Correct |
53 ms |
63224 KB |
Output is correct |
81 |
Correct |
51 ms |
63224 KB |
Output is correct |
82 |
Correct |
49 ms |
62972 KB |
Output is correct |
83 |
Correct |
48 ms |
62916 KB |
Output is correct |
84 |
Correct |
51 ms |
63176 KB |
Output is correct |
85 |
Correct |
266 ms |
72312 KB |
Output is correct |
86 |
Correct |
53 ms |
63224 KB |
Output is correct |
87 |
Correct |
237 ms |
70664 KB |
Output is correct |
88 |
Correct |
51 ms |
62968 KB |
Output is correct |
89 |
Correct |
248 ms |
70744 KB |
Output is correct |
90 |
Correct |
271 ms |
72356 KB |
Output is correct |
91 |
Correct |
258 ms |
75756 KB |
Output is correct |
92 |
Correct |
199 ms |
70384 KB |
Output is correct |
93 |
Correct |
53 ms |
63352 KB |
Output is correct |
94 |
Correct |
50 ms |
62968 KB |
Output is correct |
95 |
Correct |
232 ms |
75620 KB |
Output is correct |
96 |
Correct |
295 ms |
80660 KB |
Output is correct |
97 |
Correct |
54 ms |
63224 KB |
Output is correct |
98 |
Correct |
50 ms |
62968 KB |
Output is correct |
99 |
Correct |
230 ms |
72656 KB |
Output is correct |
100 |
Correct |
56 ms |
63224 KB |
Output is correct |
101 |
Correct |
262 ms |
71544 KB |
Output is correct |
102 |
Correct |
302 ms |
78956 KB |
Output is correct |
103 |
Correct |
54 ms |
63480 KB |
Output is correct |
104 |
Correct |
53 ms |
63480 KB |
Output is correct |
105 |
Correct |
54 ms |
63224 KB |
Output is correct |
106 |
Correct |
52 ms |
63224 KB |
Output is correct |
107 |
Correct |
53 ms |
63352 KB |
Output is correct |
108 |
Correct |
1352 ms |
110200 KB |
Output is correct |
109 |
Correct |
1611 ms |
120528 KB |
Output is correct |
110 |
Correct |
1777 ms |
134036 KB |
Output is correct |
111 |
Correct |
1925 ms |
154336 KB |
Output is correct |
112 |
Correct |
1899 ms |
140964 KB |
Output is correct |
113 |
Correct |
1270 ms |
126060 KB |
Output is correct |
114 |
Correct |
1413 ms |
98784 KB |
Output is correct |
115 |
Correct |
1437 ms |
98688 KB |
Output is correct |
116 |
Correct |
1732 ms |
105196 KB |
Output is correct |
117 |
Correct |
1485 ms |
126816 KB |
Output is correct |
118 |
Correct |
1469 ms |
101352 KB |
Output is correct |
119 |
Correct |
1463 ms |
126684 KB |
Output is correct |
120 |
Correct |
1834 ms |
143516 KB |
Output is correct |
121 |
Correct |
1494 ms |
126824 KB |
Output is correct |
122 |
Correct |
1647 ms |
112440 KB |
Output is correct |
123 |
Correct |
1220 ms |
126300 KB |
Output is correct |
124 |
Correct |
1674 ms |
124268 KB |
Output is correct |