Submission #574096

# Submission time Handle Problem Language Result Execution time Memory
574096 2022-06-07T19:02:30 Z urosk Paths (RMI21_paths) C++14
8 / 100
233 ms 124656 KB
//https://oj.uz/problem/view/RMI21_paths
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);

using namespace std;

#define maxn 1000005
#define lg 32
ll n,k;
vector<pll> g[maxn];
ll ans[maxn];
ll dp[maxn];
ll up[maxn];
ll st[maxn][lg];
ll naj[maxn];
ll val[maxn];
ll sum[maxn];
multiset<ll> topk;
multiset<ll> other;
vector<ll> v;
ll in[maxn];
ll it = 1;
void dfs(ll u,ll par){
    st[u][0] = par;
    dp[u] = 0;
    naj[u] = u;
    for(pll p : g[u]){
        ll s = p.fi;
        ll w = p.sc;
        if(s==par) continue;
        sum[s] = sum[u] + w;
        dfs(s,u);
        if(dp[s]+w>dp[u]) naj[u] = naj[s];
        dp[u] = max(dp[u],dp[s]+w);
    }
}
bool cmp(ll x,ll y){return x>y;}
vector<ll> ch;
void dfs2(ll u,ll par){
    if(sz(g[u])==1) return;
    ch.clear();
    ch.pb(up[u]);
    for(pll p : g[u]){
        ll s = p.fi;
        ll w = p.sc;
        if(s==par) continue;
        ll x = dp[s] + w;
        ch.pb(x);
    }
    sort(all(ch),cmp);
    for(pll p : g[u]){
        ll s = p.fi;
        ll w = p.sc;
        if(s==par) continue;
        ll  x = ch[0];
        if(x==dp[s]+w) x = ch[1];
        up[s] = x+w;
    }
    for(pll p : g[u]){
        ll s = p.fi;
        ll w = p.sc;
        if(s==par) continue;
        dfs2(s,u);
    }
}

ll cur = 0;
void add(ll x){
    cur+=x;
    topk.insert(x);
    if(sz(topk)>k){
        ll y = *topk.begin();
        cur-=y;
        topk.erase(topk.begin());
        other.insert(y);
    }
}
void rem(ll x){
    auto it = other.find(x);
    if(it!=other.end()){
        other.erase(it);
        return;
    }
    cur-=x;
    it = topk.find(x);
    assert(it!=topk.end());
    topk.erase(it);
    if(sz(other)){
        cur+=*other.rbegin();
        topk.insert(*other.rbegin());
        auto it = other.end();
        it--;
        other.erase(it);
    }
}
void reshi(ll u,ll par){
    ans[u] = cur;
    for(pll p : g[u]){
        ll s = p.fi;
        ll w = p.sc;
        if(s==par) continue;
        add(dp[s]);
        rem(dp[s]+w);
        add(up[s]);
        rem(up[s]-w);
        reshi(s,u);
        rem(dp[s]);
        add(dp[s]+w);
        rem(up[s]);
        add(up[s]-w);
    }
}
int main(){
	daj_mi_malo_vremena
    cin >> n >> k;
    ll sve = 0;
    for(ll i = 1;i<=n-1;i++){
        ll x,y,w; cin >> x >> y >> w;
        g[x].pb({y,w});
        g[y].pb({x,w});
        sve+=w;
    }
    for(ll i = 1;i<=n;i++) if(sz(g[i])==1) v.pb(i);
    if(sz(v)<=k){
        for(ll i = 1;i<=n;i++) cout<<sve<<endl;
        return 0;
    }
    up[1] = 0;
    dfs(1,1);
    dfs2(1,1);
    for(ll j = 1;j<lg;j++) for(ll i = 1;i<=n;i++) st[i][j] = st[st[i][j-1]][j-1];
    for(ll i = 1;i<=n;i++){
        ll e = 0;
        ll cur = i;
        for(ll j = lg-1;j>=0;j--){
            if(naj[st[cur][j]]==i) cur = st[cur][j];
        }
        val[i] = sum[i] - sum[st[cur][0]];
    }
    for(ll i = 1;i<=sz(v);i++) add(val[v[i-1]]);
    reshi(1,1);
    for(ll i = 1;i<=n;i++) cout<<ans[i]<<endl;
	return 0;
}
/*
11 3
1 2 5
2 3 3
2 6 5
3 4 4
3 5 2
1 7 6
7 8 4
7 9 5
1 10 1
10 11 1
*/

Compilation message

Main.cpp: In function 'void dfs2(long long int, long long int)':
Main.cpp:75:12: warning: unused variable 'w' [-Wunused-variable]
   75 |         ll w = p.sc;
      |            ^
Main.cpp: In function 'int main()':
Main.cpp:147:12: warning: unused variable 'e' [-Wunused-variable]
  147 |         ll e = 0;
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 14 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 14 ms 23892 KB Output is correct
3 Correct 14 ms 23996 KB Output is correct
4 Correct 13 ms 23940 KB Output is correct
5 Correct 12 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Runtime error 35 ms 48248 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 14 ms 23892 KB Output is correct
3 Correct 14 ms 23996 KB Output is correct
4 Correct 13 ms 23940 KB Output is correct
5 Correct 12 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Runtime error 35 ms 48248 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 14 ms 23892 KB Output is correct
3 Correct 14 ms 23996 KB Output is correct
4 Correct 13 ms 23940 KB Output is correct
5 Correct 12 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Runtime error 35 ms 48248 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 233 ms 124656 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 14 ms 23892 KB Output is correct
3 Correct 14 ms 23996 KB Output is correct
4 Correct 13 ms 23940 KB Output is correct
5 Correct 12 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Runtime error 35 ms 48248 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -