#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define X first
#define Y second
#define FOR(i,a,b) for(int i = a ; i < b ; ++i)
const int N = 2e5 + 1;
vector<int> g[N];
namespace Diameter {
int Max;
int fin;
int L, R;
int dfs(int u,int p,int d) {
if (Max < d) {
Max = d;
fin = u;
}
for(int v : g[u]) if (v != p)
dfs(v,u,d + 1);
return fin;
}
int LamViecCanLam() {
Max = fin = 0; L = dfs(1,0,0);
Max = fin = 0; R = dfs(L,0,0);
return 1;
}
};
using Diameter::L;
using Diameter::R;
array<int,2> MaxDep[N];
int dep[N], dig[N];
int ans[N], cnt[N];
int res = 0;
int c[N];
stack<int> st;
void add(int u) {
st.push(u);
cnt[c[u]]++;
res += (cnt[c[u]] == 1);
}
void rem(int h) {
while (st.size() && h <= dep[st.top()]) {
int x = st.top(); st.pop();
cnt[c[x]]--;
res -= (cnt[c[x]] == 0);
}
}
void upd(int u,int V) {
if (MaxDep[u][1] < V) MaxDep[u][1] = V;
if (MaxDep[u][1] > MaxDep[u][0])
swap(MaxDep[u][1],MaxDep[u][0]);
}
void dfs(int u,int p) {
rem(2 * dep[u] - MaxDep[u][1]);
add(u);
for(int v : g[u]) if (v == dig[u])
dfs(v,u);
rem(2 * dep[u] - MaxDep[u][0]);
ans[u] = max(ans[u],res);
for(int v : g[u]) if (v != p && v != dig[u]) {
if (st.empty() || st.top() != u)
add(u);
dfs(v,u);
}
rem(dep[u]);
}
int ini(int u,int p) {
dep[u] = dep[p] + 1;
dig[u] = u;
MaxDep[u][0] = dep[u];
MaxDep[u][1] = dep[u];
for(int v : g[u]) if (v != p) {
ini(v,u);
upd(u,MaxDep[v][0]);
if (MaxDep[u][0] == MaxDep[v][0])
dig[u] = v;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int m; cin >> m;
FOR(i,1,n) {
int x; cin >> x;
int y; cin >> y;
g[x].pb(y);
g[y].pb(x);
}
FOR(i,1,n + 1) cin >> c[i];
Diameter::LamViecCanLam();
ini(L,0); dfs(L,0);
ini(R,0); dfs(R,0);
FOR(i,1,n + 1)
cout << ans[i] << "\n";
}
/*
10 10
2 6
5 8
10 8
1 4
10 6
4 5
10 7
6 9
3 7
1 2 3 4 5 6 7 8 9 10
*/
Compilation message
joi2019_ho_t5.cpp: In function 'int ini(int, int)':
joi2019_ho_t5.cpp:105:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5240 KB |
Output is correct |
3 |
Correct |
7 ms |
5240 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
8 ms |
5240 KB |
Output is correct |
6 |
Correct |
7 ms |
5368 KB |
Output is correct |
7 |
Correct |
8 ms |
5240 KB |
Output is correct |
8 |
Correct |
8 ms |
5160 KB |
Output is correct |
9 |
Correct |
8 ms |
5152 KB |
Output is correct |
10 |
Correct |
8 ms |
5240 KB |
Output is correct |
11 |
Correct |
8 ms |
5240 KB |
Output is correct |
12 |
Correct |
7 ms |
5240 KB |
Output is correct |
13 |
Correct |
8 ms |
5368 KB |
Output is correct |
14 |
Correct |
8 ms |
5240 KB |
Output is correct |
15 |
Correct |
8 ms |
5292 KB |
Output is correct |
16 |
Correct |
7 ms |
5240 KB |
Output is correct |
17 |
Correct |
8 ms |
5368 KB |
Output is correct |
18 |
Correct |
8 ms |
5240 KB |
Output is correct |
19 |
Correct |
8 ms |
5240 KB |
Output is correct |
20 |
Correct |
8 ms |
5368 KB |
Output is correct |
21 |
Correct |
8 ms |
5368 KB |
Output is correct |
22 |
Correct |
8 ms |
5240 KB |
Output is correct |
23 |
Correct |
18 ms |
5240 KB |
Output is correct |
24 |
Correct |
8 ms |
5240 KB |
Output is correct |
25 |
Correct |
8 ms |
5240 KB |
Output is correct |
26 |
Correct |
8 ms |
5240 KB |
Output is correct |
27 |
Correct |
8 ms |
5368 KB |
Output is correct |
28 |
Correct |
8 ms |
5368 KB |
Output is correct |
29 |
Correct |
8 ms |
5368 KB |
Output is correct |
30 |
Correct |
8 ms |
5240 KB |
Output is correct |
31 |
Correct |
8 ms |
5368 KB |
Output is correct |
32 |
Correct |
20 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
13312 KB |
Output is correct |
2 |
Correct |
197 ms |
22776 KB |
Output is correct |
3 |
Correct |
36 ms |
8184 KB |
Output is correct |
4 |
Correct |
382 ms |
19616 KB |
Output is correct |
5 |
Correct |
505 ms |
35812 KB |
Output is correct |
6 |
Correct |
437 ms |
27464 KB |
Output is correct |
7 |
Correct |
332 ms |
19528 KB |
Output is correct |
8 |
Correct |
378 ms |
21140 KB |
Output is correct |
9 |
Correct |
375 ms |
20476 KB |
Output is correct |
10 |
Correct |
394 ms |
20476 KB |
Output is correct |
11 |
Correct |
208 ms |
20332 KB |
Output is correct |
12 |
Correct |
431 ms |
29392 KB |
Output is correct |
13 |
Correct |
372 ms |
27484 KB |
Output is correct |
14 |
Correct |
416 ms |
26996 KB |
Output is correct |
15 |
Correct |
194 ms |
20072 KB |
Output is correct |
16 |
Correct |
379 ms |
30708 KB |
Output is correct |
17 |
Correct |
464 ms |
27620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
17912 KB |
Output is correct |
2 |
Correct |
498 ms |
36696 KB |
Output is correct |
3 |
Correct |
43 ms |
8828 KB |
Output is correct |
4 |
Correct |
416 ms |
21204 KB |
Output is correct |
5 |
Correct |
511 ms |
38192 KB |
Output is correct |
6 |
Correct |
490 ms |
29420 KB |
Output is correct |
7 |
Correct |
384 ms |
21112 KB |
Output is correct |
8 |
Correct |
368 ms |
24704 KB |
Output is correct |
9 |
Correct |
387 ms |
23416 KB |
Output is correct |
10 |
Correct |
389 ms |
22348 KB |
Output is correct |
11 |
Correct |
311 ms |
21492 KB |
Output is correct |
12 |
Correct |
531 ms |
34984 KB |
Output is correct |
13 |
Correct |
435 ms |
28532 KB |
Output is correct |
14 |
Correct |
468 ms |
28544 KB |
Output is correct |
15 |
Correct |
245 ms |
21868 KB |
Output is correct |
16 |
Correct |
439 ms |
32908 KB |
Output is correct |
17 |
Correct |
436 ms |
29556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
8 ms |
5240 KB |
Output is correct |
3 |
Correct |
7 ms |
5240 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
8 ms |
5240 KB |
Output is correct |
6 |
Correct |
7 ms |
5368 KB |
Output is correct |
7 |
Correct |
8 ms |
5240 KB |
Output is correct |
8 |
Correct |
8 ms |
5160 KB |
Output is correct |
9 |
Correct |
8 ms |
5152 KB |
Output is correct |
10 |
Correct |
8 ms |
5240 KB |
Output is correct |
11 |
Correct |
8 ms |
5240 KB |
Output is correct |
12 |
Correct |
7 ms |
5240 KB |
Output is correct |
13 |
Correct |
8 ms |
5368 KB |
Output is correct |
14 |
Correct |
8 ms |
5240 KB |
Output is correct |
15 |
Correct |
8 ms |
5292 KB |
Output is correct |
16 |
Correct |
7 ms |
5240 KB |
Output is correct |
17 |
Correct |
8 ms |
5368 KB |
Output is correct |
18 |
Correct |
8 ms |
5240 KB |
Output is correct |
19 |
Correct |
8 ms |
5240 KB |
Output is correct |
20 |
Correct |
8 ms |
5368 KB |
Output is correct |
21 |
Correct |
8 ms |
5368 KB |
Output is correct |
22 |
Correct |
8 ms |
5240 KB |
Output is correct |
23 |
Correct |
18 ms |
5240 KB |
Output is correct |
24 |
Correct |
8 ms |
5240 KB |
Output is correct |
25 |
Correct |
8 ms |
5240 KB |
Output is correct |
26 |
Correct |
8 ms |
5240 KB |
Output is correct |
27 |
Correct |
8 ms |
5368 KB |
Output is correct |
28 |
Correct |
8 ms |
5368 KB |
Output is correct |
29 |
Correct |
8 ms |
5368 KB |
Output is correct |
30 |
Correct |
8 ms |
5240 KB |
Output is correct |
31 |
Correct |
8 ms |
5368 KB |
Output is correct |
32 |
Correct |
20 ms |
5240 KB |
Output is correct |
33 |
Correct |
171 ms |
13312 KB |
Output is correct |
34 |
Correct |
197 ms |
22776 KB |
Output is correct |
35 |
Correct |
36 ms |
8184 KB |
Output is correct |
36 |
Correct |
382 ms |
19616 KB |
Output is correct |
37 |
Correct |
505 ms |
35812 KB |
Output is correct |
38 |
Correct |
437 ms |
27464 KB |
Output is correct |
39 |
Correct |
332 ms |
19528 KB |
Output is correct |
40 |
Correct |
378 ms |
21140 KB |
Output is correct |
41 |
Correct |
375 ms |
20476 KB |
Output is correct |
42 |
Correct |
394 ms |
20476 KB |
Output is correct |
43 |
Correct |
208 ms |
20332 KB |
Output is correct |
44 |
Correct |
431 ms |
29392 KB |
Output is correct |
45 |
Correct |
372 ms |
27484 KB |
Output is correct |
46 |
Correct |
416 ms |
26996 KB |
Output is correct |
47 |
Correct |
194 ms |
20072 KB |
Output is correct |
48 |
Correct |
379 ms |
30708 KB |
Output is correct |
49 |
Correct |
464 ms |
27620 KB |
Output is correct |
50 |
Correct |
308 ms |
17912 KB |
Output is correct |
51 |
Correct |
498 ms |
36696 KB |
Output is correct |
52 |
Correct |
43 ms |
8828 KB |
Output is correct |
53 |
Correct |
416 ms |
21204 KB |
Output is correct |
54 |
Correct |
511 ms |
38192 KB |
Output is correct |
55 |
Correct |
490 ms |
29420 KB |
Output is correct |
56 |
Correct |
384 ms |
21112 KB |
Output is correct |
57 |
Correct |
368 ms |
24704 KB |
Output is correct |
58 |
Correct |
387 ms |
23416 KB |
Output is correct |
59 |
Correct |
389 ms |
22348 KB |
Output is correct |
60 |
Correct |
311 ms |
21492 KB |
Output is correct |
61 |
Correct |
531 ms |
34984 KB |
Output is correct |
62 |
Correct |
435 ms |
28532 KB |
Output is correct |
63 |
Correct |
468 ms |
28544 KB |
Output is correct |
64 |
Correct |
245 ms |
21868 KB |
Output is correct |
65 |
Correct |
439 ms |
32908 KB |
Output is correct |
66 |
Correct |
436 ms |
29556 KB |
Output is correct |
67 |
Correct |
34 ms |
7160 KB |
Output is correct |
68 |
Correct |
121 ms |
19064 KB |
Output is correct |
69 |
Correct |
227 ms |
19616 KB |
Output is correct |
70 |
Correct |
360 ms |
19448 KB |
Output is correct |
71 |
Correct |
524 ms |
35704 KB |
Output is correct |
72 |
Correct |
438 ms |
27420 KB |
Output is correct |
73 |
Correct |
353 ms |
19528 KB |
Output is correct |
74 |
Correct |
380 ms |
22520 KB |
Output is correct |
75 |
Correct |
412 ms |
21020 KB |
Output is correct |
76 |
Correct |
385 ms |
20728 KB |
Output is correct |
77 |
Correct |
303 ms |
19792 KB |
Output is correct |
78 |
Correct |
421 ms |
31224 KB |
Output is correct |
79 |
Correct |
382 ms |
29428 KB |
Output is correct |
80 |
Correct |
371 ms |
25972 KB |
Output is correct |
81 |
Correct |
204 ms |
19816 KB |
Output is correct |
82 |
Correct |
375 ms |
30692 KB |
Output is correct |
83 |
Correct |
421 ms |
27516 KB |
Output is correct |
84 |
Correct |
409 ms |
19716 KB |
Output is correct |
85 |
Correct |
491 ms |
36244 KB |
Output is correct |
86 |
Correct |
456 ms |
27988 KB |
Output is correct |
87 |
Correct |
331 ms |
19692 KB |
Output is correct |
88 |
Correct |
406 ms |
23328 KB |
Output is correct |
89 |
Correct |
376 ms |
21668 KB |
Output is correct |
90 |
Correct |
383 ms |
21084 KB |
Output is correct |
91 |
Correct |
296 ms |
20208 KB |
Output is correct |
92 |
Correct |
472 ms |
35716 KB |
Output is correct |
93 |
Correct |
381 ms |
25588 KB |
Output is correct |
94 |
Correct |
412 ms |
23960 KB |
Output is correct |
95 |
Correct |
197 ms |
20524 KB |
Output is correct |
96 |
Correct |
390 ms |
31288 KB |
Output is correct |
97 |
Correct |
414 ms |
28200 KB |
Output is correct |
98 |
Correct |
368 ms |
21156 KB |
Output is correct |
99 |
Correct |
510 ms |
36892 KB |
Output is correct |
100 |
Correct |
473 ms |
29040 KB |
Output is correct |
101 |
Correct |
379 ms |
20512 KB |
Output is correct |
102 |
Correct |
372 ms |
23148 KB |
Output is correct |
103 |
Correct |
378 ms |
22040 KB |
Output is correct |
104 |
Correct |
397 ms |
21848 KB |
Output is correct |
105 |
Correct |
296 ms |
20604 KB |
Output is correct |
106 |
Correct |
463 ms |
30088 KB |
Output is correct |
107 |
Correct |
409 ms |
30192 KB |
Output is correct |
108 |
Correct |
563 ms |
26188 KB |
Output is correct |
109 |
Correct |
213 ms |
21356 KB |
Output is correct |
110 |
Correct |
419 ms |
31984 KB |
Output is correct |
111 |
Correct |
422 ms |
29088 KB |
Output is correct |