#include <bits/stdc++.h>
// #pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 5e5+10;
const int MAXA = 9e3+20;
const ll INF = 1e9+10;
const int LOG = 13;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
vector <int> key = {29, 31};
vector <int> mod = {998244353, 1000000007};
void chmx(int &a, int b){ a = max(a, b); }
int n, k;
vector <int> adj[MAXN], vec[MAXN];
int a[MAXN], tot[MAXN], u[MAXN], v[MAXN];
struct dsu {
int par[MAXN], siz[MAXN];
void bd(){
for(int i=1; i<=n; i++){
par[i] = i; siz[i] = 1;
}
}
int f(int x){
if(par[x]==x) return x;
return par[x] = f(par[x]);
}
bool con(int x, int y){ return f(x) == f(y); }
void mer(int x, int y){
x = f(x); y = f(y);
if(x==y) return;
if(siz[x] > siz[y]) swap(x, y);
par[x] = y; siz[y] += siz[x];
}
} DSU;
int anc[MAXN][LOG+5], dep[MAXN], in[MAXN], tim;
void dfs(int nw, int par){
anc[nw][0] = par; dep[nw] = dep[par]+1; in[nw] = ++tim;
for(auto nx : adj[nw]){
if(nx==par) continue;
dfs(nx, nw);
}
}
int LCA(int x, int y){
if(dep[x] > dep[y]) swap(x, y);
for(int i=LOG-1; i>=0; i--){
if(dep[anc[y][i]] >= dep[x])
y = anc[y][i];
}
if(x==y) return x;
for(int i=LOG-1; i>=0; i--){
if(anc[y][i] != anc[x][i]){
y = anc[y][i]; x = anc[x][i];
}
}
return anc[x][0];
}
int cnt[MAXN];
vector <int> edge[MAXN];
bool cmp(int a, int b){
return in[a] < in[b];
}
int ANS(int nw, int par){
for(auto nx : adj[nw]){
if(nx==par) continue;
cnt[nw] += ANS(nx, nw);
}
return cnt[nw];
}
signed main(){
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> k; DSU.bd();
for(int i=1; i<=n-1; i++){
int x, y; cin >> x >> y; u[i] = x; v[i] = y;
adj[x].pb(y); adj[y].pb(x);
}
for(int i=1; i<=n; i++){
cin >> a[i]; tot[a[i]]++;
vec[a[i]].pb(i);
}
dfs(1, 0);
for(int j=1; j<LOG; j++)
for(int i=1; i<=n; i++) anc[i][j] = anc[anc[i][j-1]][j-1];
for(int i=1; i<=k; i++){
if(vec[i].size() <= 1) continue;
sort(vec[i].begin(), vec[i].end(), cmp);
for(int j=1; j<vec[i].size(); j++){
cnt[LCA(vec[i][j-1], vec[i][j])] += 2;
cnt[vec[i][j-1]]--; cnt[vec[i][j]]--;
}
cnt[LCA(vec[i][0], vec[i].back())] += 2;
cnt[vec[i][0]]--; cnt[vec[i].back()]--;
}
ANS(1, 0);
for(int i=1; i<=n; i++)
if(cnt[i]) DSU.mer(i, anc[i][0]);
for(int i=1; i<=n-1; i++){
int x = DSU.f(u[i]), y = DSU.f(v[i]);
if(x == y) continue;
edge[x].pb(y); edge[y].pb(x);
}
int tot = 0;
for(int i=1; i<=n; i++){
if(edge[i].size() == 1) tot++;
}
cout << (tot+1)/2 << '\n';
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:104:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int j=1; j<vec[i].size(); j++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
51036 KB |
Output is correct |
2 |
Correct |
8 ms |
51036 KB |
Output is correct |
3 |
Correct |
8 ms |
51036 KB |
Output is correct |
4 |
Correct |
8 ms |
51068 KB |
Output is correct |
5 |
Correct |
8 ms |
51072 KB |
Output is correct |
6 |
Correct |
7 ms |
48820 KB |
Output is correct |
7 |
Correct |
7 ms |
46936 KB |
Output is correct |
8 |
Correct |
8 ms |
50960 KB |
Output is correct |
9 |
Correct |
8 ms |
51036 KB |
Output is correct |
10 |
Correct |
8 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
46940 KB |
Output is correct |
12 |
Correct |
7 ms |
46940 KB |
Output is correct |
13 |
Correct |
8 ms |
50864 KB |
Output is correct |
14 |
Correct |
7 ms |
51036 KB |
Output is correct |
15 |
Correct |
7 ms |
44892 KB |
Output is correct |
16 |
Correct |
7 ms |
44948 KB |
Output is correct |
17 |
Correct |
7 ms |
46940 KB |
Output is correct |
18 |
Correct |
8 ms |
46940 KB |
Output is correct |
19 |
Correct |
8 ms |
51036 KB |
Output is correct |
20 |
Correct |
8 ms |
51040 KB |
Output is correct |
21 |
Correct |
7 ms |
44932 KB |
Output is correct |
22 |
Correct |
8 ms |
51028 KB |
Output is correct |
23 |
Correct |
8 ms |
51012 KB |
Output is correct |
24 |
Correct |
7 ms |
51032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
51036 KB |
Output is correct |
2 |
Correct |
8 ms |
51036 KB |
Output is correct |
3 |
Correct |
8 ms |
51036 KB |
Output is correct |
4 |
Correct |
8 ms |
51068 KB |
Output is correct |
5 |
Correct |
8 ms |
51072 KB |
Output is correct |
6 |
Correct |
7 ms |
48820 KB |
Output is correct |
7 |
Correct |
7 ms |
46936 KB |
Output is correct |
8 |
Correct |
8 ms |
50960 KB |
Output is correct |
9 |
Correct |
8 ms |
51036 KB |
Output is correct |
10 |
Correct |
8 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
46940 KB |
Output is correct |
12 |
Correct |
7 ms |
46940 KB |
Output is correct |
13 |
Correct |
8 ms |
50864 KB |
Output is correct |
14 |
Correct |
7 ms |
51036 KB |
Output is correct |
15 |
Correct |
7 ms |
44892 KB |
Output is correct |
16 |
Correct |
7 ms |
44948 KB |
Output is correct |
17 |
Correct |
7 ms |
46940 KB |
Output is correct |
18 |
Correct |
8 ms |
46940 KB |
Output is correct |
19 |
Correct |
8 ms |
51036 KB |
Output is correct |
20 |
Correct |
8 ms |
51040 KB |
Output is correct |
21 |
Correct |
7 ms |
44932 KB |
Output is correct |
22 |
Correct |
8 ms |
51028 KB |
Output is correct |
23 |
Correct |
8 ms |
51012 KB |
Output is correct |
24 |
Correct |
7 ms |
51032 KB |
Output is correct |
25 |
Correct |
8 ms |
51036 KB |
Output is correct |
26 |
Correct |
10 ms |
51292 KB |
Output is correct |
27 |
Correct |
10 ms |
51292 KB |
Output is correct |
28 |
Correct |
10 ms |
51296 KB |
Output is correct |
29 |
Correct |
10 ms |
51288 KB |
Output is correct |
30 |
Correct |
10 ms |
51332 KB |
Output is correct |
31 |
Correct |
8 ms |
51032 KB |
Output is correct |
32 |
Correct |
10 ms |
51280 KB |
Output is correct |
33 |
Correct |
10 ms |
51016 KB |
Output is correct |
34 |
Correct |
10 ms |
51292 KB |
Output is correct |
35 |
Correct |
10 ms |
51360 KB |
Output is correct |
36 |
Correct |
10 ms |
51304 KB |
Output is correct |
37 |
Correct |
10 ms |
51288 KB |
Output is correct |
38 |
Correct |
8 ms |
51028 KB |
Output is correct |
39 |
Correct |
10 ms |
51292 KB |
Output is correct |
40 |
Correct |
10 ms |
51288 KB |
Output is correct |
41 |
Correct |
9 ms |
51292 KB |
Output is correct |
42 |
Correct |
9 ms |
51292 KB |
Output is correct |
43 |
Correct |
11 ms |
51268 KB |
Output is correct |
44 |
Correct |
9 ms |
51036 KB |
Output is correct |
45 |
Correct |
10 ms |
47708 KB |
Output is correct |
46 |
Correct |
11 ms |
47452 KB |
Output is correct |
47 |
Correct |
8 ms |
46940 KB |
Output is correct |
48 |
Correct |
9 ms |
47452 KB |
Output is correct |
49 |
Correct |
10 ms |
47708 KB |
Output is correct |
50 |
Correct |
10 ms |
51316 KB |
Output is correct |
51 |
Correct |
10 ms |
51292 KB |
Output is correct |
52 |
Correct |
10 ms |
47672 KB |
Output is correct |
53 |
Correct |
10 ms |
51292 KB |
Output is correct |
54 |
Correct |
10 ms |
49516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
51036 KB |
Output is correct |
2 |
Correct |
8 ms |
51036 KB |
Output is correct |
3 |
Correct |
8 ms |
51036 KB |
Output is correct |
4 |
Correct |
8 ms |
51068 KB |
Output is correct |
5 |
Correct |
8 ms |
51072 KB |
Output is correct |
6 |
Correct |
7 ms |
48820 KB |
Output is correct |
7 |
Correct |
7 ms |
46936 KB |
Output is correct |
8 |
Correct |
8 ms |
50960 KB |
Output is correct |
9 |
Correct |
8 ms |
51036 KB |
Output is correct |
10 |
Correct |
8 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
46940 KB |
Output is correct |
12 |
Correct |
7 ms |
46940 KB |
Output is correct |
13 |
Correct |
8 ms |
50864 KB |
Output is correct |
14 |
Correct |
7 ms |
51036 KB |
Output is correct |
15 |
Correct |
7 ms |
44892 KB |
Output is correct |
16 |
Correct |
7 ms |
44948 KB |
Output is correct |
17 |
Correct |
7 ms |
46940 KB |
Output is correct |
18 |
Correct |
8 ms |
46940 KB |
Output is correct |
19 |
Correct |
8 ms |
51036 KB |
Output is correct |
20 |
Correct |
8 ms |
51040 KB |
Output is correct |
21 |
Correct |
7 ms |
44932 KB |
Output is correct |
22 |
Correct |
8 ms |
51028 KB |
Output is correct |
23 |
Correct |
8 ms |
51012 KB |
Output is correct |
24 |
Correct |
7 ms |
51032 KB |
Output is correct |
25 |
Correct |
8 ms |
48984 KB |
Output is correct |
26 |
Correct |
110 ms |
73400 KB |
Output is correct |
27 |
Correct |
111 ms |
73296 KB |
Output is correct |
28 |
Correct |
9 ms |
51288 KB |
Output is correct |
29 |
Correct |
8 ms |
51036 KB |
Output is correct |
30 |
Correct |
7 ms |
51036 KB |
Output is correct |
31 |
Correct |
96 ms |
72276 KB |
Output is correct |
32 |
Correct |
9 ms |
51292 KB |
Output is correct |
33 |
Correct |
113 ms |
78420 KB |
Output is correct |
34 |
Correct |
112 ms |
73412 KB |
Output is correct |
35 |
Correct |
10 ms |
51292 KB |
Output is correct |
36 |
Correct |
101 ms |
72908 KB |
Output is correct |
37 |
Correct |
10 ms |
51292 KB |
Output is correct |
38 |
Correct |
12 ms |
51268 KB |
Output is correct |
39 |
Correct |
94 ms |
73280 KB |
Output is correct |
40 |
Correct |
10 ms |
51292 KB |
Output is correct |
41 |
Correct |
118 ms |
73096 KB |
Output is correct |
42 |
Incorrect |
107 ms |
74064 KB |
Output isn't correct |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
73408 KB |
Output is correct |
2 |
Correct |
95 ms |
79300 KB |
Output is correct |
3 |
Correct |
10 ms |
51292 KB |
Output is correct |
4 |
Correct |
12 ms |
51288 KB |
Output is correct |
5 |
Correct |
10 ms |
51036 KB |
Output is correct |
6 |
Correct |
7 ms |
51008 KB |
Output is correct |
7 |
Correct |
10 ms |
51292 KB |
Output is correct |
8 |
Correct |
113 ms |
74380 KB |
Output is correct |
9 |
Correct |
10 ms |
51288 KB |
Output is correct |
10 |
Correct |
98 ms |
73300 KB |
Output is correct |
11 |
Correct |
8 ms |
51032 KB |
Output is correct |
12 |
Correct |
102 ms |
72780 KB |
Output is correct |
13 |
Correct |
117 ms |
76464 KB |
Output is correct |
14 |
Correct |
106 ms |
78964 KB |
Output is correct |
15 |
Correct |
88 ms |
73320 KB |
Output is correct |
16 |
Correct |
10 ms |
51288 KB |
Output is correct |
17 |
Correct |
8 ms |
51036 KB |
Output is correct |
18 |
Correct |
98 ms |
77656 KB |
Output is correct |
19 |
Correct |
116 ms |
80208 KB |
Output is correct |
20 |
Correct |
10 ms |
51288 KB |
Output is correct |
21 |
Correct |
8 ms |
51084 KB |
Output is correct |
22 |
Correct |
102 ms |
74996 KB |
Output is correct |
23 |
Correct |
10 ms |
51288 KB |
Output is correct |
24 |
Correct |
111 ms |
72788 KB |
Output is correct |
25 |
Correct |
114 ms |
81320 KB |
Output is correct |
26 |
Correct |
9 ms |
51288 KB |
Output is correct |
27 |
Correct |
10 ms |
51288 KB |
Output is correct |
28 |
Correct |
10 ms |
51292 KB |
Output is correct |
29 |
Correct |
10 ms |
51292 KB |
Output is correct |
30 |
Correct |
10 ms |
51292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
51036 KB |
Output is correct |
2 |
Correct |
8 ms |
51036 KB |
Output is correct |
3 |
Correct |
8 ms |
51036 KB |
Output is correct |
4 |
Correct |
8 ms |
51068 KB |
Output is correct |
5 |
Correct |
8 ms |
51072 KB |
Output is correct |
6 |
Correct |
7 ms |
48820 KB |
Output is correct |
7 |
Correct |
7 ms |
46936 KB |
Output is correct |
8 |
Correct |
8 ms |
50960 KB |
Output is correct |
9 |
Correct |
8 ms |
51036 KB |
Output is correct |
10 |
Correct |
8 ms |
51288 KB |
Output is correct |
11 |
Correct |
7 ms |
46940 KB |
Output is correct |
12 |
Correct |
7 ms |
46940 KB |
Output is correct |
13 |
Correct |
8 ms |
50864 KB |
Output is correct |
14 |
Correct |
7 ms |
51036 KB |
Output is correct |
15 |
Correct |
7 ms |
44892 KB |
Output is correct |
16 |
Correct |
7 ms |
44948 KB |
Output is correct |
17 |
Correct |
7 ms |
46940 KB |
Output is correct |
18 |
Correct |
8 ms |
46940 KB |
Output is correct |
19 |
Correct |
8 ms |
51036 KB |
Output is correct |
20 |
Correct |
8 ms |
51040 KB |
Output is correct |
21 |
Correct |
7 ms |
44932 KB |
Output is correct |
22 |
Correct |
8 ms |
51028 KB |
Output is correct |
23 |
Correct |
8 ms |
51012 KB |
Output is correct |
24 |
Correct |
7 ms |
51032 KB |
Output is correct |
25 |
Correct |
8 ms |
51036 KB |
Output is correct |
26 |
Correct |
10 ms |
51292 KB |
Output is correct |
27 |
Correct |
10 ms |
51292 KB |
Output is correct |
28 |
Correct |
10 ms |
51296 KB |
Output is correct |
29 |
Correct |
10 ms |
51288 KB |
Output is correct |
30 |
Correct |
10 ms |
51332 KB |
Output is correct |
31 |
Correct |
8 ms |
51032 KB |
Output is correct |
32 |
Correct |
10 ms |
51280 KB |
Output is correct |
33 |
Correct |
10 ms |
51016 KB |
Output is correct |
34 |
Correct |
10 ms |
51292 KB |
Output is correct |
35 |
Correct |
10 ms |
51360 KB |
Output is correct |
36 |
Correct |
10 ms |
51304 KB |
Output is correct |
37 |
Correct |
10 ms |
51288 KB |
Output is correct |
38 |
Correct |
8 ms |
51028 KB |
Output is correct |
39 |
Correct |
10 ms |
51292 KB |
Output is correct |
40 |
Correct |
10 ms |
51288 KB |
Output is correct |
41 |
Correct |
9 ms |
51292 KB |
Output is correct |
42 |
Correct |
9 ms |
51292 KB |
Output is correct |
43 |
Correct |
11 ms |
51268 KB |
Output is correct |
44 |
Correct |
9 ms |
51036 KB |
Output is correct |
45 |
Correct |
10 ms |
47708 KB |
Output is correct |
46 |
Correct |
11 ms |
47452 KB |
Output is correct |
47 |
Correct |
8 ms |
46940 KB |
Output is correct |
48 |
Correct |
9 ms |
47452 KB |
Output is correct |
49 |
Correct |
10 ms |
47708 KB |
Output is correct |
50 |
Correct |
10 ms |
51316 KB |
Output is correct |
51 |
Correct |
10 ms |
51292 KB |
Output is correct |
52 |
Correct |
10 ms |
47672 KB |
Output is correct |
53 |
Correct |
10 ms |
51292 KB |
Output is correct |
54 |
Correct |
10 ms |
49516 KB |
Output is correct |
55 |
Correct |
8 ms |
48984 KB |
Output is correct |
56 |
Correct |
110 ms |
73400 KB |
Output is correct |
57 |
Correct |
111 ms |
73296 KB |
Output is correct |
58 |
Correct |
9 ms |
51288 KB |
Output is correct |
59 |
Correct |
8 ms |
51036 KB |
Output is correct |
60 |
Correct |
7 ms |
51036 KB |
Output is correct |
61 |
Correct |
96 ms |
72276 KB |
Output is correct |
62 |
Correct |
9 ms |
51292 KB |
Output is correct |
63 |
Correct |
113 ms |
78420 KB |
Output is correct |
64 |
Correct |
112 ms |
73412 KB |
Output is correct |
65 |
Correct |
10 ms |
51292 KB |
Output is correct |
66 |
Correct |
101 ms |
72908 KB |
Output is correct |
67 |
Correct |
10 ms |
51292 KB |
Output is correct |
68 |
Correct |
12 ms |
51268 KB |
Output is correct |
69 |
Correct |
94 ms |
73280 KB |
Output is correct |
70 |
Correct |
10 ms |
51292 KB |
Output is correct |
71 |
Correct |
118 ms |
73096 KB |
Output is correct |
72 |
Incorrect |
107 ms |
74064 KB |
Output isn't correct |
73 |
Halted |
0 ms |
0 KB |
- |