#include <bits/stdc++.h>
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
const ll mod = 1e9 + 7;
vector<vector<int>> g;
vector<int> rt, low;
vector<bool> vis;
void dfs(int i, int p, int d) {
vis[i] = true, low[i] = d;
bool b = false;
for (int j : g[i]) {
if (j == p && !b) {
b = true;
continue;
}
if (!vis[j]) dfs(j, i, d + 1);
low[i] = min(low[i], low[j]);
}
rt[i] = low[i] < d ? p : i;
}
int find(int i) {
return rt[i] = rt[i] == i ? i : find(rt[i]);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, k;
cin >> n >> k;
g.resize(n), rt.resize(n), low.resize(n), vis.assign(n, false);
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y), g[y].push_back(x);
}
vector<int> last(k, -1);
for (int i = 0, x; i < n; ++i) {
cin >> x, --x;
if (~last[x])
g[i].push_back(last[x]), g[last[x]].push_back(i);
last[x] = i;
}
dfs(0, 0, 0);
for (int i = 0; i < n; ++i) find(i);
for (int i = 0; i < n; ++i) {
for (int &j : g[i]) j = rt[j];
if (rt[i] != i) for (int j : g[i]) g[rt[i]].push_back(j);
}
int lf = 0;
for (int i = 0; i < n; ++i) if (rt[i] == i) {
sort(all(g[i])), g[i].resize(unique(all(g[i])) - g[i].begin());
for (int &j : g[i]) if (j == i) {
swap(j, g[i].back());
g[i].pop_back();
break;
}
if (g[i].size() == 1)
++lf;
}
if (lf == 0) return cout << 0, 0;
cout << (lf + 1 >> 1);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:63:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | cout << (lf + 1 >> 1);
| ~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
3 ms |
660 KB |
Output is correct |
27 |
Correct |
3 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
748 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
3 ms |
876 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
2 ms |
876 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
2 ms |
748 KB |
Output is correct |
35 |
Correct |
2 ms |
620 KB |
Output is correct |
36 |
Correct |
3 ms |
748 KB |
Output is correct |
37 |
Correct |
3 ms |
748 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
3 ms |
896 KB |
Output is correct |
40 |
Correct |
3 ms |
876 KB |
Output is correct |
41 |
Correct |
3 ms |
748 KB |
Output is correct |
42 |
Correct |
2 ms |
744 KB |
Output is correct |
43 |
Correct |
3 ms |
1004 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
2 ms |
748 KB |
Output is correct |
46 |
Correct |
2 ms |
620 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
3 ms |
748 KB |
Output is correct |
49 |
Correct |
2 ms |
620 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
3 ms |
620 KB |
Output is correct |
52 |
Correct |
2 ms |
748 KB |
Output is correct |
53 |
Correct |
3 ms |
748 KB |
Output is correct |
54 |
Correct |
3 ms |
876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
87 ms |
10852 KB |
Output is correct |
27 |
Correct |
124 ms |
17632 KB |
Output is correct |
28 |
Correct |
3 ms |
620 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
150 ms |
11756 KB |
Output is correct |
32 |
Correct |
2 ms |
748 KB |
Output is correct |
33 |
Correct |
112 ms |
19552 KB |
Output is correct |
34 |
Correct |
109 ms |
15788 KB |
Output is correct |
35 |
Correct |
3 ms |
748 KB |
Output is correct |
36 |
Correct |
103 ms |
11372 KB |
Output is correct |
37 |
Correct |
3 ms |
876 KB |
Output is correct |
38 |
Correct |
4 ms |
748 KB |
Output is correct |
39 |
Correct |
90 ms |
10764 KB |
Output is correct |
40 |
Correct |
3 ms |
1004 KB |
Output is correct |
41 |
Correct |
123 ms |
17376 KB |
Output is correct |
42 |
Correct |
115 ms |
19020 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
118 ms |
19552 KB |
Output is correct |
45 |
Correct |
119 ms |
18528 KB |
Output is correct |
46 |
Correct |
2 ms |
748 KB |
Output is correct |
47 |
Correct |
3 ms |
876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
10852 KB |
Output is correct |
2 |
Correct |
79 ms |
9188 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
3 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
748 KB |
Output is correct |
8 |
Correct |
102 ms |
11244 KB |
Output is correct |
9 |
Correct |
3 ms |
748 KB |
Output is correct |
10 |
Correct |
126 ms |
11372 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
124 ms |
10604 KB |
Output is correct |
13 |
Correct |
134 ms |
10688 KB |
Output is correct |
14 |
Correct |
84 ms |
8852 KB |
Output is correct |
15 |
Correct |
89 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
744 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
97 ms |
9952 KB |
Output is correct |
19 |
Correct |
101 ms |
13588 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
88 ms |
10980 KB |
Output is correct |
23 |
Correct |
3 ms |
748 KB |
Output is correct |
24 |
Correct |
138 ms |
11628 KB |
Output is correct |
25 |
Correct |
103 ms |
12012 KB |
Output is correct |
26 |
Correct |
2 ms |
620 KB |
Output is correct |
27 |
Correct |
2 ms |
620 KB |
Output is correct |
28 |
Correct |
4 ms |
620 KB |
Output is correct |
29 |
Correct |
3 ms |
748 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
3 ms |
660 KB |
Output is correct |
27 |
Correct |
3 ms |
620 KB |
Output is correct |
28 |
Correct |
2 ms |
748 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
3 ms |
876 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
2 ms |
876 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
2 ms |
748 KB |
Output is correct |
35 |
Correct |
2 ms |
620 KB |
Output is correct |
36 |
Correct |
3 ms |
748 KB |
Output is correct |
37 |
Correct |
3 ms |
748 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
3 ms |
896 KB |
Output is correct |
40 |
Correct |
3 ms |
876 KB |
Output is correct |
41 |
Correct |
3 ms |
748 KB |
Output is correct |
42 |
Correct |
2 ms |
744 KB |
Output is correct |
43 |
Correct |
3 ms |
1004 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
2 ms |
748 KB |
Output is correct |
46 |
Correct |
2 ms |
620 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
3 ms |
748 KB |
Output is correct |
49 |
Correct |
2 ms |
620 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
3 ms |
620 KB |
Output is correct |
52 |
Correct |
2 ms |
748 KB |
Output is correct |
53 |
Correct |
3 ms |
748 KB |
Output is correct |
54 |
Correct |
3 ms |
876 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
87 ms |
10852 KB |
Output is correct |
57 |
Correct |
124 ms |
17632 KB |
Output is correct |
58 |
Correct |
3 ms |
620 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
150 ms |
11756 KB |
Output is correct |
62 |
Correct |
2 ms |
748 KB |
Output is correct |
63 |
Correct |
112 ms |
19552 KB |
Output is correct |
64 |
Correct |
109 ms |
15788 KB |
Output is correct |
65 |
Correct |
3 ms |
748 KB |
Output is correct |
66 |
Correct |
103 ms |
11372 KB |
Output is correct |
67 |
Correct |
3 ms |
876 KB |
Output is correct |
68 |
Correct |
4 ms |
748 KB |
Output is correct |
69 |
Correct |
90 ms |
10764 KB |
Output is correct |
70 |
Correct |
3 ms |
1004 KB |
Output is correct |
71 |
Correct |
123 ms |
17376 KB |
Output is correct |
72 |
Correct |
115 ms |
19020 KB |
Output is correct |
73 |
Correct |
1 ms |
364 KB |
Output is correct |
74 |
Correct |
118 ms |
19552 KB |
Output is correct |
75 |
Correct |
119 ms |
18528 KB |
Output is correct |
76 |
Correct |
2 ms |
748 KB |
Output is correct |
77 |
Correct |
3 ms |
876 KB |
Output is correct |
78 |
Correct |
83 ms |
10852 KB |
Output is correct |
79 |
Correct |
79 ms |
9188 KB |
Output is correct |
80 |
Correct |
3 ms |
620 KB |
Output is correct |
81 |
Correct |
3 ms |
620 KB |
Output is correct |
82 |
Correct |
1 ms |
364 KB |
Output is correct |
83 |
Correct |
1 ms |
364 KB |
Output is correct |
84 |
Correct |
2 ms |
748 KB |
Output is correct |
85 |
Correct |
102 ms |
11244 KB |
Output is correct |
86 |
Correct |
3 ms |
748 KB |
Output is correct |
87 |
Correct |
126 ms |
11372 KB |
Output is correct |
88 |
Correct |
1 ms |
364 KB |
Output is correct |
89 |
Correct |
124 ms |
10604 KB |
Output is correct |
90 |
Correct |
134 ms |
10688 KB |
Output is correct |
91 |
Correct |
84 ms |
8852 KB |
Output is correct |
92 |
Correct |
89 ms |
10844 KB |
Output is correct |
93 |
Correct |
2 ms |
744 KB |
Output is correct |
94 |
Correct |
1 ms |
364 KB |
Output is correct |
95 |
Correct |
97 ms |
9952 KB |
Output is correct |
96 |
Correct |
101 ms |
13588 KB |
Output is correct |
97 |
Correct |
3 ms |
620 KB |
Output is correct |
98 |
Correct |
1 ms |
364 KB |
Output is correct |
99 |
Correct |
88 ms |
10980 KB |
Output is correct |
100 |
Correct |
3 ms |
748 KB |
Output is correct |
101 |
Correct |
138 ms |
11628 KB |
Output is correct |
102 |
Correct |
103 ms |
12012 KB |
Output is correct |
103 |
Correct |
2 ms |
620 KB |
Output is correct |
104 |
Correct |
2 ms |
620 KB |
Output is correct |
105 |
Correct |
4 ms |
620 KB |
Output is correct |
106 |
Correct |
3 ms |
748 KB |
Output is correct |
107 |
Correct |
2 ms |
748 KB |
Output is correct |
108 |
Correct |
687 ms |
52860 KB |
Output is correct |
109 |
Correct |
803 ms |
93628 KB |
Output is correct |
110 |
Correct |
815 ms |
94552 KB |
Output is correct |
111 |
Correct |
717 ms |
72556 KB |
Output is correct |
112 |
Correct |
699 ms |
61664 KB |
Output is correct |
113 |
Correct |
546 ms |
48592 KB |
Output is correct |
114 |
Correct |
810 ms |
77732 KB |
Output is correct |
115 |
Correct |
793 ms |
84148 KB |
Output is correct |
116 |
Correct |
929 ms |
58540 KB |
Output is correct |
117 |
Correct |
637 ms |
44140 KB |
Output is correct |
118 |
Correct |
840 ms |
56212 KB |
Output is correct |
119 |
Correct |
642 ms |
44240 KB |
Output is correct |
120 |
Correct |
691 ms |
61036 KB |
Output is correct |
121 |
Correct |
693 ms |
44292 KB |
Output is correct |
122 |
Correct |
865 ms |
74176 KB |
Output is correct |
123 |
Correct |
517 ms |
45788 KB |
Output is correct |
124 |
Correct |
735 ms |
45548 KB |
Output is correct |