#include<iostream>
#include<vector>
#include<set>
using namespace std;
void dfs(int i, int p, int d, vector<vector<int>>& vec, vector<vector<int>>& g) {
vec[d].push_back(i);
for (int j : g[i]) {
if (j != p) dfs(j, i, d + 1, vec, g);
}
}
vector<int> find(int N, int M, vector<int> A, vector<int> B, vector<int> W) {
vector<int> res(N, -1);
vector<vector<int>> g(N);
for (int i = 0; i < N - 1; i++) {
g[A[i] - 1].push_back(B[i] - 1);
g[B[i] - 1].push_back(A[i] - 1);
}
for (int i = 0; i < N; i++) {
vector<vector<int>> vec(N);
dfs(i, -1, 0, vec, g);
set<int> st;
for (int j = 1; j < N; j++) if (vec[j].size() == 1) st.insert(W[vec[j][0]]);
res[i] = st.size();
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> A(N - 1), B(N - 1);
for (int i = 0; i < N - 1; i++) {
cin >> A[i] >> B[i];
}
vector<int> W(N);
for (int i = 0; i < N; i++) {
cin >> W[i];
}
vector<int> ans = find(N, M, A, B, W);
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
96 ms |
600 KB |
Output is correct |
3 |
Correct |
83 ms |
676 KB |
Output is correct |
4 |
Correct |
145 ms |
604 KB |
Output is correct |
5 |
Correct |
109 ms |
708 KB |
Output is correct |
6 |
Correct |
219 ms |
1132 KB |
Output is correct |
7 |
Correct |
157 ms |
780 KB |
Output is correct |
8 |
Correct |
53 ms |
856 KB |
Output is correct |
9 |
Correct |
155 ms |
848 KB |
Output is correct |
10 |
Correct |
135 ms |
704 KB |
Output is correct |
11 |
Correct |
143 ms |
600 KB |
Output is correct |
12 |
Correct |
36 ms |
968 KB |
Output is correct |
13 |
Correct |
227 ms |
868 KB |
Output is correct |
14 |
Correct |
164 ms |
752 KB |
Output is correct |
15 |
Correct |
177 ms |
604 KB |
Output is correct |
16 |
Correct |
32 ms |
600 KB |
Output is correct |
17 |
Correct |
157 ms |
860 KB |
Output is correct |
18 |
Correct |
141 ms |
780 KB |
Output is correct |
19 |
Correct |
106 ms |
600 KB |
Output is correct |
20 |
Correct |
376 ms |
944 KB |
Output is correct |
21 |
Correct |
191 ms |
604 KB |
Output is correct |
22 |
Correct |
63 ms |
692 KB |
Output is correct |
23 |
Correct |
161 ms |
604 KB |
Output is correct |
24 |
Correct |
122 ms |
708 KB |
Output is correct |
25 |
Correct |
125 ms |
700 KB |
Output is correct |
26 |
Correct |
37 ms |
720 KB |
Output is correct |
27 |
Correct |
319 ms |
880 KB |
Output is correct |
28 |
Correct |
204 ms |
860 KB |
Output is correct |
29 |
Correct |
159 ms |
772 KB |
Output is correct |
30 |
Correct |
36 ms |
604 KB |
Output is correct |
31 |
Correct |
209 ms |
864 KB |
Output is correct |
32 |
Correct |
178 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2017 ms |
15080 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2094 ms |
21564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
96 ms |
600 KB |
Output is correct |
3 |
Correct |
83 ms |
676 KB |
Output is correct |
4 |
Correct |
145 ms |
604 KB |
Output is correct |
5 |
Correct |
109 ms |
708 KB |
Output is correct |
6 |
Correct |
219 ms |
1132 KB |
Output is correct |
7 |
Correct |
157 ms |
780 KB |
Output is correct |
8 |
Correct |
53 ms |
856 KB |
Output is correct |
9 |
Correct |
155 ms |
848 KB |
Output is correct |
10 |
Correct |
135 ms |
704 KB |
Output is correct |
11 |
Correct |
143 ms |
600 KB |
Output is correct |
12 |
Correct |
36 ms |
968 KB |
Output is correct |
13 |
Correct |
227 ms |
868 KB |
Output is correct |
14 |
Correct |
164 ms |
752 KB |
Output is correct |
15 |
Correct |
177 ms |
604 KB |
Output is correct |
16 |
Correct |
32 ms |
600 KB |
Output is correct |
17 |
Correct |
157 ms |
860 KB |
Output is correct |
18 |
Correct |
141 ms |
780 KB |
Output is correct |
19 |
Correct |
106 ms |
600 KB |
Output is correct |
20 |
Correct |
376 ms |
944 KB |
Output is correct |
21 |
Correct |
191 ms |
604 KB |
Output is correct |
22 |
Correct |
63 ms |
692 KB |
Output is correct |
23 |
Correct |
161 ms |
604 KB |
Output is correct |
24 |
Correct |
122 ms |
708 KB |
Output is correct |
25 |
Correct |
125 ms |
700 KB |
Output is correct |
26 |
Correct |
37 ms |
720 KB |
Output is correct |
27 |
Correct |
319 ms |
880 KB |
Output is correct |
28 |
Correct |
204 ms |
860 KB |
Output is correct |
29 |
Correct |
159 ms |
772 KB |
Output is correct |
30 |
Correct |
36 ms |
604 KB |
Output is correct |
31 |
Correct |
209 ms |
864 KB |
Output is correct |
32 |
Correct |
178 ms |
604 KB |
Output is correct |
33 |
Execution timed out |
2017 ms |
15080 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |