#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define em emplace
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int MAX = 505050;
const int INF = 1e9;
const ll LINF = 1e18;
vector <int> g[MAX], s[MAX], ng[MAX];
vector <pii> E;
int num[MAX], low[MAX], cnt = 0, p[MAX], ans = 0;
int find(int x) {
return p[x] == x ? x : p[x] = find(p[x]);
}
void uni(int x, int y) {
x = find(x), y = find(y);
if(x > y) swap(x, y);
p[y] = x;
}
void dfs(int x, int pa) {
low[x] = num[x] = ++cnt;
bool flag = false;
for(auto i : g[x]) {
if(i == pa && !flag) {
flag = true;
continue;
}
if(num[i] != 0) low[x] = min(low[x], num[i]);
else if(num[i] == 0) {
dfs(i, x);
low[x] = min(low[x], low[i]);
}
}
for(auto i : g[x]) {
if(i == pa) continue;
if(low[i] > num[x]) E.eb(i, x);
else uni(i, x);
}
}
void ndfs(int x, int pa) {
int tcnt = 0;
for(auto i : ng[x]) {
if(i == pa) continue;
ndfs(i, x);
tcnt++;
}
if(tcnt == 0 && x != 1) ans++;
if(tcnt == 1 && x == 1) ans++;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, k;
cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].eb(v), g[v].eb(u);
}
for(int i = 1; i <= n; i++) {
p[i] = i;
int u; cin >> u;
s[u].eb(i);
}
for(int i = 1; i <= k; i++) {
for(int j = 1; j < s[i].size(); j++) {
g[s[i][j-1]].eb(s[i][j]);
g[s[i][j]].eb(s[i][j-1]);
}
}
dfs(1, 0);
for(auto i : E) {
ng[find(i.fi)].eb(find(i.se));
ng[find(i.se)].eb(find(i.fi));
}
ndfs(1, 0);
cout << (ans + 1) / 2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:78:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; j < s[i].size(); j++) {
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35960 KB |
Output is correct |
2 |
Correct |
37 ms |
35900 KB |
Output is correct |
3 |
Correct |
36 ms |
35932 KB |
Output is correct |
4 |
Correct |
36 ms |
35960 KB |
Output is correct |
5 |
Correct |
35 ms |
35896 KB |
Output is correct |
6 |
Correct |
36 ms |
35968 KB |
Output is correct |
7 |
Correct |
35 ms |
35960 KB |
Output is correct |
8 |
Correct |
34 ms |
35924 KB |
Output is correct |
9 |
Correct |
34 ms |
35960 KB |
Output is correct |
10 |
Correct |
34 ms |
35960 KB |
Output is correct |
11 |
Correct |
35 ms |
35960 KB |
Output is correct |
12 |
Correct |
34 ms |
35932 KB |
Output is correct |
13 |
Correct |
34 ms |
35960 KB |
Output is correct |
14 |
Correct |
34 ms |
36088 KB |
Output is correct |
15 |
Correct |
34 ms |
35904 KB |
Output is correct |
16 |
Correct |
36 ms |
35960 KB |
Output is correct |
17 |
Correct |
36 ms |
35940 KB |
Output is correct |
18 |
Correct |
35 ms |
35960 KB |
Output is correct |
19 |
Correct |
35 ms |
35960 KB |
Output is correct |
20 |
Correct |
35 ms |
35960 KB |
Output is correct |
21 |
Correct |
35 ms |
35932 KB |
Output is correct |
22 |
Correct |
34 ms |
35960 KB |
Output is correct |
23 |
Correct |
35 ms |
35960 KB |
Output is correct |
24 |
Correct |
34 ms |
35960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35960 KB |
Output is correct |
2 |
Correct |
37 ms |
35900 KB |
Output is correct |
3 |
Correct |
36 ms |
35932 KB |
Output is correct |
4 |
Correct |
36 ms |
35960 KB |
Output is correct |
5 |
Correct |
35 ms |
35896 KB |
Output is correct |
6 |
Correct |
36 ms |
35968 KB |
Output is correct |
7 |
Correct |
35 ms |
35960 KB |
Output is correct |
8 |
Correct |
34 ms |
35924 KB |
Output is correct |
9 |
Correct |
34 ms |
35960 KB |
Output is correct |
10 |
Correct |
34 ms |
35960 KB |
Output is correct |
11 |
Correct |
35 ms |
35960 KB |
Output is correct |
12 |
Correct |
34 ms |
35932 KB |
Output is correct |
13 |
Correct |
34 ms |
35960 KB |
Output is correct |
14 |
Correct |
34 ms |
36088 KB |
Output is correct |
15 |
Correct |
34 ms |
35904 KB |
Output is correct |
16 |
Correct |
36 ms |
35960 KB |
Output is correct |
17 |
Correct |
36 ms |
35940 KB |
Output is correct |
18 |
Correct |
35 ms |
35960 KB |
Output is correct |
19 |
Correct |
35 ms |
35960 KB |
Output is correct |
20 |
Correct |
35 ms |
35960 KB |
Output is correct |
21 |
Correct |
35 ms |
35932 KB |
Output is correct |
22 |
Correct |
34 ms |
35960 KB |
Output is correct |
23 |
Correct |
35 ms |
35960 KB |
Output is correct |
24 |
Correct |
34 ms |
35960 KB |
Output is correct |
25 |
Correct |
34 ms |
36088 KB |
Output is correct |
26 |
Correct |
36 ms |
36216 KB |
Output is correct |
27 |
Correct |
38 ms |
36088 KB |
Output is correct |
28 |
Correct |
39 ms |
36444 KB |
Output is correct |
29 |
Correct |
39 ms |
36380 KB |
Output is correct |
30 |
Correct |
41 ms |
36316 KB |
Output is correct |
31 |
Correct |
35 ms |
35960 KB |
Output is correct |
32 |
Correct |
36 ms |
36472 KB |
Output is correct |
33 |
Correct |
35 ms |
35912 KB |
Output is correct |
34 |
Correct |
35 ms |
36088 KB |
Output is correct |
35 |
Correct |
37 ms |
36348 KB |
Output is correct |
36 |
Correct |
36 ms |
36216 KB |
Output is correct |
37 |
Correct |
33 ms |
36216 KB |
Output is correct |
38 |
Correct |
34 ms |
35940 KB |
Output is correct |
39 |
Correct |
37 ms |
36344 KB |
Output is correct |
40 |
Correct |
38 ms |
36340 KB |
Output is correct |
41 |
Correct |
38 ms |
36216 KB |
Output is correct |
42 |
Correct |
38 ms |
36208 KB |
Output is correct |
43 |
Correct |
37 ms |
36344 KB |
Output is correct |
44 |
Correct |
35 ms |
35916 KB |
Output is correct |
45 |
Correct |
38 ms |
36216 KB |
Output is correct |
46 |
Correct |
36 ms |
36216 KB |
Output is correct |
47 |
Correct |
34 ms |
35960 KB |
Output is correct |
48 |
Correct |
37 ms |
36216 KB |
Output is correct |
49 |
Correct |
36 ms |
36344 KB |
Output is correct |
50 |
Correct |
36 ms |
36348 KB |
Output is correct |
51 |
Correct |
39 ms |
36132 KB |
Output is correct |
52 |
Correct |
37 ms |
36088 KB |
Output is correct |
53 |
Correct |
38 ms |
36188 KB |
Output is correct |
54 |
Correct |
39 ms |
36348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35960 KB |
Output is correct |
2 |
Correct |
37 ms |
35900 KB |
Output is correct |
3 |
Correct |
36 ms |
35932 KB |
Output is correct |
4 |
Correct |
36 ms |
35960 KB |
Output is correct |
5 |
Correct |
35 ms |
35896 KB |
Output is correct |
6 |
Correct |
36 ms |
35968 KB |
Output is correct |
7 |
Correct |
35 ms |
35960 KB |
Output is correct |
8 |
Correct |
34 ms |
35924 KB |
Output is correct |
9 |
Correct |
34 ms |
35960 KB |
Output is correct |
10 |
Correct |
34 ms |
35960 KB |
Output is correct |
11 |
Correct |
35 ms |
35960 KB |
Output is correct |
12 |
Correct |
34 ms |
35932 KB |
Output is correct |
13 |
Correct |
34 ms |
35960 KB |
Output is correct |
14 |
Correct |
34 ms |
36088 KB |
Output is correct |
15 |
Correct |
34 ms |
35904 KB |
Output is correct |
16 |
Correct |
36 ms |
35960 KB |
Output is correct |
17 |
Correct |
36 ms |
35940 KB |
Output is correct |
18 |
Correct |
35 ms |
35960 KB |
Output is correct |
19 |
Correct |
35 ms |
35960 KB |
Output is correct |
20 |
Correct |
35 ms |
35960 KB |
Output is correct |
21 |
Correct |
35 ms |
35932 KB |
Output is correct |
22 |
Correct |
34 ms |
35960 KB |
Output is correct |
23 |
Correct |
35 ms |
35960 KB |
Output is correct |
24 |
Correct |
34 ms |
35960 KB |
Output is correct |
25 |
Correct |
34 ms |
35960 KB |
Output is correct |
26 |
Correct |
116 ms |
42864 KB |
Output is correct |
27 |
Correct |
153 ms |
49420 KB |
Output is correct |
28 |
Correct |
37 ms |
36216 KB |
Output is correct |
29 |
Correct |
34 ms |
35960 KB |
Output is correct |
30 |
Correct |
35 ms |
35960 KB |
Output is correct |
31 |
Correct |
145 ms |
44012 KB |
Output is correct |
32 |
Correct |
37 ms |
36088 KB |
Output is correct |
33 |
Correct |
127 ms |
51576 KB |
Output is correct |
34 |
Correct |
147 ms |
48248 KB |
Output is correct |
35 |
Correct |
36 ms |
36216 KB |
Output is correct |
36 |
Correct |
130 ms |
43592 KB |
Output is correct |
37 |
Correct |
37 ms |
36304 KB |
Output is correct |
38 |
Correct |
37 ms |
36164 KB |
Output is correct |
39 |
Correct |
122 ms |
42736 KB |
Output is correct |
40 |
Correct |
37 ms |
36344 KB |
Output is correct |
41 |
Correct |
150 ms |
49220 KB |
Output is correct |
42 |
Correct |
160 ms |
51480 KB |
Output is correct |
43 |
Correct |
35 ms |
35960 KB |
Output is correct |
44 |
Correct |
146 ms |
51508 KB |
Output is correct |
45 |
Correct |
163 ms |
50428 KB |
Output is correct |
46 |
Correct |
37 ms |
36088 KB |
Output is correct |
47 |
Correct |
37 ms |
36344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
42804 KB |
Output is correct |
2 |
Correct |
129 ms |
49748 KB |
Output is correct |
3 |
Correct |
39 ms |
36392 KB |
Output is correct |
4 |
Correct |
36 ms |
36192 KB |
Output is correct |
5 |
Correct |
35 ms |
35992 KB |
Output is correct |
6 |
Correct |
35 ms |
36032 KB |
Output is correct |
7 |
Correct |
37 ms |
36192 KB |
Output is correct |
8 |
Correct |
145 ms |
44280 KB |
Output is correct |
9 |
Correct |
37 ms |
36216 KB |
Output is correct |
10 |
Correct |
145 ms |
43256 KB |
Output is correct |
11 |
Correct |
34 ms |
35960 KB |
Output is correct |
12 |
Correct |
136 ms |
42616 KB |
Output is correct |
13 |
Correct |
148 ms |
44536 KB |
Output is correct |
14 |
Correct |
161 ms |
49304 KB |
Output is correct |
15 |
Correct |
122 ms |
42808 KB |
Output is correct |
16 |
Correct |
37 ms |
36120 KB |
Output is correct |
17 |
Correct |
35 ms |
35960 KB |
Output is correct |
18 |
Correct |
129 ms |
48608 KB |
Output is correct |
19 |
Correct |
162 ms |
53868 KB |
Output is correct |
20 |
Correct |
36 ms |
36220 KB |
Output is correct |
21 |
Correct |
34 ms |
35960 KB |
Output is correct |
22 |
Correct |
130 ms |
44700 KB |
Output is correct |
23 |
Correct |
37 ms |
36224 KB |
Output is correct |
24 |
Correct |
146 ms |
43516 KB |
Output is correct |
25 |
Correct |
159 ms |
52204 KB |
Output is correct |
26 |
Correct |
37 ms |
36344 KB |
Output is correct |
27 |
Correct |
37 ms |
36344 KB |
Output is correct |
28 |
Correct |
36 ms |
36128 KB |
Output is correct |
29 |
Correct |
35 ms |
36088 KB |
Output is correct |
30 |
Correct |
36 ms |
36216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
35960 KB |
Output is correct |
2 |
Correct |
37 ms |
35900 KB |
Output is correct |
3 |
Correct |
36 ms |
35932 KB |
Output is correct |
4 |
Correct |
36 ms |
35960 KB |
Output is correct |
5 |
Correct |
35 ms |
35896 KB |
Output is correct |
6 |
Correct |
36 ms |
35968 KB |
Output is correct |
7 |
Correct |
35 ms |
35960 KB |
Output is correct |
8 |
Correct |
34 ms |
35924 KB |
Output is correct |
9 |
Correct |
34 ms |
35960 KB |
Output is correct |
10 |
Correct |
34 ms |
35960 KB |
Output is correct |
11 |
Correct |
35 ms |
35960 KB |
Output is correct |
12 |
Correct |
34 ms |
35932 KB |
Output is correct |
13 |
Correct |
34 ms |
35960 KB |
Output is correct |
14 |
Correct |
34 ms |
36088 KB |
Output is correct |
15 |
Correct |
34 ms |
35904 KB |
Output is correct |
16 |
Correct |
36 ms |
35960 KB |
Output is correct |
17 |
Correct |
36 ms |
35940 KB |
Output is correct |
18 |
Correct |
35 ms |
35960 KB |
Output is correct |
19 |
Correct |
35 ms |
35960 KB |
Output is correct |
20 |
Correct |
35 ms |
35960 KB |
Output is correct |
21 |
Correct |
35 ms |
35932 KB |
Output is correct |
22 |
Correct |
34 ms |
35960 KB |
Output is correct |
23 |
Correct |
35 ms |
35960 KB |
Output is correct |
24 |
Correct |
34 ms |
35960 KB |
Output is correct |
25 |
Correct |
34 ms |
36088 KB |
Output is correct |
26 |
Correct |
36 ms |
36216 KB |
Output is correct |
27 |
Correct |
38 ms |
36088 KB |
Output is correct |
28 |
Correct |
39 ms |
36444 KB |
Output is correct |
29 |
Correct |
39 ms |
36380 KB |
Output is correct |
30 |
Correct |
41 ms |
36316 KB |
Output is correct |
31 |
Correct |
35 ms |
35960 KB |
Output is correct |
32 |
Correct |
36 ms |
36472 KB |
Output is correct |
33 |
Correct |
35 ms |
35912 KB |
Output is correct |
34 |
Correct |
35 ms |
36088 KB |
Output is correct |
35 |
Correct |
37 ms |
36348 KB |
Output is correct |
36 |
Correct |
36 ms |
36216 KB |
Output is correct |
37 |
Correct |
33 ms |
36216 KB |
Output is correct |
38 |
Correct |
34 ms |
35940 KB |
Output is correct |
39 |
Correct |
37 ms |
36344 KB |
Output is correct |
40 |
Correct |
38 ms |
36340 KB |
Output is correct |
41 |
Correct |
38 ms |
36216 KB |
Output is correct |
42 |
Correct |
38 ms |
36208 KB |
Output is correct |
43 |
Correct |
37 ms |
36344 KB |
Output is correct |
44 |
Correct |
35 ms |
35916 KB |
Output is correct |
45 |
Correct |
38 ms |
36216 KB |
Output is correct |
46 |
Correct |
36 ms |
36216 KB |
Output is correct |
47 |
Correct |
34 ms |
35960 KB |
Output is correct |
48 |
Correct |
37 ms |
36216 KB |
Output is correct |
49 |
Correct |
36 ms |
36344 KB |
Output is correct |
50 |
Correct |
36 ms |
36348 KB |
Output is correct |
51 |
Correct |
39 ms |
36132 KB |
Output is correct |
52 |
Correct |
37 ms |
36088 KB |
Output is correct |
53 |
Correct |
38 ms |
36188 KB |
Output is correct |
54 |
Correct |
39 ms |
36348 KB |
Output is correct |
55 |
Correct |
34 ms |
35960 KB |
Output is correct |
56 |
Correct |
116 ms |
42864 KB |
Output is correct |
57 |
Correct |
153 ms |
49420 KB |
Output is correct |
58 |
Correct |
37 ms |
36216 KB |
Output is correct |
59 |
Correct |
34 ms |
35960 KB |
Output is correct |
60 |
Correct |
35 ms |
35960 KB |
Output is correct |
61 |
Correct |
145 ms |
44012 KB |
Output is correct |
62 |
Correct |
37 ms |
36088 KB |
Output is correct |
63 |
Correct |
127 ms |
51576 KB |
Output is correct |
64 |
Correct |
147 ms |
48248 KB |
Output is correct |
65 |
Correct |
36 ms |
36216 KB |
Output is correct |
66 |
Correct |
130 ms |
43592 KB |
Output is correct |
67 |
Correct |
37 ms |
36304 KB |
Output is correct |
68 |
Correct |
37 ms |
36164 KB |
Output is correct |
69 |
Correct |
122 ms |
42736 KB |
Output is correct |
70 |
Correct |
37 ms |
36344 KB |
Output is correct |
71 |
Correct |
150 ms |
49220 KB |
Output is correct |
72 |
Correct |
160 ms |
51480 KB |
Output is correct |
73 |
Correct |
35 ms |
35960 KB |
Output is correct |
74 |
Correct |
146 ms |
51508 KB |
Output is correct |
75 |
Correct |
163 ms |
50428 KB |
Output is correct |
76 |
Correct |
37 ms |
36088 KB |
Output is correct |
77 |
Correct |
37 ms |
36344 KB |
Output is correct |
78 |
Correct |
117 ms |
42804 KB |
Output is correct |
79 |
Correct |
129 ms |
49748 KB |
Output is correct |
80 |
Correct |
39 ms |
36392 KB |
Output is correct |
81 |
Correct |
36 ms |
36192 KB |
Output is correct |
82 |
Correct |
35 ms |
35992 KB |
Output is correct |
83 |
Correct |
35 ms |
36032 KB |
Output is correct |
84 |
Correct |
37 ms |
36192 KB |
Output is correct |
85 |
Correct |
145 ms |
44280 KB |
Output is correct |
86 |
Correct |
37 ms |
36216 KB |
Output is correct |
87 |
Correct |
145 ms |
43256 KB |
Output is correct |
88 |
Correct |
34 ms |
35960 KB |
Output is correct |
89 |
Correct |
136 ms |
42616 KB |
Output is correct |
90 |
Correct |
148 ms |
44536 KB |
Output is correct |
91 |
Correct |
161 ms |
49304 KB |
Output is correct |
92 |
Correct |
122 ms |
42808 KB |
Output is correct |
93 |
Correct |
37 ms |
36120 KB |
Output is correct |
94 |
Correct |
35 ms |
35960 KB |
Output is correct |
95 |
Correct |
129 ms |
48608 KB |
Output is correct |
96 |
Correct |
162 ms |
53868 KB |
Output is correct |
97 |
Correct |
36 ms |
36220 KB |
Output is correct |
98 |
Correct |
34 ms |
35960 KB |
Output is correct |
99 |
Correct |
130 ms |
44700 KB |
Output is correct |
100 |
Correct |
37 ms |
36224 KB |
Output is correct |
101 |
Correct |
146 ms |
43516 KB |
Output is correct |
102 |
Correct |
159 ms |
52204 KB |
Output is correct |
103 |
Correct |
37 ms |
36344 KB |
Output is correct |
104 |
Correct |
37 ms |
36344 KB |
Output is correct |
105 |
Correct |
36 ms |
36128 KB |
Output is correct |
106 |
Correct |
35 ms |
36088 KB |
Output is correct |
107 |
Correct |
36 ms |
36216 KB |
Output is correct |
108 |
Correct |
740 ms |
77732 KB |
Output is correct |
109 |
Correct |
851 ms |
113480 KB |
Output is correct |
110 |
Correct |
849 ms |
114112 KB |
Output is correct |
111 |
Correct |
1080 ms |
129284 KB |
Output is correct |
112 |
Correct |
913 ms |
108104 KB |
Output is correct |
113 |
Correct |
810 ms |
100796 KB |
Output is correct |
114 |
Correct |
823 ms |
97320 KB |
Output is correct |
115 |
Correct |
823 ms |
103524 KB |
Output is correct |
116 |
Correct |
940 ms |
77824 KB |
Output is correct |
117 |
Correct |
946 ms |
103864 KB |
Output is correct |
118 |
Correct |
827 ms |
73352 KB |
Output is correct |
119 |
Correct |
1077 ms |
103832 KB |
Output is correct |
120 |
Correct |
1018 ms |
119868 KB |
Output is correct |
121 |
Correct |
952 ms |
103888 KB |
Output is correct |
122 |
Correct |
1119 ms |
102664 KB |
Output is correct |
123 |
Correct |
727 ms |
106648 KB |
Output is correct |
124 |
Correct |
930 ms |
95296 KB |
Output is correct |