#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 5e5+10;
int n, k, tin[maxn], p[maxn][23], ps[maxn][23], h[maxn], ds[maxn], dsz[maxn];
int timer = 0;
vector<int> g[maxn];
vector<pair<int,int>> vec[maxn];
void dfs(int u, int ant) {
p[u][0] = ant;
tin[u] = ++timer;
for(int i = 1; i <= 20; i++) {
p[u][i] = p[p[u][i-1]][i-1];
}
for(auto v : g[u]) {
if(v == ant) continue;
h[v] = h[u]+1;
dfs(v,u);
}
}
int lca(int u, int v) {
if(h[u] < h[v]) swap(u,v);
for(int i = 20; i >= 0; i--) {
if(h[p[u][i]] >= h[v]) {
u = p[u][i];
}
}
if(u == v) return u;
for(int i = 20; i >= 0; i--) {
if(p[u][i] != p[v][i]) {
u = p[u][i];
v = p[v][i];
}
}
return p[u][0];
}
int find(int v) {
if(ds[v] == v) return v;
return ds[v] = find(ds[v]);
}
void join(int u, int v) {
if(dsz[u] < dsz[v]) swap(u,v);
ds[v] = u;
dsz[u]+= dsz[v];
}
int qtdf = 0, mark[maxn];
void dfs1(int u) {
mark[u] = 1;
set<int> st;
for(auto v : g[u]) {
v = find(v);
if(v != u) st.insert(v);
if(mark[v]) continue;
dfs1(v);
}
if(st.size() == 1) qtdf++;
}
void solve() {
cin >> n >> k;
for(int i = 1; i <= n-1; i++) {
int u,v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1,1);
for(int i = 1; i <= n; i++) {
int cl; cin >> cl;
vec[cl].pb(mp(tin[i],i));
}
for(int i = 1; i <= k; i++) {
sort(all(vec[i]));
vec[i].pb(vec[i][0]);
for(int j = 1; j < vec[i].size(); j++) {
int u = vec[i][j-1].sc;
int v = vec[i][j].sc;
int lc = lca(u,v);
for(int j = 20; j >= 0; j--) {
if(h[p[u][j]] > h[lc]) {
ps[u][j]++;
u = p[u][j];
}
}
if(u != lc) {
ps[u][0]++;
}
for(int j = 20; j >= 0; j--) {
if(h[p[v][j]] > h[lc]) {
ps[v][j]++;
v = p[v][j];
}
}
if(v != lc) {
ps[v][0]++;
}
}
}
for(int j = 20; j >= 1; j--) {
for(int i = 1; i <= n; i++) {
ps[i][j-1]+= ps[i][j];
ps[p[i][j-1]][j-1]+= ps[i][j];
}
}
for(int i = 1; i <= n; i++) {
ds[i] = i;
dsz[i] = 1;
}
int ans = n-1;
for(int i = 1; i <= n; i++) {
if(ps[i][0] != 0) {
int u = find(i);
int v = find(p[i][0]);
if(u != v) {
ans--;
join(u,v);
}
}
}
for(int i = 1; i <= n; i++) {
int v = find(i);
if(v == i) continue;
for(auto x : g[i]) {
if(x != v) g[v].pb(find(x));
}
}
dfs1(find(1));
cout << (qtdf+1)/2 << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
int t = 1;
while(tt--) {
solve();
}
}
Compilation message
mergers.cpp: In function 'void solve()':
mergers.cpp:100:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int j = 1; j < vec[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:174:9: warning: unused variable 't' [-Wunused-variable]
174 | int t = 1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23820 KB |
Output is correct |
2 |
Correct |
14 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23912 KB |
Output is correct |
4 |
Correct |
12 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23736 KB |
Output is correct |
6 |
Correct |
12 ms |
23824 KB |
Output is correct |
7 |
Correct |
12 ms |
23784 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
12 ms |
23868 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23908 KB |
Output is correct |
12 |
Correct |
11 ms |
23892 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
14 ms |
23904 KB |
Output is correct |
15 |
Correct |
13 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23816 KB |
Output is correct |
20 |
Correct |
11 ms |
23824 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23820 KB |
Output is correct |
2 |
Correct |
14 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23912 KB |
Output is correct |
4 |
Correct |
12 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23736 KB |
Output is correct |
6 |
Correct |
12 ms |
23824 KB |
Output is correct |
7 |
Correct |
12 ms |
23784 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
12 ms |
23868 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23908 KB |
Output is correct |
12 |
Correct |
11 ms |
23892 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
14 ms |
23904 KB |
Output is correct |
15 |
Correct |
13 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23816 KB |
Output is correct |
20 |
Correct |
11 ms |
23824 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
13 ms |
23892 KB |
Output is correct |
26 |
Correct |
16 ms |
25396 KB |
Output is correct |
27 |
Correct |
15 ms |
25240 KB |
Output is correct |
28 |
Correct |
17 ms |
25496 KB |
Output is correct |
29 |
Correct |
16 ms |
25360 KB |
Output is correct |
30 |
Correct |
16 ms |
25376 KB |
Output is correct |
31 |
Correct |
13 ms |
23892 KB |
Output is correct |
32 |
Correct |
16 ms |
25500 KB |
Output is correct |
33 |
Correct |
12 ms |
23892 KB |
Output is correct |
34 |
Correct |
16 ms |
25312 KB |
Output is correct |
35 |
Correct |
16 ms |
25372 KB |
Output is correct |
36 |
Correct |
20 ms |
25236 KB |
Output is correct |
37 |
Correct |
17 ms |
25428 KB |
Output is correct |
38 |
Correct |
13 ms |
23916 KB |
Output is correct |
39 |
Correct |
16 ms |
25372 KB |
Output is correct |
40 |
Correct |
15 ms |
25244 KB |
Output is correct |
41 |
Correct |
16 ms |
25372 KB |
Output is correct |
42 |
Correct |
18 ms |
25376 KB |
Output is correct |
43 |
Correct |
16 ms |
25428 KB |
Output is correct |
44 |
Correct |
12 ms |
23824 KB |
Output is correct |
45 |
Correct |
16 ms |
25420 KB |
Output is correct |
46 |
Correct |
16 ms |
25368 KB |
Output is correct |
47 |
Correct |
13 ms |
23892 KB |
Output is correct |
48 |
Correct |
17 ms |
25300 KB |
Output is correct |
49 |
Correct |
16 ms |
25440 KB |
Output is correct |
50 |
Correct |
16 ms |
25556 KB |
Output is correct |
51 |
Correct |
17 ms |
25380 KB |
Output is correct |
52 |
Correct |
16 ms |
25364 KB |
Output is correct |
53 |
Correct |
16 ms |
25392 KB |
Output is correct |
54 |
Correct |
16 ms |
25300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23820 KB |
Output is correct |
2 |
Correct |
14 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23912 KB |
Output is correct |
4 |
Correct |
12 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23736 KB |
Output is correct |
6 |
Correct |
12 ms |
23824 KB |
Output is correct |
7 |
Correct |
12 ms |
23784 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
12 ms |
23868 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23908 KB |
Output is correct |
12 |
Correct |
11 ms |
23892 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
14 ms |
23904 KB |
Output is correct |
15 |
Correct |
13 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23816 KB |
Output is correct |
20 |
Correct |
11 ms |
23824 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
12 ms |
23824 KB |
Output is correct |
26 |
Correct |
179 ms |
71720 KB |
Output is correct |
27 |
Correct |
241 ms |
71424 KB |
Output is correct |
28 |
Correct |
17 ms |
25300 KB |
Output is correct |
29 |
Correct |
13 ms |
23844 KB |
Output is correct |
30 |
Correct |
14 ms |
23892 KB |
Output is correct |
31 |
Correct |
208 ms |
70752 KB |
Output is correct |
32 |
Correct |
16 ms |
25300 KB |
Output is correct |
33 |
Correct |
278 ms |
75160 KB |
Output is correct |
34 |
Correct |
211 ms |
71040 KB |
Output is correct |
35 |
Correct |
16 ms |
25272 KB |
Output is correct |
36 |
Correct |
219 ms |
70672 KB |
Output is correct |
37 |
Correct |
15 ms |
25248 KB |
Output is correct |
38 |
Correct |
15 ms |
25300 KB |
Output is correct |
39 |
Correct |
164 ms |
71552 KB |
Output is correct |
40 |
Correct |
19 ms |
25372 KB |
Output is correct |
41 |
Correct |
204 ms |
71888 KB |
Output is correct |
42 |
Correct |
223 ms |
71748 KB |
Output is correct |
43 |
Correct |
14 ms |
23820 KB |
Output is correct |
44 |
Correct |
266 ms |
75832 KB |
Output is correct |
45 |
Correct |
249 ms |
74248 KB |
Output is correct |
46 |
Correct |
17 ms |
25400 KB |
Output is correct |
47 |
Correct |
18 ms |
25292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
71128 KB |
Output is correct |
2 |
Correct |
211 ms |
77860 KB |
Output is correct |
3 |
Correct |
16 ms |
25300 KB |
Output is correct |
4 |
Correct |
16 ms |
25252 KB |
Output is correct |
5 |
Correct |
16 ms |
23820 KB |
Output is correct |
6 |
Correct |
14 ms |
23776 KB |
Output is correct |
7 |
Correct |
17 ms |
25388 KB |
Output is correct |
8 |
Correct |
231 ms |
74132 KB |
Output is correct |
9 |
Correct |
16 ms |
25232 KB |
Output is correct |
10 |
Correct |
205 ms |
71912 KB |
Output is correct |
11 |
Correct |
16 ms |
23892 KB |
Output is correct |
12 |
Correct |
197 ms |
71416 KB |
Output is correct |
13 |
Correct |
213 ms |
74336 KB |
Output is correct |
14 |
Correct |
256 ms |
73600 KB |
Output is correct |
15 |
Correct |
159 ms |
71496 KB |
Output is correct |
16 |
Correct |
17 ms |
25472 KB |
Output is correct |
17 |
Correct |
13 ms |
23824 KB |
Output is correct |
18 |
Correct |
219 ms |
78284 KB |
Output is correct |
19 |
Correct |
254 ms |
83828 KB |
Output is correct |
20 |
Correct |
16 ms |
25300 KB |
Output is correct |
21 |
Correct |
13 ms |
23892 KB |
Output is correct |
22 |
Correct |
173 ms |
74316 KB |
Output is correct |
23 |
Correct |
18 ms |
25428 KB |
Output is correct |
24 |
Correct |
221 ms |
73348 KB |
Output is correct |
25 |
Correct |
249 ms |
82256 KB |
Output is correct |
26 |
Correct |
20 ms |
25428 KB |
Output is correct |
27 |
Correct |
17 ms |
25556 KB |
Output is correct |
28 |
Correct |
16 ms |
25316 KB |
Output is correct |
29 |
Correct |
18 ms |
25320 KB |
Output is correct |
30 |
Correct |
16 ms |
25428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23820 KB |
Output is correct |
2 |
Correct |
14 ms |
23892 KB |
Output is correct |
3 |
Correct |
13 ms |
23912 KB |
Output is correct |
4 |
Correct |
12 ms |
23820 KB |
Output is correct |
5 |
Correct |
12 ms |
23736 KB |
Output is correct |
6 |
Correct |
12 ms |
23824 KB |
Output is correct |
7 |
Correct |
12 ms |
23784 KB |
Output is correct |
8 |
Correct |
13 ms |
23892 KB |
Output is correct |
9 |
Correct |
12 ms |
23868 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23908 KB |
Output is correct |
12 |
Correct |
11 ms |
23892 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
14 ms |
23904 KB |
Output is correct |
15 |
Correct |
13 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23824 KB |
Output is correct |
19 |
Correct |
12 ms |
23816 KB |
Output is correct |
20 |
Correct |
11 ms |
23824 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
13 ms |
23892 KB |
Output is correct |
26 |
Correct |
16 ms |
25396 KB |
Output is correct |
27 |
Correct |
15 ms |
25240 KB |
Output is correct |
28 |
Correct |
17 ms |
25496 KB |
Output is correct |
29 |
Correct |
16 ms |
25360 KB |
Output is correct |
30 |
Correct |
16 ms |
25376 KB |
Output is correct |
31 |
Correct |
13 ms |
23892 KB |
Output is correct |
32 |
Correct |
16 ms |
25500 KB |
Output is correct |
33 |
Correct |
12 ms |
23892 KB |
Output is correct |
34 |
Correct |
16 ms |
25312 KB |
Output is correct |
35 |
Correct |
16 ms |
25372 KB |
Output is correct |
36 |
Correct |
20 ms |
25236 KB |
Output is correct |
37 |
Correct |
17 ms |
25428 KB |
Output is correct |
38 |
Correct |
13 ms |
23916 KB |
Output is correct |
39 |
Correct |
16 ms |
25372 KB |
Output is correct |
40 |
Correct |
15 ms |
25244 KB |
Output is correct |
41 |
Correct |
16 ms |
25372 KB |
Output is correct |
42 |
Correct |
18 ms |
25376 KB |
Output is correct |
43 |
Correct |
16 ms |
25428 KB |
Output is correct |
44 |
Correct |
12 ms |
23824 KB |
Output is correct |
45 |
Correct |
16 ms |
25420 KB |
Output is correct |
46 |
Correct |
16 ms |
25368 KB |
Output is correct |
47 |
Correct |
13 ms |
23892 KB |
Output is correct |
48 |
Correct |
17 ms |
25300 KB |
Output is correct |
49 |
Correct |
16 ms |
25440 KB |
Output is correct |
50 |
Correct |
16 ms |
25556 KB |
Output is correct |
51 |
Correct |
17 ms |
25380 KB |
Output is correct |
52 |
Correct |
16 ms |
25364 KB |
Output is correct |
53 |
Correct |
16 ms |
25392 KB |
Output is correct |
54 |
Correct |
16 ms |
25300 KB |
Output is correct |
55 |
Correct |
12 ms |
23824 KB |
Output is correct |
56 |
Correct |
179 ms |
71720 KB |
Output is correct |
57 |
Correct |
241 ms |
71424 KB |
Output is correct |
58 |
Correct |
17 ms |
25300 KB |
Output is correct |
59 |
Correct |
13 ms |
23844 KB |
Output is correct |
60 |
Correct |
14 ms |
23892 KB |
Output is correct |
61 |
Correct |
208 ms |
70752 KB |
Output is correct |
62 |
Correct |
16 ms |
25300 KB |
Output is correct |
63 |
Correct |
278 ms |
75160 KB |
Output is correct |
64 |
Correct |
211 ms |
71040 KB |
Output is correct |
65 |
Correct |
16 ms |
25272 KB |
Output is correct |
66 |
Correct |
219 ms |
70672 KB |
Output is correct |
67 |
Correct |
15 ms |
25248 KB |
Output is correct |
68 |
Correct |
15 ms |
25300 KB |
Output is correct |
69 |
Correct |
164 ms |
71552 KB |
Output is correct |
70 |
Correct |
19 ms |
25372 KB |
Output is correct |
71 |
Correct |
204 ms |
71888 KB |
Output is correct |
72 |
Correct |
223 ms |
71748 KB |
Output is correct |
73 |
Correct |
14 ms |
23820 KB |
Output is correct |
74 |
Correct |
266 ms |
75832 KB |
Output is correct |
75 |
Correct |
249 ms |
74248 KB |
Output is correct |
76 |
Correct |
17 ms |
25400 KB |
Output is correct |
77 |
Correct |
18 ms |
25292 KB |
Output is correct |
78 |
Correct |
180 ms |
71128 KB |
Output is correct |
79 |
Correct |
211 ms |
77860 KB |
Output is correct |
80 |
Correct |
16 ms |
25300 KB |
Output is correct |
81 |
Correct |
16 ms |
25252 KB |
Output is correct |
82 |
Correct |
16 ms |
23820 KB |
Output is correct |
83 |
Correct |
14 ms |
23776 KB |
Output is correct |
84 |
Correct |
17 ms |
25388 KB |
Output is correct |
85 |
Correct |
231 ms |
74132 KB |
Output is correct |
86 |
Correct |
16 ms |
25232 KB |
Output is correct |
87 |
Correct |
205 ms |
71912 KB |
Output is correct |
88 |
Correct |
16 ms |
23892 KB |
Output is correct |
89 |
Correct |
197 ms |
71416 KB |
Output is correct |
90 |
Correct |
213 ms |
74336 KB |
Output is correct |
91 |
Correct |
256 ms |
73600 KB |
Output is correct |
92 |
Correct |
159 ms |
71496 KB |
Output is correct |
93 |
Correct |
17 ms |
25472 KB |
Output is correct |
94 |
Correct |
13 ms |
23824 KB |
Output is correct |
95 |
Correct |
219 ms |
78284 KB |
Output is correct |
96 |
Correct |
254 ms |
83828 KB |
Output is correct |
97 |
Correct |
16 ms |
25300 KB |
Output is correct |
98 |
Correct |
13 ms |
23892 KB |
Output is correct |
99 |
Correct |
173 ms |
74316 KB |
Output is correct |
100 |
Correct |
18 ms |
25428 KB |
Output is correct |
101 |
Correct |
221 ms |
73348 KB |
Output is correct |
102 |
Correct |
249 ms |
82256 KB |
Output is correct |
103 |
Correct |
20 ms |
25428 KB |
Output is correct |
104 |
Correct |
17 ms |
25556 KB |
Output is correct |
105 |
Correct |
16 ms |
25316 KB |
Output is correct |
106 |
Correct |
18 ms |
25320 KB |
Output is correct |
107 |
Correct |
16 ms |
25428 KB |
Output is correct |
108 |
Runtime error |
948 ms |
262144 KB |
Execution killed with signal 9 |
109 |
Halted |
0 ms |
0 KB |
- |