# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
538732 |
2022-03-17T15:35:29 Z |
dantoh000 |
Paths (RMI21_paths) |
C++14 |
|
305 ms |
16796 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];
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]) continue;
d[v.fi] = d[u]+v.se;
dep[v.fi] = dep[u]+1;
p[v.fi] = 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]) 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]) 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 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;
}
//printf("diameter %d %d\n",d1,d2);
int cur = d2;
while (cur != -1){
ondia[cur] = 1;
cur = p[cur];
}
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:115:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4620 KB |
Output is correct |
3 |
Correct |
3 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4564 KB |
Output is correct |
5 |
Correct |
4 ms |
4564 KB |
Output is correct |
6 |
Correct |
4 ms |
4564 KB |
Output is correct |
7 |
Correct |
4 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4620 KB |
Output is correct |
3 |
Correct |
3 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4564 KB |
Output is correct |
5 |
Correct |
4 ms |
4564 KB |
Output is correct |
6 |
Correct |
4 ms |
4564 KB |
Output is correct |
7 |
Correct |
4 ms |
4564 KB |
Output is correct |
8 |
Correct |
4 ms |
4692 KB |
Output is correct |
9 |
Correct |
4 ms |
4692 KB |
Output is correct |
10 |
Correct |
4 ms |
4692 KB |
Output is correct |
11 |
Correct |
4 ms |
4692 KB |
Output is correct |
12 |
Correct |
3 ms |
4692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4620 KB |
Output is correct |
3 |
Correct |
3 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4564 KB |
Output is correct |
5 |
Correct |
4 ms |
4564 KB |
Output is correct |
6 |
Correct |
4 ms |
4564 KB |
Output is correct |
7 |
Correct |
4 ms |
4564 KB |
Output is correct |
8 |
Correct |
4 ms |
4692 KB |
Output is correct |
9 |
Correct |
4 ms |
4692 KB |
Output is correct |
10 |
Correct |
4 ms |
4692 KB |
Output is correct |
11 |
Correct |
4 ms |
4692 KB |
Output is correct |
12 |
Correct |
3 ms |
4692 KB |
Output is correct |
13 |
Correct |
6 ms |
4692 KB |
Output is correct |
14 |
Correct |
7 ms |
4820 KB |
Output is correct |
15 |
Correct |
6 ms |
4692 KB |
Output is correct |
16 |
Correct |
5 ms |
4820 KB |
Output is correct |
17 |
Correct |
5 ms |
4692 KB |
Output is correct |
18 |
Correct |
6 ms |
4692 KB |
Output is correct |
19 |
Correct |
5 ms |
4692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
13576 KB |
Output is correct |
2 |
Correct |
287 ms |
15412 KB |
Output is correct |
3 |
Correct |
143 ms |
11232 KB |
Output is correct |
4 |
Correct |
295 ms |
13524 KB |
Output is correct |
5 |
Correct |
228 ms |
14896 KB |
Output is correct |
6 |
Correct |
262 ms |
13584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4620 KB |
Output is correct |
3 |
Correct |
3 ms |
4564 KB |
Output is correct |
4 |
Correct |
3 ms |
4564 KB |
Output is correct |
5 |
Correct |
4 ms |
4564 KB |
Output is correct |
6 |
Correct |
4 ms |
4564 KB |
Output is correct |
7 |
Correct |
4 ms |
4564 KB |
Output is correct |
8 |
Correct |
4 ms |
4692 KB |
Output is correct |
9 |
Correct |
4 ms |
4692 KB |
Output is correct |
10 |
Correct |
4 ms |
4692 KB |
Output is correct |
11 |
Correct |
4 ms |
4692 KB |
Output is correct |
12 |
Correct |
3 ms |
4692 KB |
Output is correct |
13 |
Correct |
6 ms |
4692 KB |
Output is correct |
14 |
Correct |
7 ms |
4820 KB |
Output is correct |
15 |
Correct |
6 ms |
4692 KB |
Output is correct |
16 |
Correct |
5 ms |
4820 KB |
Output is correct |
17 |
Correct |
5 ms |
4692 KB |
Output is correct |
18 |
Correct |
6 ms |
4692 KB |
Output is correct |
19 |
Correct |
5 ms |
4692 KB |
Output is correct |
20 |
Correct |
272 ms |
13576 KB |
Output is correct |
21 |
Correct |
287 ms |
15412 KB |
Output is correct |
22 |
Correct |
143 ms |
11232 KB |
Output is correct |
23 |
Correct |
295 ms |
13524 KB |
Output is correct |
24 |
Correct |
228 ms |
14896 KB |
Output is correct |
25 |
Correct |
262 ms |
13584 KB |
Output is correct |
26 |
Correct |
284 ms |
13864 KB |
Output is correct |
27 |
Correct |
231 ms |
15620 KB |
Output is correct |
28 |
Correct |
289 ms |
16132 KB |
Output is correct |
29 |
Correct |
182 ms |
11572 KB |
Output is correct |
30 |
Correct |
305 ms |
13848 KB |
Output is correct |
31 |
Correct |
219 ms |
12872 KB |
Output is correct |
32 |
Correct |
279 ms |
15404 KB |
Output is correct |
33 |
Correct |
248 ms |
13828 KB |
Output is correct |
34 |
Correct |
144 ms |
11156 KB |
Output is correct |
35 |
Correct |
258 ms |
13836 KB |
Output is correct |
36 |
Correct |
198 ms |
16796 KB |
Output is correct |