# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
538724 |
2022-03-17T15:27:08 Z |
dantoh000 |
Paths (RMI21_paths) |
C++14 |
|
406 ms |
20304 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];
ll md = 0;
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);
}
}
int dfs2(int u){
int best = 0;
for (auto v : G[u]){
if (v.fi == p[u][0]) continue;
int 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, int 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));
p[1][0] = -1;
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));
p[d1][0] = -1;
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:37:23: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
37 | while (top.size() > k){
| ~~~~~~~~~~~^~~
Main.cpp:44:40: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | while (bottom.size() && top.size() < k){
| ~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:136:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
Main.cpp:139:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
406 ms |
20304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
11220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |