#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define bas(x) #x << ": " << x << " "
#define prarr(x, n) cout << #x << ": "; for (int qsd = 0; qsd < n; qsd++) cout << x[qsd] << " "; cout << endl;
#define prarrv(x) cout << #x << ": "; for (int qsd = 0; qsd < (int)x.size(); qsd++) cout << x[qsd] << " "; cout << endl;
#define inside sl<=l&&r<=sr
#define outside sr<l||r<sl
using ll = long long;
int n, k;
list<int> adj[500005];
int ilk[500005];
int son[500005];
list<int> st[500005];
int dfs(int node, int ata, int state){
//cout << "dfs(" << node << ", " << ata << ", " << state << ")" << endl;
int ret = (son[ilk[node]] == son[state]);
for (int ch: adj[node]){
if (ch == ata) continue;
if (dfs(ch, node, state)){
ret = 1;
if (son[ilk[node]] > state) son[state] = son[ilk[node]];
}
}
return ret;
}
int main(){
cin >> n >> k;
for (int i = 0; i < n-1; i++){
int u, v;
cin >> u >> v;
u--; v--;
adj[u].pb(v);
adj[v].pb(u);
}
for (int i = 0; i < n; i++){
cin >> ilk[i];
ilk[i]--;
st[ilk[i]].pb(i);
}
for (int i = 0 ; i < k; i++) son[i] = i;
for (int i = 0; i < k; i++){
if (st[i].size() != 0) dfs(*st[i].begin(), -1, i);
}
int ans = 0;
vector<int> say(k, 0);
for (int i = 0; i < n; i++){
for (int j: adj[i]){
if (son[ilk[i]] != son[ilk[j]]){
//cout << "artir" << bas(i) << bas(j) << endl;
say[son[ilk[i]]]++;
//say[son[ilk[j]]]++;
}
}
}
//for (int i = 0; i < n; i++) cout << son[ilk[i]] << " "; cout << endl;
for (int i = 0; i < k; i++) if (say[i] == 1) ans++;
cout << (ans+1)/2 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
28 ms |
23800 KB |
Output is correct |
3 |
Correct |
28 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23792 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
24 ms |
23824 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
24 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23804 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23848 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23880 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
28 ms |
23800 KB |
Output is correct |
3 |
Correct |
28 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23792 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
24 ms |
23824 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
24 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23804 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23848 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23880 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
24 ms |
23800 KB |
Output is correct |
26 |
Incorrect |
181 ms |
24184 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
28 ms |
23800 KB |
Output is correct |
3 |
Correct |
28 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23792 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
24 ms |
23824 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
24 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23804 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23848 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23880 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
24 ms |
23800 KB |
Output is correct |
26 |
Correct |
290 ms |
35080 KB |
Output is correct |
27 |
Correct |
581 ms |
34956 KB |
Output is correct |
28 |
Correct |
32 ms |
24184 KB |
Output is correct |
29 |
Correct |
24 ms |
23800 KB |
Output is correct |
30 |
Correct |
24 ms |
23800 KB |
Output is correct |
31 |
Correct |
614 ms |
35064 KB |
Output is correct |
32 |
Correct |
29 ms |
24184 KB |
Output is correct |
33 |
Correct |
1278 ms |
42900 KB |
Output is correct |
34 |
Incorrect |
547 ms |
35176 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
33564 KB |
Output is correct |
2 |
Execution timed out |
3043 ms |
34008 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
28 ms |
23800 KB |
Output is correct |
3 |
Correct |
28 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23792 KB |
Output is correct |
5 |
Correct |
23 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
24 ms |
23824 KB |
Output is correct |
10 |
Correct |
24 ms |
23800 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
24 ms |
23800 KB |
Output is correct |
13 |
Correct |
23 ms |
23804 KB |
Output is correct |
14 |
Correct |
24 ms |
23800 KB |
Output is correct |
15 |
Correct |
24 ms |
23848 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
23 ms |
23800 KB |
Output is correct |
19 |
Correct |
24 ms |
23928 KB |
Output is correct |
20 |
Correct |
24 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23880 KB |
Output is correct |
22 |
Correct |
24 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
24 ms |
23800 KB |
Output is correct |
26 |
Incorrect |
181 ms |
24184 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |