/* IN THE NAME OF GOD */
/* |\/| /-\ |\| | |\/| /-\ */
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int)x.size()
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define mr make_pair
#define int long long
#define pii pair<int, int>
typedef long double ld;
typedef long long ll;
mt19937 rng (chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 5e5 + 10;
const ll MOD = 1e9 + 9;
const ll inf = 2e17;
const ll INF = 7e15;
vector<int> g[N];
int cnt[N], s[N], sum[N], k, T, par[N][20], h[N], tin[N], tout[N], f[N], d[N];
vector<int> col[N];
void dfs(int v, int p){
for(int i = 1; i < 20; i++)
par[v][i] = par[par[v][i - 1]][i - 1];
tin[v] = ++T;
for(int u : g[v]){
if(u == p)
continue;
par[u][0] = v;
h[u] = h[v] + 1;
dfs(u, v);
}
tout[v] = ++T;
}
int lca(int u, int v){
if(h[u] > h[v])
swap(u, v);
int H = h[v] - h[u];
for(int i = 0; i < 20; i++){
if(H & (1 << i))
v = par[v][i];
}
if(v == u)
return v;
for(int i = 19; i >= 0; i--){
if(par[v][i] != par[u][i])
v = par[v][i], u = par[u][i];
}
return par[v][0];
}
void moorgh(int v, int p){
for(int u : g[v]){
if(u == p)
continue;
moorgh(u, v);
sum[v] += sum[u];
}
sum[v] += f[v];
}
int32_t main(){
ios_base:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n >> k;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; i++){
cin >> s[i];
col[s[i]].pb(i);
cnt[s[i]]++;
}
dfs(1, 0);
vector<pii> st;
for(int i = 1; i <= k; i++){
if(sz(col[i]) == 0)
continue;
int root = col[i][0];
for(int v : col[i])
root = lca(root, v);
for(int v : col[i]){
f[root]--;
f[v]++;
}
}
moorgh(1, 0);
vector<pii> vec;
for(int i = 2; i <= n; i++){
if(sum[i] == 0){
vec.pb(mr(tin[i], i));
vec.pb(mr(tin[par[i][0]], par[i][0]));
}
}
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
st.clear();
for(pii v : vec)
st.pb(v);
for(int i = 0; i < sz(vec) - 1; i++)
st.pb(mr(tin[lca(vec[i].S, vec[i + 1].S)], lca(vec[i].S, vec[i + 1].S)));
sort(all(st));
st.resize(unique(all(st)) - st.begin());
vector<int> V;
for(pii v : st) {
while(!V.empty() && tout[V.back()] < tout[v.S])
V.pop_back();
if(!V.empty()){
d[v.S]++;
d[V.back()]++;
}
V.pb(v.S);
}
int t = 0;
for(pii v : st){
if(d[v.S] == 1)
t++;
}
cout << t / 2 + (t % 2);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23776 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23844 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23788 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23812 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
12 ms |
23828 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23776 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23844 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23788 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23812 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
12 ms |
23828 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23788 KB |
Output is correct |
26 |
Correct |
14 ms |
24844 KB |
Output is correct |
27 |
Correct |
14 ms |
24660 KB |
Output is correct |
28 |
Correct |
16 ms |
24984 KB |
Output is correct |
29 |
Correct |
15 ms |
25044 KB |
Output is correct |
30 |
Correct |
14 ms |
24660 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24916 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24532 KB |
Output is correct |
35 |
Correct |
15 ms |
25028 KB |
Output is correct |
36 |
Correct |
14 ms |
24660 KB |
Output is correct |
37 |
Correct |
14 ms |
24752 KB |
Output is correct |
38 |
Correct |
11 ms |
23892 KB |
Output is correct |
39 |
Correct |
14 ms |
24760 KB |
Output is correct |
40 |
Correct |
13 ms |
24660 KB |
Output is correct |
41 |
Correct |
13 ms |
24684 KB |
Output is correct |
42 |
Correct |
13 ms |
24660 KB |
Output is correct |
43 |
Correct |
13 ms |
24772 KB |
Output is correct |
44 |
Correct |
13 ms |
23896 KB |
Output is correct |
45 |
Correct |
15 ms |
24788 KB |
Output is correct |
46 |
Correct |
15 ms |
24644 KB |
Output is correct |
47 |
Correct |
12 ms |
23892 KB |
Output is correct |
48 |
Correct |
14 ms |
24748 KB |
Output is correct |
49 |
Correct |
14 ms |
25004 KB |
Output is correct |
50 |
Correct |
15 ms |
25044 KB |
Output is correct |
51 |
Correct |
14 ms |
24660 KB |
Output is correct |
52 |
Correct |
14 ms |
24676 KB |
Output is correct |
53 |
Correct |
16 ms |
24660 KB |
Output is correct |
54 |
Correct |
14 ms |
24532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23776 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23844 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23788 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23812 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
12 ms |
23828 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23892 KB |
Output is correct |
26 |
Correct |
81 ms |
49264 KB |
Output is correct |
27 |
Correct |
111 ms |
49016 KB |
Output is correct |
28 |
Correct |
15 ms |
24660 KB |
Output is correct |
29 |
Correct |
13 ms |
23892 KB |
Output is correct |
30 |
Correct |
12 ms |
23892 KB |
Output is correct |
31 |
Correct |
109 ms |
49212 KB |
Output is correct |
32 |
Correct |
13 ms |
24660 KB |
Output is correct |
33 |
Correct |
133 ms |
54092 KB |
Output is correct |
34 |
Correct |
108 ms |
49032 KB |
Output is correct |
35 |
Correct |
14 ms |
24568 KB |
Output is correct |
36 |
Correct |
129 ms |
49252 KB |
Output is correct |
37 |
Correct |
14 ms |
24640 KB |
Output is correct |
38 |
Correct |
13 ms |
24660 KB |
Output is correct |
39 |
Correct |
80 ms |
49160 KB |
Output is correct |
40 |
Correct |
14 ms |
24700 KB |
Output is correct |
41 |
Correct |
104 ms |
48840 KB |
Output is correct |
42 |
Correct |
135 ms |
50296 KB |
Output is correct |
43 |
Correct |
12 ms |
23892 KB |
Output is correct |
44 |
Correct |
129 ms |
54192 KB |
Output is correct |
45 |
Correct |
140 ms |
52640 KB |
Output is correct |
46 |
Correct |
14 ms |
24656 KB |
Output is correct |
47 |
Correct |
14 ms |
24560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
49224 KB |
Output is correct |
2 |
Correct |
131 ms |
61236 KB |
Output is correct |
3 |
Correct |
15 ms |
24916 KB |
Output is correct |
4 |
Correct |
14 ms |
24660 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
14 ms |
24612 KB |
Output is correct |
8 |
Correct |
137 ms |
51932 KB |
Output is correct |
9 |
Correct |
14 ms |
24660 KB |
Output is correct |
10 |
Correct |
110 ms |
49968 KB |
Output is correct |
11 |
Correct |
12 ms |
23892 KB |
Output is correct |
12 |
Correct |
126 ms |
49452 KB |
Output is correct |
13 |
Correct |
138 ms |
52884 KB |
Output is correct |
14 |
Correct |
170 ms |
61120 KB |
Output is correct |
15 |
Correct |
81 ms |
49160 KB |
Output is correct |
16 |
Correct |
14 ms |
24760 KB |
Output is correct |
17 |
Correct |
12 ms |
23892 KB |
Output is correct |
18 |
Correct |
132 ms |
60332 KB |
Output is correct |
19 |
Correct |
191 ms |
63732 KB |
Output is correct |
20 |
Correct |
14 ms |
24660 KB |
Output is correct |
21 |
Correct |
14 ms |
23900 KB |
Output is correct |
22 |
Correct |
108 ms |
52396 KB |
Output is correct |
23 |
Correct |
15 ms |
24720 KB |
Output is correct |
24 |
Correct |
130 ms |
51104 KB |
Output is correct |
25 |
Correct |
187 ms |
63416 KB |
Output is correct |
26 |
Correct |
15 ms |
24984 KB |
Output is correct |
27 |
Correct |
15 ms |
25080 KB |
Output is correct |
28 |
Correct |
15 ms |
24744 KB |
Output is correct |
29 |
Correct |
13 ms |
24632 KB |
Output is correct |
30 |
Correct |
14 ms |
24660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23892 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23776 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23844 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23784 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23788 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
14 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23856 KB |
Output is correct |
18 |
Correct |
11 ms |
23812 KB |
Output is correct |
19 |
Correct |
11 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23808 KB |
Output is correct |
22 |
Correct |
12 ms |
23828 KB |
Output is correct |
23 |
Correct |
12 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
12 ms |
23788 KB |
Output is correct |
26 |
Correct |
14 ms |
24844 KB |
Output is correct |
27 |
Correct |
14 ms |
24660 KB |
Output is correct |
28 |
Correct |
16 ms |
24984 KB |
Output is correct |
29 |
Correct |
15 ms |
25044 KB |
Output is correct |
30 |
Correct |
14 ms |
24660 KB |
Output is correct |
31 |
Correct |
12 ms |
23764 KB |
Output is correct |
32 |
Correct |
14 ms |
24916 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
13 ms |
24532 KB |
Output is correct |
35 |
Correct |
15 ms |
25028 KB |
Output is correct |
36 |
Correct |
14 ms |
24660 KB |
Output is correct |
37 |
Correct |
14 ms |
24752 KB |
Output is correct |
38 |
Correct |
11 ms |
23892 KB |
Output is correct |
39 |
Correct |
14 ms |
24760 KB |
Output is correct |
40 |
Correct |
13 ms |
24660 KB |
Output is correct |
41 |
Correct |
13 ms |
24684 KB |
Output is correct |
42 |
Correct |
13 ms |
24660 KB |
Output is correct |
43 |
Correct |
13 ms |
24772 KB |
Output is correct |
44 |
Correct |
13 ms |
23896 KB |
Output is correct |
45 |
Correct |
15 ms |
24788 KB |
Output is correct |
46 |
Correct |
15 ms |
24644 KB |
Output is correct |
47 |
Correct |
12 ms |
23892 KB |
Output is correct |
48 |
Correct |
14 ms |
24748 KB |
Output is correct |
49 |
Correct |
14 ms |
25004 KB |
Output is correct |
50 |
Correct |
15 ms |
25044 KB |
Output is correct |
51 |
Correct |
14 ms |
24660 KB |
Output is correct |
52 |
Correct |
14 ms |
24676 KB |
Output is correct |
53 |
Correct |
16 ms |
24660 KB |
Output is correct |
54 |
Correct |
14 ms |
24532 KB |
Output is correct |
55 |
Correct |
12 ms |
23892 KB |
Output is correct |
56 |
Correct |
81 ms |
49264 KB |
Output is correct |
57 |
Correct |
111 ms |
49016 KB |
Output is correct |
58 |
Correct |
15 ms |
24660 KB |
Output is correct |
59 |
Correct |
13 ms |
23892 KB |
Output is correct |
60 |
Correct |
12 ms |
23892 KB |
Output is correct |
61 |
Correct |
109 ms |
49212 KB |
Output is correct |
62 |
Correct |
13 ms |
24660 KB |
Output is correct |
63 |
Correct |
133 ms |
54092 KB |
Output is correct |
64 |
Correct |
108 ms |
49032 KB |
Output is correct |
65 |
Correct |
14 ms |
24568 KB |
Output is correct |
66 |
Correct |
129 ms |
49252 KB |
Output is correct |
67 |
Correct |
14 ms |
24640 KB |
Output is correct |
68 |
Correct |
13 ms |
24660 KB |
Output is correct |
69 |
Correct |
80 ms |
49160 KB |
Output is correct |
70 |
Correct |
14 ms |
24700 KB |
Output is correct |
71 |
Correct |
104 ms |
48840 KB |
Output is correct |
72 |
Correct |
135 ms |
50296 KB |
Output is correct |
73 |
Correct |
12 ms |
23892 KB |
Output is correct |
74 |
Correct |
129 ms |
54192 KB |
Output is correct |
75 |
Correct |
140 ms |
52640 KB |
Output is correct |
76 |
Correct |
14 ms |
24656 KB |
Output is correct |
77 |
Correct |
14 ms |
24560 KB |
Output is correct |
78 |
Correct |
82 ms |
49224 KB |
Output is correct |
79 |
Correct |
131 ms |
61236 KB |
Output is correct |
80 |
Correct |
15 ms |
24916 KB |
Output is correct |
81 |
Correct |
14 ms |
24660 KB |
Output is correct |
82 |
Correct |
12 ms |
23764 KB |
Output is correct |
83 |
Correct |
12 ms |
23764 KB |
Output is correct |
84 |
Correct |
14 ms |
24612 KB |
Output is correct |
85 |
Correct |
137 ms |
51932 KB |
Output is correct |
86 |
Correct |
14 ms |
24660 KB |
Output is correct |
87 |
Correct |
110 ms |
49968 KB |
Output is correct |
88 |
Correct |
12 ms |
23892 KB |
Output is correct |
89 |
Correct |
126 ms |
49452 KB |
Output is correct |
90 |
Correct |
138 ms |
52884 KB |
Output is correct |
91 |
Correct |
170 ms |
61120 KB |
Output is correct |
92 |
Correct |
81 ms |
49160 KB |
Output is correct |
93 |
Correct |
14 ms |
24760 KB |
Output is correct |
94 |
Correct |
12 ms |
23892 KB |
Output is correct |
95 |
Correct |
132 ms |
60332 KB |
Output is correct |
96 |
Correct |
191 ms |
63732 KB |
Output is correct |
97 |
Correct |
14 ms |
24660 KB |
Output is correct |
98 |
Correct |
14 ms |
23900 KB |
Output is correct |
99 |
Correct |
108 ms |
52396 KB |
Output is correct |
100 |
Correct |
15 ms |
24720 KB |
Output is correct |
101 |
Correct |
130 ms |
51104 KB |
Output is correct |
102 |
Correct |
187 ms |
63416 KB |
Output is correct |
103 |
Correct |
15 ms |
24984 KB |
Output is correct |
104 |
Correct |
15 ms |
25080 KB |
Output is correct |
105 |
Correct |
15 ms |
24744 KB |
Output is correct |
106 |
Correct |
13 ms |
24632 KB |
Output is correct |
107 |
Correct |
14 ms |
24660 KB |
Output is correct |
108 |
Correct |
615 ms |
164068 KB |
Output is correct |
109 |
Correct |
876 ms |
160140 KB |
Output is correct |
110 |
Correct |
872 ms |
169020 KB |
Output is correct |
111 |
Correct |
1269 ms |
218716 KB |
Output is correct |
112 |
Correct |
1250 ms |
208884 KB |
Output is correct |
113 |
Correct |
830 ms |
199932 KB |
Output is correct |
114 |
Correct |
680 ms |
148348 KB |
Output is correct |
115 |
Correct |
646 ms |
148304 KB |
Output is correct |
116 |
Correct |
856 ms |
156548 KB |
Output is correct |
117 |
Correct |
1187 ms |
204092 KB |
Output is correct |
118 |
Correct |
660 ms |
151628 KB |
Output is correct |
119 |
Correct |
1145 ms |
204220 KB |
Output is correct |
120 |
Correct |
1300 ms |
216776 KB |
Output is correct |
121 |
Correct |
1159 ms |
204212 KB |
Output is correct |
122 |
Correct |
952 ms |
168300 KB |
Output is correct |
123 |
Correct |
878 ms |
205004 KB |
Output is correct |
124 |
Correct |
1113 ms |
193064 KB |
Output is correct |