# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
538729 |
2022-03-17T15:33:48 Z |
dantoh000 |
Paths (RMI21_paths) |
C++14 |
|
303 ms |
23952 KB |
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
int n,k;
vector<ii> G[100005];
ll d[100005];
int p[100005][18];
int dep[100005];
int d1 = 0;
int d2 = 0;
int vis[19];
int dtp[100005];
int ondia[100005];
ll len[100005];
multiset<ll> top;
multiset<ll> bottom;
ll cursum = 0;
ll ans[100005];
void change(ll a, ll b){
//printf("changing %lld for %lld\n",a,b);
if (top.find(a) != top.end()) {
top.erase(top.find(a));
cursum -= a;
}
else if (bottom.find(a) != bottom.end()) bottom.erase(bottom.find(a));
if (top.empty() || b >= *top.begin()){
top.insert(b);
cursum += b;
}
else{
bottom.insert(b);
}
while (top.size() > k){
auto it = top.begin();
bottom.insert(*it);
//printf("too many, removing %lld\n",*it);
cursum -= *it;
top.erase(it);
}
while (bottom.size() && top.size() < k){
auto it = --bottom.end();
top.insert(*it);
//printf("too little, adding %lld\n",*it);
cursum += *it;
bottom.erase(it);
}
}
void dfs(int u){
for (auto v : G[u]){
if (v.fi == p[u][0]) continue;
d[v.fi] = d[u]+v.se;
dep[v.fi] = dep[u]+1;
p[v.fi][0] = u;
dtp[v.fi] = v.se;
dfs(v.fi);
}
}
ll dfs2(int u){
ll best = 0;
for (auto v : G[u]){
if (v.fi == p[u][0]) continue;
ll x = dfs2(v.fi);
if (best < x){
if (best != 0) change(-1, best);
best = x;
}
else{
if (x != 0) change(-1, x);
}
}
len[u] = best+dtp[u];
//printf("len from %d = %lld\n",u,len[u]);
return len[u];
}
void dfs3(int u, ll l){
//printf("currently at %d: ans = %lld\n",u,cursum);
//printf("at top: "); for (auto x : top) printf("%lld ",x); printf("\n");
//printf("at bottom: "); for (auto x : bottom) printf("%lld ",x); printf("\n");
ans[u] = cursum;
for (auto v : G[u]){
if (v.fi == p[u][0]) continue;
if (ondia[u] && !ondia[v.fi]){
l = max(d[d2]-d[u], d[u]);
change(l,l+v.se);
change(len[v.fi], len[v.fi]-v.se);
dfs3(v.fi, l+v.se);
change(len[v.fi]-v.se,len[v.fi]);
change(l+v.se,l);
}
else if (!ondia[u] && !ondia[v.fi]){
change(l,l+v.se);
change(len[v.fi], len[v.fi]-v.se);
dfs3(v.fi, l+v.se);
change(len[v.fi]-v.se,len[v.fi]);
change(l+v.se,l);
}
else{
change(d[d2]-d[u], d[d2]-d[v.fi]);
change(d[u], d[v.fi]);
dfs3(v.fi,l);
change(d[v.fi],d[u]);
change(d[d2]-d[v.fi],d[d2]-d[u]);
}
}
}
int lca(int a, int b){
if (dep[a] > dep[b]) swap(a,b);
int DD = dep[b] - dep[a];
for (int k = 0; k < 18; k++){
if (DD>>k&1){
b = p[b][k];
}
}
if (a == b) return a;
for (int k = 17; k >= 0; k--){
if (p[a][k] != -1 && p[a][k] != p[b][k]){
a = p[a][k];
b = p[b][k];
}
}
assert(p[a][0] == p[b][0]);
return p[a][0];
}
ll dist(int a, int b){
return d[a] + d[b] - 2*d[lca(a,b)];
}
int main(){
scanf("%d%d",&n,&k);
for (int i = 1 ; i < n; i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
G[a].push_back({b,c});
G[b].push_back({a,c});
}
memset(p,-1,sizeof(p));
dfs(1);
for (int i = 1; i <= n; i++){
if (d[i] > d[d1]) d1 = i;
}
memset(p,-1,sizeof(p));
memset(d,0,sizeof(d));
memset(dep,0,sizeof(dep));
memset(dtp,0,sizeof(dtp));
dfs(d1);
for (int i = 1; i <= n; i++){
if (d[i] > d[d2]) d2 = i;
}
for (int k = 1; k < 18; k++){
for (int i = 1; i <= n; i++){
if (p[i][k-1] != -1) p[i][k] = p[p[i][k-1]][k-1];
}
}
//printf("diameter %d %d\n",d1,d2);
int cur = d2;
while (cur != -1){
ondia[cur] = 1;
cur = p[cur][0];
}
dfs2(d1);
if (len[d1] != 0) change(-1, len[d1]);
dfs3(d1, 0);
for (int i = 1; i <= n; i++){
printf("%lld\n",ans[i]);
}
}
Compilation message
Main.cpp: In function 'void change(ll, ll)':
Main.cpp:36:23: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
36 | while (top.size() > k){
| ~~~~~~~~~~~^~~
Main.cpp:43:40: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
43 | while (bottom.size() && top.size() < k){
| ~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:135:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:138:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
7 ms |
11220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
7 ms |
11220 KB |
Output is correct |
3 |
Correct |
7 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11220 KB |
Output is correct |
5 |
Correct |
7 ms |
11220 KB |
Output is correct |
6 |
Correct |
6 ms |
11168 KB |
Output is correct |
7 |
Correct |
8 ms |
11220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
7 ms |
11220 KB |
Output is correct |
3 |
Correct |
7 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11220 KB |
Output is correct |
5 |
Correct |
7 ms |
11220 KB |
Output is correct |
6 |
Correct |
6 ms |
11168 KB |
Output is correct |
7 |
Correct |
8 ms |
11220 KB |
Output is correct |
8 |
Correct |
8 ms |
11348 KB |
Output is correct |
9 |
Correct |
8 ms |
11348 KB |
Output is correct |
10 |
Correct |
7 ms |
11348 KB |
Output is correct |
11 |
Correct |
7 ms |
11348 KB |
Output is correct |
12 |
Correct |
7 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
7 ms |
11220 KB |
Output is correct |
3 |
Correct |
7 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11220 KB |
Output is correct |
5 |
Correct |
7 ms |
11220 KB |
Output is correct |
6 |
Correct |
6 ms |
11168 KB |
Output is correct |
7 |
Correct |
8 ms |
11220 KB |
Output is correct |
8 |
Correct |
8 ms |
11348 KB |
Output is correct |
9 |
Correct |
8 ms |
11348 KB |
Output is correct |
10 |
Correct |
7 ms |
11348 KB |
Output is correct |
11 |
Correct |
7 ms |
11348 KB |
Output is correct |
12 |
Correct |
7 ms |
11352 KB |
Output is correct |
13 |
Correct |
9 ms |
11348 KB |
Output is correct |
14 |
Correct |
8 ms |
11496 KB |
Output is correct |
15 |
Correct |
8 ms |
11348 KB |
Output is correct |
16 |
Correct |
8 ms |
11448 KB |
Output is correct |
17 |
Correct |
10 ms |
11348 KB |
Output is correct |
18 |
Correct |
8 ms |
11348 KB |
Output is correct |
19 |
Correct |
9 ms |
11348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
302 ms |
20224 KB |
Output is correct |
2 |
Correct |
237 ms |
22384 KB |
Output is correct |
3 |
Correct |
178 ms |
17908 KB |
Output is correct |
4 |
Correct |
226 ms |
20172 KB |
Output is correct |
5 |
Correct |
227 ms |
21816 KB |
Output is correct |
6 |
Correct |
220 ms |
20308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
7 ms |
11220 KB |
Output is correct |
3 |
Correct |
7 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11220 KB |
Output is correct |
5 |
Correct |
7 ms |
11220 KB |
Output is correct |
6 |
Correct |
6 ms |
11168 KB |
Output is correct |
7 |
Correct |
8 ms |
11220 KB |
Output is correct |
8 |
Correct |
8 ms |
11348 KB |
Output is correct |
9 |
Correct |
8 ms |
11348 KB |
Output is correct |
10 |
Correct |
7 ms |
11348 KB |
Output is correct |
11 |
Correct |
7 ms |
11348 KB |
Output is correct |
12 |
Correct |
7 ms |
11352 KB |
Output is correct |
13 |
Correct |
9 ms |
11348 KB |
Output is correct |
14 |
Correct |
8 ms |
11496 KB |
Output is correct |
15 |
Correct |
8 ms |
11348 KB |
Output is correct |
16 |
Correct |
8 ms |
11448 KB |
Output is correct |
17 |
Correct |
10 ms |
11348 KB |
Output is correct |
18 |
Correct |
8 ms |
11348 KB |
Output is correct |
19 |
Correct |
9 ms |
11348 KB |
Output is correct |
20 |
Correct |
302 ms |
20224 KB |
Output is correct |
21 |
Correct |
237 ms |
22384 KB |
Output is correct |
22 |
Correct |
178 ms |
17908 KB |
Output is correct |
23 |
Correct |
226 ms |
20172 KB |
Output is correct |
24 |
Correct |
227 ms |
21816 KB |
Output is correct |
25 |
Correct |
220 ms |
20308 KB |
Output is correct |
26 |
Correct |
303 ms |
20508 KB |
Output is correct |
27 |
Correct |
248 ms |
22588 KB |
Output is correct |
28 |
Correct |
283 ms |
23068 KB |
Output is correct |
29 |
Correct |
152 ms |
18084 KB |
Output is correct |
30 |
Correct |
264 ms |
20480 KB |
Output is correct |
31 |
Correct |
221 ms |
19444 KB |
Output is correct |
32 |
Correct |
252 ms |
22200 KB |
Output is correct |
33 |
Correct |
279 ms |
20476 KB |
Output is correct |
34 |
Correct |
132 ms |
17828 KB |
Output is correct |
35 |
Correct |
296 ms |
20608 KB |
Output is correct |
36 |
Correct |
196 ms |
23952 KB |
Output is correct |