#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
vector<int> adj[maxn];
int N, cnt[maxn], sum[maxn], K;
vector<int> grp[maxn];
int depth[maxn], tin[maxn], tout[maxn], par[maxn][20];
class DSU
{
public:
vector<int> lab;
void init(int n)
{
lab.assign(n + 5, -1);
}
int finds(int u)
{
if (lab[u] < 0) return u;
return lab[u] = finds(lab[u]);
}
void merges(int u, int v)
{
u = finds(u); v = finds(v);
if (u == v) return;
if (lab[v] < lab[u]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
}
}dsu;
void prepare(int u = 1, int p = -1)
{
static int nTime = 0;
tin[u] = ++nTime;
for (int i = 1; (1 << i) <= N; ++i){
par[u][i] = par[par[u][i - 1]][i - 1];
}
for (int v : adj[u]) if (v != p){
depth[v] = depth[u] + 1;
par[v][0] = u;
prepare(v, u);
}
tout[u] = nTime;
}
int LCA(int x, int y)
{
if (depth[y] > depth[x]) swap(x, y);
for (int k = 19; k >= 0; --k){
if (depth[par[x][k]] >= depth[y]){
x = par[x][k];
}
}
if (x == y) return x;
for (int k = 19; k >= 0; --k){
if (par[x][k] != par[y][k]){
x = par[x][k];
y = par[y][k];
}
}
return par[x][0];
}
void DFS(int u, int p = -1)
{
for (int v : adj[u]) if (v != p){
DFS(v, u);
sum[u] += sum[v];
}
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
cin >> N >> K; dsu.init(N);
for (int i = 1; i < N; ++i){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
for (int i = 1; i <= N; ++i){
int v; cin >> v;
grp[v].pb(i);
}
depth[1] = 1;
prepare();
for (int i = 1; i <= K; ++i){
sort(grp[i].begin(), grp[i].end(),
[&](const int & x, const int & y){
return tin[x] < tin[y];
}
);
for (int j = 0; j < (int)grp[i].size() - 1; ++j){
sum[grp[i][j]]++; sum[grp[i][j + 1]]++;
sum[LCA(grp[i][j], grp[i][j + 1])] -= 2;
}
}
DFS(1);
for (int i = 2; i <= N; ++i){
if (sum[i] >= 1){
dsu.merges(i, par[i][0]);
}
}
for (int i = 2; i <= N; ++i){
int l = dsu.finds(i);
int r = dsu.finds(par[i][0]);
if (l != r) ++cnt[l], ++cnt[r];
}
int res = 0;
for (int i = 1; i <= N; ++i){
res += (cnt[i] == 1);
}
cout << (res + 1) / 2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:86:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.INP", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
mergers.cpp:87:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.OUT", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
8 ms |
5112 KB |
Output is correct |
16 |
Correct |
7 ms |
4984 KB |
Output is correct |
17 |
Correct |
9 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
8 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
8 ms |
5112 KB |
Output is correct |
23 |
Correct |
9 ms |
4984 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
8 ms |
5112 KB |
Output is correct |
16 |
Correct |
7 ms |
4984 KB |
Output is correct |
17 |
Correct |
9 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
8 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
8 ms |
5112 KB |
Output is correct |
23 |
Correct |
9 ms |
4984 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
25 |
Correct |
8 ms |
5112 KB |
Output is correct |
26 |
Correct |
10 ms |
5496 KB |
Output is correct |
27 |
Correct |
10 ms |
5496 KB |
Output is correct |
28 |
Correct |
11 ms |
5728 KB |
Output is correct |
29 |
Correct |
9 ms |
5624 KB |
Output is correct |
30 |
Correct |
11 ms |
5496 KB |
Output is correct |
31 |
Correct |
8 ms |
5016 KB |
Output is correct |
32 |
Correct |
10 ms |
5752 KB |
Output is correct |
33 |
Correct |
8 ms |
5112 KB |
Output is correct |
34 |
Correct |
10 ms |
5496 KB |
Output is correct |
35 |
Correct |
11 ms |
5624 KB |
Output is correct |
36 |
Correct |
11 ms |
5496 KB |
Output is correct |
37 |
Correct |
10 ms |
5624 KB |
Output is correct |
38 |
Correct |
8 ms |
5112 KB |
Output is correct |
39 |
Correct |
11 ms |
5496 KB |
Output is correct |
40 |
Correct |
10 ms |
5500 KB |
Output is correct |
41 |
Correct |
10 ms |
5496 KB |
Output is correct |
42 |
Correct |
10 ms |
5496 KB |
Output is correct |
43 |
Correct |
11 ms |
5752 KB |
Output is correct |
44 |
Correct |
8 ms |
5112 KB |
Output is correct |
45 |
Correct |
12 ms |
5752 KB |
Output is correct |
46 |
Correct |
10 ms |
5496 KB |
Output is correct |
47 |
Correct |
8 ms |
5112 KB |
Output is correct |
48 |
Correct |
11 ms |
5496 KB |
Output is correct |
49 |
Correct |
10 ms |
5624 KB |
Output is correct |
50 |
Correct |
10 ms |
5624 KB |
Output is correct |
51 |
Correct |
10 ms |
5500 KB |
Output is correct |
52 |
Correct |
10 ms |
5500 KB |
Output is correct |
53 |
Correct |
10 ms |
5624 KB |
Output is correct |
54 |
Correct |
10 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
8 ms |
5112 KB |
Output is correct |
16 |
Correct |
7 ms |
4984 KB |
Output is correct |
17 |
Correct |
9 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
8 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
8 ms |
5112 KB |
Output is correct |
23 |
Correct |
9 ms |
4984 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
25 |
Correct |
8 ms |
5116 KB |
Output is correct |
26 |
Correct |
116 ms |
20336 KB |
Output is correct |
27 |
Correct |
165 ms |
19960 KB |
Output is correct |
28 |
Correct |
10 ms |
5496 KB |
Output is correct |
29 |
Correct |
8 ms |
5112 KB |
Output is correct |
30 |
Correct |
8 ms |
5112 KB |
Output is correct |
31 |
Correct |
152 ms |
19960 KB |
Output is correct |
32 |
Correct |
10 ms |
5496 KB |
Output is correct |
33 |
Correct |
218 ms |
25720 KB |
Output is correct |
34 |
Correct |
163 ms |
19960 KB |
Output is correct |
35 |
Correct |
11 ms |
5496 KB |
Output is correct |
36 |
Correct |
181 ms |
20576 KB |
Output is correct |
37 |
Correct |
10 ms |
5496 KB |
Output is correct |
38 |
Correct |
12 ms |
5752 KB |
Output is correct |
39 |
Correct |
116 ms |
20208 KB |
Output is correct |
40 |
Correct |
11 ms |
5624 KB |
Output is correct |
41 |
Correct |
154 ms |
19952 KB |
Output is correct |
42 |
Correct |
206 ms |
22008 KB |
Output is correct |
43 |
Correct |
8 ms |
5112 KB |
Output is correct |
44 |
Correct |
192 ms |
25856 KB |
Output is correct |
45 |
Correct |
212 ms |
23008 KB |
Output is correct |
46 |
Correct |
10 ms |
5496 KB |
Output is correct |
47 |
Correct |
11 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
18928 KB |
Output is correct |
2 |
Correct |
103 ms |
23280 KB |
Output is correct |
3 |
Correct |
10 ms |
5496 KB |
Output is correct |
4 |
Correct |
10 ms |
5496 KB |
Output is correct |
5 |
Correct |
8 ms |
5112 KB |
Output is correct |
6 |
Correct |
9 ms |
5112 KB |
Output is correct |
7 |
Correct |
10 ms |
5496 KB |
Output is correct |
8 |
Correct |
162 ms |
21752 KB |
Output is correct |
9 |
Correct |
11 ms |
5496 KB |
Output is correct |
10 |
Correct |
146 ms |
20320 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
173 ms |
20344 KB |
Output is correct |
13 |
Correct |
162 ms |
21624 KB |
Output is correct |
14 |
Correct |
131 ms |
23292 KB |
Output is correct |
15 |
Correct |
118 ms |
20208 KB |
Output is correct |
16 |
Correct |
10 ms |
5496 KB |
Output is correct |
17 |
Correct |
8 ms |
5112 KB |
Output is correct |
18 |
Correct |
105 ms |
23344 KB |
Output is correct |
19 |
Correct |
163 ms |
26488 KB |
Output is correct |
20 |
Correct |
10 ms |
5624 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
112 ms |
22000 KB |
Output is correct |
23 |
Correct |
11 ms |
5496 KB |
Output is correct |
24 |
Correct |
165 ms |
20856 KB |
Output is correct |
25 |
Correct |
157 ms |
25336 KB |
Output is correct |
26 |
Correct |
10 ms |
5624 KB |
Output is correct |
27 |
Correct |
10 ms |
5624 KB |
Output is correct |
28 |
Correct |
10 ms |
5496 KB |
Output is correct |
29 |
Correct |
10 ms |
5496 KB |
Output is correct |
30 |
Correct |
10 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5112 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
8 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
8 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
8 ms |
5240 KB |
Output is correct |
14 |
Correct |
8 ms |
5112 KB |
Output is correct |
15 |
Correct |
8 ms |
5112 KB |
Output is correct |
16 |
Correct |
7 ms |
4984 KB |
Output is correct |
17 |
Correct |
9 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
8 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
8 ms |
5112 KB |
Output is correct |
23 |
Correct |
9 ms |
4984 KB |
Output is correct |
24 |
Correct |
8 ms |
5112 KB |
Output is correct |
25 |
Correct |
8 ms |
5112 KB |
Output is correct |
26 |
Correct |
10 ms |
5496 KB |
Output is correct |
27 |
Correct |
10 ms |
5496 KB |
Output is correct |
28 |
Correct |
11 ms |
5728 KB |
Output is correct |
29 |
Correct |
9 ms |
5624 KB |
Output is correct |
30 |
Correct |
11 ms |
5496 KB |
Output is correct |
31 |
Correct |
8 ms |
5016 KB |
Output is correct |
32 |
Correct |
10 ms |
5752 KB |
Output is correct |
33 |
Correct |
8 ms |
5112 KB |
Output is correct |
34 |
Correct |
10 ms |
5496 KB |
Output is correct |
35 |
Correct |
11 ms |
5624 KB |
Output is correct |
36 |
Correct |
11 ms |
5496 KB |
Output is correct |
37 |
Correct |
10 ms |
5624 KB |
Output is correct |
38 |
Correct |
8 ms |
5112 KB |
Output is correct |
39 |
Correct |
11 ms |
5496 KB |
Output is correct |
40 |
Correct |
10 ms |
5500 KB |
Output is correct |
41 |
Correct |
10 ms |
5496 KB |
Output is correct |
42 |
Correct |
10 ms |
5496 KB |
Output is correct |
43 |
Correct |
11 ms |
5752 KB |
Output is correct |
44 |
Correct |
8 ms |
5112 KB |
Output is correct |
45 |
Correct |
12 ms |
5752 KB |
Output is correct |
46 |
Correct |
10 ms |
5496 KB |
Output is correct |
47 |
Correct |
8 ms |
5112 KB |
Output is correct |
48 |
Correct |
11 ms |
5496 KB |
Output is correct |
49 |
Correct |
10 ms |
5624 KB |
Output is correct |
50 |
Correct |
10 ms |
5624 KB |
Output is correct |
51 |
Correct |
10 ms |
5500 KB |
Output is correct |
52 |
Correct |
10 ms |
5500 KB |
Output is correct |
53 |
Correct |
10 ms |
5624 KB |
Output is correct |
54 |
Correct |
10 ms |
5496 KB |
Output is correct |
55 |
Correct |
8 ms |
5116 KB |
Output is correct |
56 |
Correct |
116 ms |
20336 KB |
Output is correct |
57 |
Correct |
165 ms |
19960 KB |
Output is correct |
58 |
Correct |
10 ms |
5496 KB |
Output is correct |
59 |
Correct |
8 ms |
5112 KB |
Output is correct |
60 |
Correct |
8 ms |
5112 KB |
Output is correct |
61 |
Correct |
152 ms |
19960 KB |
Output is correct |
62 |
Correct |
10 ms |
5496 KB |
Output is correct |
63 |
Correct |
218 ms |
25720 KB |
Output is correct |
64 |
Correct |
163 ms |
19960 KB |
Output is correct |
65 |
Correct |
11 ms |
5496 KB |
Output is correct |
66 |
Correct |
181 ms |
20576 KB |
Output is correct |
67 |
Correct |
10 ms |
5496 KB |
Output is correct |
68 |
Correct |
12 ms |
5752 KB |
Output is correct |
69 |
Correct |
116 ms |
20208 KB |
Output is correct |
70 |
Correct |
11 ms |
5624 KB |
Output is correct |
71 |
Correct |
154 ms |
19952 KB |
Output is correct |
72 |
Correct |
206 ms |
22008 KB |
Output is correct |
73 |
Correct |
8 ms |
5112 KB |
Output is correct |
74 |
Correct |
192 ms |
25856 KB |
Output is correct |
75 |
Correct |
212 ms |
23008 KB |
Output is correct |
76 |
Correct |
10 ms |
5496 KB |
Output is correct |
77 |
Correct |
11 ms |
5496 KB |
Output is correct |
78 |
Correct |
114 ms |
18928 KB |
Output is correct |
79 |
Correct |
103 ms |
23280 KB |
Output is correct |
80 |
Correct |
10 ms |
5496 KB |
Output is correct |
81 |
Correct |
10 ms |
5496 KB |
Output is correct |
82 |
Correct |
8 ms |
5112 KB |
Output is correct |
83 |
Correct |
9 ms |
5112 KB |
Output is correct |
84 |
Correct |
10 ms |
5496 KB |
Output is correct |
85 |
Correct |
162 ms |
21752 KB |
Output is correct |
86 |
Correct |
11 ms |
5496 KB |
Output is correct |
87 |
Correct |
146 ms |
20320 KB |
Output is correct |
88 |
Correct |
8 ms |
5112 KB |
Output is correct |
89 |
Correct |
173 ms |
20344 KB |
Output is correct |
90 |
Correct |
162 ms |
21624 KB |
Output is correct |
91 |
Correct |
131 ms |
23292 KB |
Output is correct |
92 |
Correct |
118 ms |
20208 KB |
Output is correct |
93 |
Correct |
10 ms |
5496 KB |
Output is correct |
94 |
Correct |
8 ms |
5112 KB |
Output is correct |
95 |
Correct |
105 ms |
23344 KB |
Output is correct |
96 |
Correct |
163 ms |
26488 KB |
Output is correct |
97 |
Correct |
10 ms |
5624 KB |
Output is correct |
98 |
Correct |
8 ms |
5112 KB |
Output is correct |
99 |
Correct |
112 ms |
22000 KB |
Output is correct |
100 |
Correct |
11 ms |
5496 KB |
Output is correct |
101 |
Correct |
165 ms |
20856 KB |
Output is correct |
102 |
Correct |
157 ms |
25336 KB |
Output is correct |
103 |
Correct |
10 ms |
5624 KB |
Output is correct |
104 |
Correct |
10 ms |
5624 KB |
Output is correct |
105 |
Correct |
10 ms |
5496 KB |
Output is correct |
106 |
Correct |
10 ms |
5496 KB |
Output is correct |
107 |
Correct |
10 ms |
5496 KB |
Output is correct |
108 |
Runtime error |
18 ms |
14200 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
109 |
Halted |
0 ms |
0 KB |
- |