#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]];
else son[ilk[node]] = son[state];
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23868 KB |
Output is correct |
6 |
Correct |
23 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23772 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23768 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
24 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23928 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
23 ms |
23800 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23824 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23868 KB |
Output is correct |
6 |
Correct |
23 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23772 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23768 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
24 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23928 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
23 ms |
23800 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23824 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
23 ms |
23800 KB |
Output is correct |
26 |
Correct |
197 ms |
24184 KB |
Output is correct |
27 |
Correct |
33 ms |
24188 KB |
Output is correct |
28 |
Correct |
315 ms |
24316 KB |
Output is correct |
29 |
Correct |
242 ms |
24184 KB |
Output is correct |
30 |
Correct |
44 ms |
24184 KB |
Output is correct |
31 |
Correct |
24 ms |
23800 KB |
Output is correct |
32 |
Correct |
413 ms |
24444 KB |
Output is correct |
33 |
Correct |
23 ms |
23800 KB |
Output is correct |
34 |
Correct |
30 ms |
24188 KB |
Output is correct |
35 |
Correct |
240 ms |
24184 KB |
Output is correct |
36 |
Correct |
32 ms |
24184 KB |
Output is correct |
37 |
Correct |
136 ms |
24192 KB |
Output is correct |
38 |
Correct |
23 ms |
23800 KB |
Output is correct |
39 |
Correct |
128 ms |
24196 KB |
Output is correct |
40 |
Correct |
28 ms |
24184 KB |
Output is correct |
41 |
Correct |
29 ms |
24184 KB |
Output is correct |
42 |
Correct |
75 ms |
24184 KB |
Output is correct |
43 |
Correct |
31 ms |
24444 KB |
Output is correct |
44 |
Correct |
24 ms |
23800 KB |
Output is correct |
45 |
Correct |
352 ms |
24312 KB |
Output is correct |
46 |
Correct |
138 ms |
24196 KB |
Output is correct |
47 |
Correct |
23 ms |
23800 KB |
Output is correct |
48 |
Correct |
74 ms |
24184 KB |
Output is correct |
49 |
Correct |
110 ms |
24184 KB |
Output is correct |
50 |
Correct |
314 ms |
24316 KB |
Output is correct |
51 |
Correct |
81 ms |
24192 KB |
Output is correct |
52 |
Correct |
30 ms |
24184 KB |
Output is correct |
53 |
Correct |
67 ms |
24184 KB |
Output is correct |
54 |
Correct |
33 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23868 KB |
Output is correct |
6 |
Correct |
23 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23772 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23768 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
24 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23928 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
23 ms |
23800 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23824 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
23 ms |
23800 KB |
Output is correct |
26 |
Correct |
322 ms |
33716 KB |
Output is correct |
27 |
Correct |
625 ms |
33644 KB |
Output is correct |
28 |
Correct |
33 ms |
24056 KB |
Output is correct |
29 |
Correct |
23 ms |
23800 KB |
Output is correct |
30 |
Correct |
23 ms |
23800 KB |
Output is correct |
31 |
Correct |
568 ms |
33676 KB |
Output is correct |
32 |
Correct |
30 ms |
24060 KB |
Output is correct |
33 |
Correct |
1242 ms |
41368 KB |
Output is correct |
34 |
Correct |
538 ms |
33648 KB |
Output is correct |
35 |
Correct |
34 ms |
24184 KB |
Output is correct |
36 |
Correct |
1026 ms |
36012 KB |
Output is correct |
37 |
Correct |
27 ms |
24184 KB |
Output is correct |
38 |
Correct |
28 ms |
24184 KB |
Output is correct |
39 |
Correct |
321 ms |
34980 KB |
Output is correct |
40 |
Correct |
28 ms |
24312 KB |
Output is correct |
41 |
Correct |
226 ms |
34972 KB |
Output is correct |
42 |
Correct |
1130 ms |
39884 KB |
Output is correct |
43 |
Correct |
23 ms |
23800 KB |
Output is correct |
44 |
Correct |
252 ms |
42564 KB |
Output is correct |
45 |
Correct |
840 ms |
38904 KB |
Output is correct |
46 |
Correct |
30 ms |
24184 KB |
Output is correct |
47 |
Correct |
33 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
283 ms |
33640 KB |
Output is correct |
2 |
Execution timed out |
3033 ms |
33912 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23804 KB |
Output is correct |
2 |
Correct |
24 ms |
23800 KB |
Output is correct |
3 |
Correct |
23 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
23 ms |
23868 KB |
Output is correct |
6 |
Correct |
23 ms |
23804 KB |
Output is correct |
7 |
Correct |
24 ms |
23772 KB |
Output is correct |
8 |
Correct |
23 ms |
23800 KB |
Output is correct |
9 |
Correct |
23 ms |
23768 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
24 ms |
23800 KB |
Output is correct |
12 |
Correct |
23 ms |
23928 KB |
Output is correct |
13 |
Correct |
23 ms |
23800 KB |
Output is correct |
14 |
Correct |
23 ms |
23800 KB |
Output is correct |
15 |
Correct |
23 ms |
23800 KB |
Output is correct |
16 |
Correct |
24 ms |
23800 KB |
Output is correct |
17 |
Correct |
23 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23800 KB |
Output is correct |
19 |
Correct |
23 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
23824 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
23 ms |
23800 KB |
Output is correct |
23 |
Correct |
23 ms |
23800 KB |
Output is correct |
24 |
Correct |
23 ms |
23800 KB |
Output is correct |
25 |
Correct |
23 ms |
23800 KB |
Output is correct |
26 |
Correct |
197 ms |
24184 KB |
Output is correct |
27 |
Correct |
33 ms |
24188 KB |
Output is correct |
28 |
Correct |
315 ms |
24316 KB |
Output is correct |
29 |
Correct |
242 ms |
24184 KB |
Output is correct |
30 |
Correct |
44 ms |
24184 KB |
Output is correct |
31 |
Correct |
24 ms |
23800 KB |
Output is correct |
32 |
Correct |
413 ms |
24444 KB |
Output is correct |
33 |
Correct |
23 ms |
23800 KB |
Output is correct |
34 |
Correct |
30 ms |
24188 KB |
Output is correct |
35 |
Correct |
240 ms |
24184 KB |
Output is correct |
36 |
Correct |
32 ms |
24184 KB |
Output is correct |
37 |
Correct |
136 ms |
24192 KB |
Output is correct |
38 |
Correct |
23 ms |
23800 KB |
Output is correct |
39 |
Correct |
128 ms |
24196 KB |
Output is correct |
40 |
Correct |
28 ms |
24184 KB |
Output is correct |
41 |
Correct |
29 ms |
24184 KB |
Output is correct |
42 |
Correct |
75 ms |
24184 KB |
Output is correct |
43 |
Correct |
31 ms |
24444 KB |
Output is correct |
44 |
Correct |
24 ms |
23800 KB |
Output is correct |
45 |
Correct |
352 ms |
24312 KB |
Output is correct |
46 |
Correct |
138 ms |
24196 KB |
Output is correct |
47 |
Correct |
23 ms |
23800 KB |
Output is correct |
48 |
Correct |
74 ms |
24184 KB |
Output is correct |
49 |
Correct |
110 ms |
24184 KB |
Output is correct |
50 |
Correct |
314 ms |
24316 KB |
Output is correct |
51 |
Correct |
81 ms |
24192 KB |
Output is correct |
52 |
Correct |
30 ms |
24184 KB |
Output is correct |
53 |
Correct |
67 ms |
24184 KB |
Output is correct |
54 |
Correct |
33 ms |
24184 KB |
Output is correct |
55 |
Correct |
23 ms |
23800 KB |
Output is correct |
56 |
Correct |
322 ms |
33716 KB |
Output is correct |
57 |
Correct |
625 ms |
33644 KB |
Output is correct |
58 |
Correct |
33 ms |
24056 KB |
Output is correct |
59 |
Correct |
23 ms |
23800 KB |
Output is correct |
60 |
Correct |
23 ms |
23800 KB |
Output is correct |
61 |
Correct |
568 ms |
33676 KB |
Output is correct |
62 |
Correct |
30 ms |
24060 KB |
Output is correct |
63 |
Correct |
1242 ms |
41368 KB |
Output is correct |
64 |
Correct |
538 ms |
33648 KB |
Output is correct |
65 |
Correct |
34 ms |
24184 KB |
Output is correct |
66 |
Correct |
1026 ms |
36012 KB |
Output is correct |
67 |
Correct |
27 ms |
24184 KB |
Output is correct |
68 |
Correct |
28 ms |
24184 KB |
Output is correct |
69 |
Correct |
321 ms |
34980 KB |
Output is correct |
70 |
Correct |
28 ms |
24312 KB |
Output is correct |
71 |
Correct |
226 ms |
34972 KB |
Output is correct |
72 |
Correct |
1130 ms |
39884 KB |
Output is correct |
73 |
Correct |
23 ms |
23800 KB |
Output is correct |
74 |
Correct |
252 ms |
42564 KB |
Output is correct |
75 |
Correct |
840 ms |
38904 KB |
Output is correct |
76 |
Correct |
30 ms |
24184 KB |
Output is correct |
77 |
Correct |
33 ms |
24184 KB |
Output is correct |
78 |
Correct |
283 ms |
33640 KB |
Output is correct |
79 |
Execution timed out |
3033 ms |
33912 KB |
Time limit exceeded |
80 |
Halted |
0 ms |
0 KB |
- |