// In the name of God
//-MILY-
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 5e5 + 5;
int n, k;
vector<int> adj[N], vec[N];
int mn[N], tin[N], tout[N], T;
int ans, h[N], cnt, c[N];
bool vis[N], mark[N];
void dfs(int v, int p) {
tin[v] = ++T;
for (auto u : adj[v]) {
if (u == p)
continue;
dfs(u, v);
}
tout[v] = ++T;
}
void dfs2(int v, int p) {
vis[v] = true;
mn[v] = h[v];
bool tmp = false;
for (auto u : adj[v]) {
if (!vis[u]) {
h[u] = h[v] + 1;
dfs2(u, v);
if (mn[u] > h[v]) {
mark[u] = true;
cnt++;
}
mn[v] = min(mn[v], mn[u]);
}
else if (u != p || tmp) {
mn[v] = min(mn[v], h[u]);
}
if (u == p)
tmp = true;
}
}
void dfs3(int v) {
vis[v] = true;
for (auto u : adj[v]) {
if (!vis[u]) {
dfs3(u);
c[v] += c[u];
if (mark[u])
c[v]++;
}
}
ans += mark[v] && (c[v] == 0 || c[v] == cnt - 1);
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int v, u; cin >> v >> u;
adj[v].pb(u), adj[u].pb(v);
}
for (int i = 1; i <= n; i++) {
int s; cin >> s;
vec[s].pb(i);
}
dfs(1, 0);
for (int i = 1; i <= k; i++) {
vector<pair<int, int> > V;
for (auto v : vec[i]) {
V.pb(mp(tin[v], v));
}
sort(V.begin(), V.end());
for (int i = 0; i < (int)V.size(); i++) {
int j = (i + 1) % (int)V.size();
adj[V[i].se].pb(V[j].se);
adj[V[j].se].pb(V[i].se);
}
}
dfs2(1, 0);
memset(vis, 0, sizeof vis);
dfs3(1);
cout << (ans + 1) / 2 << '\n';
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24276 KB |
Output is correct |
2 |
Correct |
13 ms |
24384 KB |
Output is correct |
3 |
Correct |
13 ms |
24280 KB |
Output is correct |
4 |
Correct |
13 ms |
24308 KB |
Output is correct |
5 |
Correct |
12 ms |
24340 KB |
Output is correct |
6 |
Correct |
12 ms |
24228 KB |
Output is correct |
7 |
Correct |
13 ms |
24320 KB |
Output is correct |
8 |
Correct |
13 ms |
24276 KB |
Output is correct |
9 |
Correct |
12 ms |
24276 KB |
Output is correct |
10 |
Correct |
13 ms |
24276 KB |
Output is correct |
11 |
Correct |
13 ms |
24276 KB |
Output is correct |
12 |
Correct |
15 ms |
24276 KB |
Output is correct |
13 |
Correct |
13 ms |
24248 KB |
Output is correct |
14 |
Correct |
14 ms |
24284 KB |
Output is correct |
15 |
Correct |
13 ms |
24276 KB |
Output is correct |
16 |
Correct |
13 ms |
24276 KB |
Output is correct |
17 |
Correct |
13 ms |
24244 KB |
Output is correct |
18 |
Correct |
13 ms |
24276 KB |
Output is correct |
19 |
Correct |
13 ms |
24208 KB |
Output is correct |
20 |
Correct |
12 ms |
24324 KB |
Output is correct |
21 |
Correct |
15 ms |
24244 KB |
Output is correct |
22 |
Correct |
13 ms |
24324 KB |
Output is correct |
23 |
Correct |
13 ms |
24320 KB |
Output is correct |
24 |
Correct |
13 ms |
24276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24276 KB |
Output is correct |
2 |
Correct |
13 ms |
24384 KB |
Output is correct |
3 |
Correct |
13 ms |
24280 KB |
Output is correct |
4 |
Correct |
13 ms |
24308 KB |
Output is correct |
5 |
Correct |
12 ms |
24340 KB |
Output is correct |
6 |
Correct |
12 ms |
24228 KB |
Output is correct |
7 |
Correct |
13 ms |
24320 KB |
Output is correct |
8 |
Correct |
13 ms |
24276 KB |
Output is correct |
9 |
Correct |
12 ms |
24276 KB |
Output is correct |
10 |
Correct |
13 ms |
24276 KB |
Output is correct |
11 |
Correct |
13 ms |
24276 KB |
Output is correct |
12 |
Correct |
15 ms |
24276 KB |
Output is correct |
13 |
Correct |
13 ms |
24248 KB |
Output is correct |
14 |
Correct |
14 ms |
24284 KB |
Output is correct |
15 |
Correct |
13 ms |
24276 KB |
Output is correct |
16 |
Correct |
13 ms |
24276 KB |
Output is correct |
17 |
Correct |
13 ms |
24244 KB |
Output is correct |
18 |
Correct |
13 ms |
24276 KB |
Output is correct |
19 |
Correct |
13 ms |
24208 KB |
Output is correct |
20 |
Correct |
12 ms |
24324 KB |
Output is correct |
21 |
Correct |
15 ms |
24244 KB |
Output is correct |
22 |
Correct |
13 ms |
24324 KB |
Output is correct |
23 |
Correct |
13 ms |
24320 KB |
Output is correct |
24 |
Correct |
13 ms |
24276 KB |
Output is correct |
25 |
Correct |
13 ms |
24256 KB |
Output is correct |
26 |
Correct |
14 ms |
24640 KB |
Output is correct |
27 |
Correct |
14 ms |
24520 KB |
Output is correct |
28 |
Correct |
15 ms |
24660 KB |
Output is correct |
29 |
Correct |
17 ms |
24660 KB |
Output is correct |
30 |
Correct |
15 ms |
24716 KB |
Output is correct |
31 |
Correct |
13 ms |
24276 KB |
Output is correct |
32 |
Correct |
14 ms |
24720 KB |
Output is correct |
33 |
Correct |
13 ms |
24320 KB |
Output is correct |
34 |
Correct |
14 ms |
24464 KB |
Output is correct |
35 |
Correct |
17 ms |
24644 KB |
Output is correct |
36 |
Correct |
15 ms |
24544 KB |
Output is correct |
37 |
Correct |
17 ms |
24660 KB |
Output is correct |
38 |
Correct |
15 ms |
24276 KB |
Output is correct |
39 |
Correct |
14 ms |
24720 KB |
Output is correct |
40 |
Correct |
17 ms |
24832 KB |
Output is correct |
41 |
Correct |
14 ms |
24532 KB |
Output is correct |
42 |
Correct |
14 ms |
24556 KB |
Output is correct |
43 |
Correct |
15 ms |
24716 KB |
Output is correct |
44 |
Correct |
13 ms |
24276 KB |
Output is correct |
45 |
Correct |
18 ms |
24660 KB |
Output is correct |
46 |
Correct |
15 ms |
24532 KB |
Output is correct |
47 |
Correct |
13 ms |
24220 KB |
Output is correct |
48 |
Correct |
15 ms |
24480 KB |
Output is correct |
49 |
Correct |
16 ms |
24588 KB |
Output is correct |
50 |
Correct |
15 ms |
24608 KB |
Output is correct |
51 |
Correct |
14 ms |
24588 KB |
Output is correct |
52 |
Correct |
15 ms |
24460 KB |
Output is correct |
53 |
Correct |
14 ms |
24592 KB |
Output is correct |
54 |
Correct |
16 ms |
24748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24276 KB |
Output is correct |
2 |
Correct |
13 ms |
24384 KB |
Output is correct |
3 |
Correct |
13 ms |
24280 KB |
Output is correct |
4 |
Correct |
13 ms |
24308 KB |
Output is correct |
5 |
Correct |
12 ms |
24340 KB |
Output is correct |
6 |
Correct |
12 ms |
24228 KB |
Output is correct |
7 |
Correct |
13 ms |
24320 KB |
Output is correct |
8 |
Correct |
13 ms |
24276 KB |
Output is correct |
9 |
Correct |
12 ms |
24276 KB |
Output is correct |
10 |
Correct |
13 ms |
24276 KB |
Output is correct |
11 |
Correct |
13 ms |
24276 KB |
Output is correct |
12 |
Correct |
15 ms |
24276 KB |
Output is correct |
13 |
Correct |
13 ms |
24248 KB |
Output is correct |
14 |
Correct |
14 ms |
24284 KB |
Output is correct |
15 |
Correct |
13 ms |
24276 KB |
Output is correct |
16 |
Correct |
13 ms |
24276 KB |
Output is correct |
17 |
Correct |
13 ms |
24244 KB |
Output is correct |
18 |
Correct |
13 ms |
24276 KB |
Output is correct |
19 |
Correct |
13 ms |
24208 KB |
Output is correct |
20 |
Correct |
12 ms |
24324 KB |
Output is correct |
21 |
Correct |
15 ms |
24244 KB |
Output is correct |
22 |
Correct |
13 ms |
24324 KB |
Output is correct |
23 |
Correct |
13 ms |
24320 KB |
Output is correct |
24 |
Correct |
13 ms |
24276 KB |
Output is correct |
25 |
Correct |
13 ms |
24308 KB |
Output is correct |
26 |
Correct |
88 ms |
31980 KB |
Output is correct |
27 |
Correct |
100 ms |
37432 KB |
Output is correct |
28 |
Correct |
15 ms |
24536 KB |
Output is correct |
29 |
Correct |
13 ms |
24328 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
91 ms |
33048 KB |
Output is correct |
32 |
Correct |
15 ms |
24532 KB |
Output is correct |
33 |
Correct |
149 ms |
39196 KB |
Output is correct |
34 |
Correct |
97 ms |
33996 KB |
Output is correct |
35 |
Correct |
15 ms |
24532 KB |
Output is correct |
36 |
Correct |
100 ms |
32460 KB |
Output is correct |
37 |
Correct |
19 ms |
24788 KB |
Output is correct |
38 |
Correct |
17 ms |
24532 KB |
Output is correct |
39 |
Correct |
83 ms |
31940 KB |
Output is correct |
40 |
Correct |
15 ms |
24744 KB |
Output is correct |
41 |
Correct |
104 ms |
39840 KB |
Output is correct |
42 |
Correct |
109 ms |
38048 KB |
Output is correct |
43 |
Correct |
13 ms |
24276 KB |
Output is correct |
44 |
Correct |
102 ms |
39012 KB |
Output is correct |
45 |
Correct |
119 ms |
38520 KB |
Output is correct |
46 |
Correct |
17 ms |
24532 KB |
Output is correct |
47 |
Correct |
17 ms |
24652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
30464 KB |
Output is correct |
2 |
Correct |
93 ms |
32540 KB |
Output is correct |
3 |
Correct |
14 ms |
24532 KB |
Output is correct |
4 |
Correct |
15 ms |
24532 KB |
Output is correct |
5 |
Correct |
17 ms |
24276 KB |
Output is correct |
6 |
Correct |
13 ms |
24276 KB |
Output is correct |
7 |
Correct |
14 ms |
24464 KB |
Output is correct |
8 |
Correct |
119 ms |
33688 KB |
Output is correct |
9 |
Correct |
15 ms |
24612 KB |
Output is correct |
10 |
Correct |
100 ms |
32460 KB |
Output is correct |
11 |
Correct |
17 ms |
24332 KB |
Output is correct |
12 |
Correct |
118 ms |
31708 KB |
Output is correct |
13 |
Correct |
122 ms |
33644 KB |
Output is correct |
14 |
Correct |
135 ms |
35352 KB |
Output is correct |
15 |
Correct |
74 ms |
31872 KB |
Output is correct |
16 |
Correct |
16 ms |
24532 KB |
Output is correct |
17 |
Correct |
14 ms |
24276 KB |
Output is correct |
18 |
Correct |
125 ms |
34352 KB |
Output is correct |
19 |
Correct |
121 ms |
38392 KB |
Output is correct |
20 |
Correct |
14 ms |
24532 KB |
Output is correct |
21 |
Correct |
14 ms |
24324 KB |
Output is correct |
22 |
Correct |
103 ms |
33048 KB |
Output is correct |
23 |
Correct |
14 ms |
24580 KB |
Output is correct |
24 |
Correct |
110 ms |
32604 KB |
Output is correct |
25 |
Correct |
147 ms |
38632 KB |
Output is correct |
26 |
Correct |
14 ms |
24528 KB |
Output is correct |
27 |
Correct |
14 ms |
24660 KB |
Output is correct |
28 |
Correct |
14 ms |
24532 KB |
Output is correct |
29 |
Correct |
14 ms |
24532 KB |
Output is correct |
30 |
Correct |
15 ms |
24532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24276 KB |
Output is correct |
2 |
Correct |
13 ms |
24384 KB |
Output is correct |
3 |
Correct |
13 ms |
24280 KB |
Output is correct |
4 |
Correct |
13 ms |
24308 KB |
Output is correct |
5 |
Correct |
12 ms |
24340 KB |
Output is correct |
6 |
Correct |
12 ms |
24228 KB |
Output is correct |
7 |
Correct |
13 ms |
24320 KB |
Output is correct |
8 |
Correct |
13 ms |
24276 KB |
Output is correct |
9 |
Correct |
12 ms |
24276 KB |
Output is correct |
10 |
Correct |
13 ms |
24276 KB |
Output is correct |
11 |
Correct |
13 ms |
24276 KB |
Output is correct |
12 |
Correct |
15 ms |
24276 KB |
Output is correct |
13 |
Correct |
13 ms |
24248 KB |
Output is correct |
14 |
Correct |
14 ms |
24284 KB |
Output is correct |
15 |
Correct |
13 ms |
24276 KB |
Output is correct |
16 |
Correct |
13 ms |
24276 KB |
Output is correct |
17 |
Correct |
13 ms |
24244 KB |
Output is correct |
18 |
Correct |
13 ms |
24276 KB |
Output is correct |
19 |
Correct |
13 ms |
24208 KB |
Output is correct |
20 |
Correct |
12 ms |
24324 KB |
Output is correct |
21 |
Correct |
15 ms |
24244 KB |
Output is correct |
22 |
Correct |
13 ms |
24324 KB |
Output is correct |
23 |
Correct |
13 ms |
24320 KB |
Output is correct |
24 |
Correct |
13 ms |
24276 KB |
Output is correct |
25 |
Correct |
13 ms |
24256 KB |
Output is correct |
26 |
Correct |
14 ms |
24640 KB |
Output is correct |
27 |
Correct |
14 ms |
24520 KB |
Output is correct |
28 |
Correct |
15 ms |
24660 KB |
Output is correct |
29 |
Correct |
17 ms |
24660 KB |
Output is correct |
30 |
Correct |
15 ms |
24716 KB |
Output is correct |
31 |
Correct |
13 ms |
24276 KB |
Output is correct |
32 |
Correct |
14 ms |
24720 KB |
Output is correct |
33 |
Correct |
13 ms |
24320 KB |
Output is correct |
34 |
Correct |
14 ms |
24464 KB |
Output is correct |
35 |
Correct |
17 ms |
24644 KB |
Output is correct |
36 |
Correct |
15 ms |
24544 KB |
Output is correct |
37 |
Correct |
17 ms |
24660 KB |
Output is correct |
38 |
Correct |
15 ms |
24276 KB |
Output is correct |
39 |
Correct |
14 ms |
24720 KB |
Output is correct |
40 |
Correct |
17 ms |
24832 KB |
Output is correct |
41 |
Correct |
14 ms |
24532 KB |
Output is correct |
42 |
Correct |
14 ms |
24556 KB |
Output is correct |
43 |
Correct |
15 ms |
24716 KB |
Output is correct |
44 |
Correct |
13 ms |
24276 KB |
Output is correct |
45 |
Correct |
18 ms |
24660 KB |
Output is correct |
46 |
Correct |
15 ms |
24532 KB |
Output is correct |
47 |
Correct |
13 ms |
24220 KB |
Output is correct |
48 |
Correct |
15 ms |
24480 KB |
Output is correct |
49 |
Correct |
16 ms |
24588 KB |
Output is correct |
50 |
Correct |
15 ms |
24608 KB |
Output is correct |
51 |
Correct |
14 ms |
24588 KB |
Output is correct |
52 |
Correct |
15 ms |
24460 KB |
Output is correct |
53 |
Correct |
14 ms |
24592 KB |
Output is correct |
54 |
Correct |
16 ms |
24748 KB |
Output is correct |
55 |
Correct |
13 ms |
24308 KB |
Output is correct |
56 |
Correct |
88 ms |
31980 KB |
Output is correct |
57 |
Correct |
100 ms |
37432 KB |
Output is correct |
58 |
Correct |
15 ms |
24536 KB |
Output is correct |
59 |
Correct |
13 ms |
24328 KB |
Output is correct |
60 |
Correct |
13 ms |
24276 KB |
Output is correct |
61 |
Correct |
91 ms |
33048 KB |
Output is correct |
62 |
Correct |
15 ms |
24532 KB |
Output is correct |
63 |
Correct |
149 ms |
39196 KB |
Output is correct |
64 |
Correct |
97 ms |
33996 KB |
Output is correct |
65 |
Correct |
15 ms |
24532 KB |
Output is correct |
66 |
Correct |
100 ms |
32460 KB |
Output is correct |
67 |
Correct |
19 ms |
24788 KB |
Output is correct |
68 |
Correct |
17 ms |
24532 KB |
Output is correct |
69 |
Correct |
83 ms |
31940 KB |
Output is correct |
70 |
Correct |
15 ms |
24744 KB |
Output is correct |
71 |
Correct |
104 ms |
39840 KB |
Output is correct |
72 |
Correct |
109 ms |
38048 KB |
Output is correct |
73 |
Correct |
13 ms |
24276 KB |
Output is correct |
74 |
Correct |
102 ms |
39012 KB |
Output is correct |
75 |
Correct |
119 ms |
38520 KB |
Output is correct |
76 |
Correct |
17 ms |
24532 KB |
Output is correct |
77 |
Correct |
17 ms |
24652 KB |
Output is correct |
78 |
Correct |
83 ms |
30464 KB |
Output is correct |
79 |
Correct |
93 ms |
32540 KB |
Output is correct |
80 |
Correct |
14 ms |
24532 KB |
Output is correct |
81 |
Correct |
15 ms |
24532 KB |
Output is correct |
82 |
Correct |
17 ms |
24276 KB |
Output is correct |
83 |
Correct |
13 ms |
24276 KB |
Output is correct |
84 |
Correct |
14 ms |
24464 KB |
Output is correct |
85 |
Correct |
119 ms |
33688 KB |
Output is correct |
86 |
Correct |
15 ms |
24612 KB |
Output is correct |
87 |
Correct |
100 ms |
32460 KB |
Output is correct |
88 |
Correct |
17 ms |
24332 KB |
Output is correct |
89 |
Correct |
118 ms |
31708 KB |
Output is correct |
90 |
Correct |
122 ms |
33644 KB |
Output is correct |
91 |
Correct |
135 ms |
35352 KB |
Output is correct |
92 |
Correct |
74 ms |
31872 KB |
Output is correct |
93 |
Correct |
16 ms |
24532 KB |
Output is correct |
94 |
Correct |
14 ms |
24276 KB |
Output is correct |
95 |
Correct |
125 ms |
34352 KB |
Output is correct |
96 |
Correct |
121 ms |
38392 KB |
Output is correct |
97 |
Correct |
14 ms |
24532 KB |
Output is correct |
98 |
Correct |
14 ms |
24324 KB |
Output is correct |
99 |
Correct |
103 ms |
33048 KB |
Output is correct |
100 |
Correct |
14 ms |
24580 KB |
Output is correct |
101 |
Correct |
110 ms |
32604 KB |
Output is correct |
102 |
Correct |
147 ms |
38632 KB |
Output is correct |
103 |
Correct |
14 ms |
24528 KB |
Output is correct |
104 |
Correct |
14 ms |
24660 KB |
Output is correct |
105 |
Correct |
14 ms |
24532 KB |
Output is correct |
106 |
Correct |
14 ms |
24532 KB |
Output is correct |
107 |
Correct |
15 ms |
24532 KB |
Output is correct |
108 |
Correct |
699 ms |
68496 KB |
Output is correct |
109 |
Correct |
908 ms |
86096 KB |
Output is correct |
110 |
Correct |
910 ms |
98628 KB |
Output is correct |
111 |
Correct |
1141 ms |
99360 KB |
Output is correct |
112 |
Correct |
1046 ms |
94792 KB |
Output is correct |
113 |
Correct |
691 ms |
76240 KB |
Output is correct |
114 |
Correct |
888 ms |
102916 KB |
Output is correct |
115 |
Correct |
857 ms |
102928 KB |
Output is correct |
116 |
Correct |
905 ms |
71408 KB |
Output is correct |
117 |
Correct |
948 ms |
80268 KB |
Output is correct |
118 |
Correct |
815 ms |
65832 KB |
Output is correct |
119 |
Correct |
899 ms |
80236 KB |
Output is correct |
120 |
Correct |
971 ms |
97956 KB |
Output is correct |
121 |
Correct |
902 ms |
80332 KB |
Output is correct |
122 |
Correct |
966 ms |
92236 KB |
Output is correct |
123 |
Correct |
680 ms |
75900 KB |
Output is correct |
124 |
Correct |
998 ms |
75244 KB |
Output is correct |