#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using PII = pair<int,int>;
constexpr int SZ = 1 << 18;
int N, M, C[SZ], R[SZ];
vector<int> G[SZ];
int D[2020], SR[2020], Use[2020];
int BFS(int st){
memset(SR, 0, sizeof SR);
memset(D, -1, sizeof D);
memset(Use, 0, sizeof Use);
queue<int> Q; Q.push(st); D[st] = 0;
while(Q.size()){
int v = Q.front(); Q.pop();
if(!SR[D[v]]) SR[D[v]] = C[v];
else SR[D[v]] = -1;
for(auto i : G[v]) if(D[i] == -1) Q.push(i), D[i] = D[v] + 1;
}
for(int i=1; i<=N; i++) if(SR[i] > 0) Use[SR[i]] = 1;
return accumulate(Use+1, Use+M+1, 0);
}
void Naive(){
for(int i=1; i<=N; i++) cout << BFS(i) << "\n";
}
PII T[SZ << 1]; int L[SZ << 1];
PII Merge(const PII &l, const PII &r){
return l.x == r.x ? PII(l.x, l.y + r.y) : min(l, r);
}
void Push(int node, int s, int e){
T[node].x += L[node];
if(s != e) L[node<<1] += L[node], L[node<<1|1] += L[node];
L[node] = 0;
}
void Init(int node=1, int s=1, int e=N){
L[node] = 0;
if(s == e){ T[node] = {0, 1}; return; }
int m = s + e >> 1;
Init(node<<1, s, m);
Init(node<<1|1, m+1, e);
T[node] = Merge(T[node<<1], T[node<<1|1]);
}
void Update(int l, int r, int v, int node=1, int s=1, int e=N){
Push(node, s, e);
if(r < s || e < l) return;
if(l <= s && e <= r){ L[node] += v; Push(node, s, e); return; }
int m = s + e >> 1;
Update(l, r, v, node<<1, s, m);
Update(l, r, v, node<<1|1, m+1, e);
T[node] = Merge(T[node<<1], T[node<<1|1]);
}
PII Query(int l, int r, int node=1, int s=1, int e=N){
Push(node, s, e);
if(r < s || e < l) return T[0];
if(l <= s && e <= r) return T[node];
int m = s + e >> 1;
return Merge(Query(l, r, node<<1, s, m), Query(l, r, node<<1|1, m+1, e));
}
PII Diameter(int root=1){
function<PII(int,int,int)> dfs_far = [&dfs_far](int v, int b, int d){
PII ret(d, v);
for(auto i : G[v]) if(i != b) ret = max(ret, dfs_far(i, v, d+1));
return ret;
};
int t1 = dfs_far(root, -1, 0).y;
int t2 = dfs_far(t1, -1, 0).y;
return {t1, t2};
}
int Dep[SZ]; PII Sub[SZ];
void dfs_info(int v, int b=-1){
Sub[v] = {0, v};
for(auto i : G[v]){
if(i == b) continue;
Dep[i] = Dep[v] + 1;
dfs_info(i, v);
Sub[v] = max(Sub[v], PII(Sub[i].x+1, Sub[i].y));
}
}
void dfs_calc(int v, int b=-1){
auto [mn,cnt] = Query(1, Dep[v]-Sub[v].x-1);
if(mn == 0) R[v] = max(R[v], cnt);
for(auto i : G[v]){
if(i == b) continue;
Update(Dep[v]-Sub[i].x-1, Dep[v]-1, +1);
}
for(auto i : G[v]){
if(i == b) continue;
Update(Dep[v]-Sub[i].x-1, Dep[v]-1, -1);
dfs_calc(i, v);
Update(Dep[v]-Sub[i].x-1, Dep[v]-1, +1);
}
for(auto i : G[v]){
if(i == b) continue;
Update(Dep[v]-Sub[i].x-1, Dep[v]-1, -1);
}
}
void Solve(int st){
Dep[st] = 1;
dfs_info(st);
Init();
dfs_calc(st);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N >> M;
for(int i=1,s,e; i<N; i++) cin >> s >> e, G[s].push_back(e), G[e].push_back(s);
for(int i=1; i<=N; i++) cin >> C[i];
if(N <= 2000){ Naive(); return 0; }
auto [u,v] = Diameter();
Solve(u); Solve(v);
for(int i=1; i<=N; i++) cout << min(R[i], M) << "\n";
}
Compilation message
joi2019_ho_t5.cpp: In function 'void Init(int, int, int)':
joi2019_ho_t5.cpp:44:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int m = s + e >> 1;
| ~~^~~
joi2019_ho_t5.cpp: In function 'void Update(int, int, int, int, int, int)':
joi2019_ho_t5.cpp:53:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int m = s + e >> 1;
| ~~^~~
joi2019_ho_t5.cpp: In function 'PII Query(int, int, int, int, int)':
joi2019_ho_t5.cpp:62:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int m = s + e >> 1;
| ~~^~~
joi2019_ho_t5.cpp: In function 'void dfs_calc(int, int)':
joi2019_ho_t5.cpp:88:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
88 | auto [mn,cnt] = Query(1, Dep[v]-Sub[v].x-1);
| ^
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:118:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
118 | auto [u,v] = Diameter();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6476 KB |
Output is correct |
2 |
Correct |
99 ms |
6560 KB |
Output is correct |
3 |
Correct |
33 ms |
6536 KB |
Output is correct |
4 |
Correct |
101 ms |
6584 KB |
Output is correct |
5 |
Correct |
115 ms |
6728 KB |
Output is correct |
6 |
Correct |
125 ms |
6604 KB |
Output is correct |
7 |
Correct |
122 ms |
6604 KB |
Output is correct |
8 |
Correct |
107 ms |
6604 KB |
Output is correct |
9 |
Correct |
122 ms |
6604 KB |
Output is correct |
10 |
Correct |
123 ms |
6596 KB |
Output is correct |
11 |
Correct |
118 ms |
6604 KB |
Output is correct |
12 |
Correct |
80 ms |
6608 KB |
Output is correct |
13 |
Correct |
125 ms |
6604 KB |
Output is correct |
14 |
Correct |
122 ms |
6604 KB |
Output is correct |
15 |
Correct |
123 ms |
6604 KB |
Output is correct |
16 |
Correct |
80 ms |
6728 KB |
Output is correct |
17 |
Correct |
108 ms |
6604 KB |
Output is correct |
18 |
Correct |
111 ms |
6500 KB |
Output is correct |
19 |
Correct |
116 ms |
6504 KB |
Output is correct |
20 |
Correct |
122 ms |
6596 KB |
Output is correct |
21 |
Correct |
124 ms |
6604 KB |
Output is correct |
22 |
Correct |
111 ms |
6532 KB |
Output is correct |
23 |
Correct |
128 ms |
6604 KB |
Output is correct |
24 |
Correct |
119 ms |
6500 KB |
Output is correct |
25 |
Correct |
123 ms |
6620 KB |
Output is correct |
26 |
Correct |
84 ms |
6632 KB |
Output is correct |
27 |
Correct |
136 ms |
6604 KB |
Output is correct |
28 |
Correct |
117 ms |
6604 KB |
Output is correct |
29 |
Correct |
120 ms |
6604 KB |
Output is correct |
30 |
Correct |
80 ms |
6736 KB |
Output is correct |
31 |
Correct |
109 ms |
6500 KB |
Output is correct |
32 |
Correct |
112 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
662 ms |
15648 KB |
Output is correct |
2 |
Correct |
825 ms |
28124 KB |
Output is correct |
3 |
Correct |
154 ms |
10280 KB |
Output is correct |
4 |
Correct |
1255 ms |
23324 KB |
Output is correct |
5 |
Correct |
1719 ms |
45108 KB |
Output is correct |
6 |
Correct |
1607 ms |
34088 KB |
Output is correct |
7 |
Correct |
1183 ms |
23360 KB |
Output is correct |
8 |
Correct |
1259 ms |
25540 KB |
Output is correct |
9 |
Correct |
1276 ms |
24832 KB |
Output is correct |
10 |
Correct |
1262 ms |
24740 KB |
Output is correct |
11 |
Correct |
1153 ms |
23928 KB |
Output is correct |
12 |
Correct |
1522 ms |
36888 KB |
Output is correct |
13 |
Correct |
1366 ms |
34004 KB |
Output is correct |
14 |
Correct |
1508 ms |
33272 KB |
Output is correct |
15 |
Correct |
1049 ms |
23988 KB |
Output is correct |
16 |
Correct |
1420 ms |
38064 KB |
Output is correct |
17 |
Correct |
1513 ms |
34432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
924 ms |
21048 KB |
Output is correct |
2 |
Correct |
1542 ms |
44460 KB |
Output is correct |
3 |
Correct |
183 ms |
10696 KB |
Output is correct |
4 |
Correct |
1200 ms |
23324 KB |
Output is correct |
5 |
Correct |
1631 ms |
46000 KB |
Output is correct |
6 |
Correct |
1579 ms |
34416 KB |
Output is correct |
7 |
Correct |
1210 ms |
23376 KB |
Output is correct |
8 |
Correct |
1297 ms |
27980 KB |
Output is correct |
9 |
Correct |
1244 ms |
26352 KB |
Output is correct |
10 |
Correct |
1278 ms |
25076 KB |
Output is correct |
11 |
Correct |
1167 ms |
23672 KB |
Output is correct |
12 |
Correct |
1536 ms |
41420 KB |
Output is correct |
13 |
Correct |
1356 ms |
33156 KB |
Output is correct |
14 |
Correct |
1498 ms |
33284 KB |
Output is correct |
15 |
Correct |
1079 ms |
23984 KB |
Output is correct |
16 |
Correct |
1429 ms |
38584 KB |
Output is correct |
17 |
Correct |
1517 ms |
34596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6476 KB |
Output is correct |
2 |
Correct |
99 ms |
6560 KB |
Output is correct |
3 |
Correct |
33 ms |
6536 KB |
Output is correct |
4 |
Correct |
101 ms |
6584 KB |
Output is correct |
5 |
Correct |
115 ms |
6728 KB |
Output is correct |
6 |
Correct |
125 ms |
6604 KB |
Output is correct |
7 |
Correct |
122 ms |
6604 KB |
Output is correct |
8 |
Correct |
107 ms |
6604 KB |
Output is correct |
9 |
Correct |
122 ms |
6604 KB |
Output is correct |
10 |
Correct |
123 ms |
6596 KB |
Output is correct |
11 |
Correct |
118 ms |
6604 KB |
Output is correct |
12 |
Correct |
80 ms |
6608 KB |
Output is correct |
13 |
Correct |
125 ms |
6604 KB |
Output is correct |
14 |
Correct |
122 ms |
6604 KB |
Output is correct |
15 |
Correct |
123 ms |
6604 KB |
Output is correct |
16 |
Correct |
80 ms |
6728 KB |
Output is correct |
17 |
Correct |
108 ms |
6604 KB |
Output is correct |
18 |
Correct |
111 ms |
6500 KB |
Output is correct |
19 |
Correct |
116 ms |
6504 KB |
Output is correct |
20 |
Correct |
122 ms |
6596 KB |
Output is correct |
21 |
Correct |
124 ms |
6604 KB |
Output is correct |
22 |
Correct |
111 ms |
6532 KB |
Output is correct |
23 |
Correct |
128 ms |
6604 KB |
Output is correct |
24 |
Correct |
119 ms |
6500 KB |
Output is correct |
25 |
Correct |
123 ms |
6620 KB |
Output is correct |
26 |
Correct |
84 ms |
6632 KB |
Output is correct |
27 |
Correct |
136 ms |
6604 KB |
Output is correct |
28 |
Correct |
117 ms |
6604 KB |
Output is correct |
29 |
Correct |
120 ms |
6604 KB |
Output is correct |
30 |
Correct |
80 ms |
6736 KB |
Output is correct |
31 |
Correct |
109 ms |
6500 KB |
Output is correct |
32 |
Correct |
112 ms |
6604 KB |
Output is correct |
33 |
Correct |
662 ms |
15648 KB |
Output is correct |
34 |
Correct |
825 ms |
28124 KB |
Output is correct |
35 |
Correct |
154 ms |
10280 KB |
Output is correct |
36 |
Correct |
1255 ms |
23324 KB |
Output is correct |
37 |
Correct |
1719 ms |
45108 KB |
Output is correct |
38 |
Correct |
1607 ms |
34088 KB |
Output is correct |
39 |
Correct |
1183 ms |
23360 KB |
Output is correct |
40 |
Correct |
1259 ms |
25540 KB |
Output is correct |
41 |
Correct |
1276 ms |
24832 KB |
Output is correct |
42 |
Correct |
1262 ms |
24740 KB |
Output is correct |
43 |
Correct |
1153 ms |
23928 KB |
Output is correct |
44 |
Correct |
1522 ms |
36888 KB |
Output is correct |
45 |
Correct |
1366 ms |
34004 KB |
Output is correct |
46 |
Correct |
1508 ms |
33272 KB |
Output is correct |
47 |
Correct |
1049 ms |
23988 KB |
Output is correct |
48 |
Correct |
1420 ms |
38064 KB |
Output is correct |
49 |
Correct |
1513 ms |
34432 KB |
Output is correct |
50 |
Correct |
924 ms |
21048 KB |
Output is correct |
51 |
Correct |
1542 ms |
44460 KB |
Output is correct |
52 |
Correct |
183 ms |
10696 KB |
Output is correct |
53 |
Correct |
1200 ms |
23324 KB |
Output is correct |
54 |
Correct |
1631 ms |
46000 KB |
Output is correct |
55 |
Correct |
1579 ms |
34416 KB |
Output is correct |
56 |
Correct |
1210 ms |
23376 KB |
Output is correct |
57 |
Correct |
1297 ms |
27980 KB |
Output is correct |
58 |
Correct |
1244 ms |
26352 KB |
Output is correct |
59 |
Correct |
1278 ms |
25076 KB |
Output is correct |
60 |
Correct |
1167 ms |
23672 KB |
Output is correct |
61 |
Correct |
1536 ms |
41420 KB |
Output is correct |
62 |
Correct |
1356 ms |
33156 KB |
Output is correct |
63 |
Correct |
1498 ms |
33284 KB |
Output is correct |
64 |
Correct |
1079 ms |
23984 KB |
Output is correct |
65 |
Correct |
1429 ms |
38584 KB |
Output is correct |
66 |
Correct |
1517 ms |
34596 KB |
Output is correct |
67 |
Incorrect |
120 ms |
9108 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |