#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (1 << 20) + 12, MOD = (int)1e9 + 7;
int n, m, a[N], vis[N], timer, p[N];
vector<int> g[N];
int f(int d) {
timer++;
vis[d] = timer;
int lst = d;
queue<int> q;
q.push(d);
while(!q.empty()) {
int v = q.front();
lst = v;
q.pop();
for(int to : g[v]) {
if(vis[to] != timer) {
q.push(to);
p[to] = v;
vis[to] = timer;
}
}
}
int x = lst;
timer++;
vis[d] = timer;
while(x != d) {
vis[x] = timer;
x = p[x];
}
return lst;
}
int dist[N], col[N];
void test() {
cin >> n >> m;
for(int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1; i <= n; i++)
cin >> a[i];
int d = f(1), d1 = f(d);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
dist[j] = col[j] = 0;
}
dist[i] = 1;
queue<int> q;
q.push(i);
set<int> st;
while(!q.empty()) {
int v = q.front();
q.pop();
col[dist[v]]++;
for(int to : g[v]) {
if(dist[to] == 0) {
dist[to] = dist[v] + 1;
q.push(to);
}
}
}
for(int j = 1; j <= n; j++) if(j != i) {
if(col[dist[j]] == 1) {
st.insert(a[j]);
}
}
cout << (int)st.size() << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
Compilation message
joi2019_ho_t5.cpp: In function 'void test()':
joi2019_ho_t5.cpp:49:19: warning: unused variable 'd1' [-Wunused-variable]
49 | int d = f(1), d1 = f(d);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31240 KB |
Output is correct |
2 |
Correct |
44 ms |
31312 KB |
Output is correct |
3 |
Correct |
48 ms |
31312 KB |
Output is correct |
4 |
Correct |
52 ms |
31368 KB |
Output is correct |
5 |
Correct |
50 ms |
25244 KB |
Output is correct |
6 |
Correct |
104 ms |
25244 KB |
Output is correct |
7 |
Correct |
53 ms |
31312 KB |
Output is correct |
8 |
Correct |
35 ms |
26960 KB |
Output is correct |
9 |
Correct |
52 ms |
26960 KB |
Output is correct |
10 |
Correct |
46 ms |
26976 KB |
Output is correct |
11 |
Correct |
51 ms |
31312 KB |
Output is correct |
12 |
Correct |
33 ms |
31364 KB |
Output is correct |
13 |
Correct |
91 ms |
27216 KB |
Output is correct |
14 |
Correct |
48 ms |
27288 KB |
Output is correct |
15 |
Correct |
50 ms |
29324 KB |
Output is correct |
16 |
Correct |
27 ms |
31380 KB |
Output is correct |
17 |
Correct |
63 ms |
31312 KB |
Output is correct |
18 |
Correct |
55 ms |
31356 KB |
Output is correct |
19 |
Correct |
48 ms |
26960 KB |
Output is correct |
20 |
Correct |
212 ms |
25168 KB |
Output is correct |
21 |
Correct |
77 ms |
31312 KB |
Output is correct |
22 |
Correct |
46 ms |
25112 KB |
Output is correct |
23 |
Correct |
61 ms |
27120 KB |
Output is correct |
24 |
Correct |
43 ms |
27132 KB |
Output is correct |
25 |
Correct |
48 ms |
31312 KB |
Output is correct |
26 |
Correct |
36 ms |
31376 KB |
Output is correct |
27 |
Correct |
150 ms |
27160 KB |
Output is correct |
28 |
Correct |
115 ms |
31312 KB |
Output is correct |
29 |
Correct |
59 ms |
27140 KB |
Output is correct |
30 |
Correct |
29 ms |
26960 KB |
Output is correct |
31 |
Correct |
98 ms |
26960 KB |
Output is correct |
32 |
Correct |
68 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2031 ms |
34376 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2045 ms |
47944 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31240 KB |
Output is correct |
2 |
Correct |
44 ms |
31312 KB |
Output is correct |
3 |
Correct |
48 ms |
31312 KB |
Output is correct |
4 |
Correct |
52 ms |
31368 KB |
Output is correct |
5 |
Correct |
50 ms |
25244 KB |
Output is correct |
6 |
Correct |
104 ms |
25244 KB |
Output is correct |
7 |
Correct |
53 ms |
31312 KB |
Output is correct |
8 |
Correct |
35 ms |
26960 KB |
Output is correct |
9 |
Correct |
52 ms |
26960 KB |
Output is correct |
10 |
Correct |
46 ms |
26976 KB |
Output is correct |
11 |
Correct |
51 ms |
31312 KB |
Output is correct |
12 |
Correct |
33 ms |
31364 KB |
Output is correct |
13 |
Correct |
91 ms |
27216 KB |
Output is correct |
14 |
Correct |
48 ms |
27288 KB |
Output is correct |
15 |
Correct |
50 ms |
29324 KB |
Output is correct |
16 |
Correct |
27 ms |
31380 KB |
Output is correct |
17 |
Correct |
63 ms |
31312 KB |
Output is correct |
18 |
Correct |
55 ms |
31356 KB |
Output is correct |
19 |
Correct |
48 ms |
26960 KB |
Output is correct |
20 |
Correct |
212 ms |
25168 KB |
Output is correct |
21 |
Correct |
77 ms |
31312 KB |
Output is correct |
22 |
Correct |
46 ms |
25112 KB |
Output is correct |
23 |
Correct |
61 ms |
27120 KB |
Output is correct |
24 |
Correct |
43 ms |
27132 KB |
Output is correct |
25 |
Correct |
48 ms |
31312 KB |
Output is correct |
26 |
Correct |
36 ms |
31376 KB |
Output is correct |
27 |
Correct |
150 ms |
27160 KB |
Output is correct |
28 |
Correct |
115 ms |
31312 KB |
Output is correct |
29 |
Correct |
59 ms |
27140 KB |
Output is correct |
30 |
Correct |
29 ms |
26960 KB |
Output is correct |
31 |
Correct |
98 ms |
26960 KB |
Output is correct |
32 |
Correct |
68 ms |
26960 KB |
Output is correct |
33 |
Execution timed out |
2031 ms |
34376 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |