Submission #538038

# Submission time Handle Problem Language Result Execution time Memory
538038 2022-03-16T03:39:52 Z zaneyu Paths (RMI21_paths) C++14
100 / 100
409 ms 23716 KB
/*input
3 1
1 2 10
2 3 9
*/
#include<bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define MNTO(x,y) x=min(x,y)
#define MXTO(x,y) x=max(x,y)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define ll long long
#define ld long double
#define sz(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define pii pair<ll,int>
#define f first
#define s second
#define pb push_back
const int maxn=1e5+5;
vector<pii> v[maxn];
int k;
multiset<ll> ms;
multiset<ll,greater<ll>> ls;
ll s=0;
void add(ll x){
    ms.insert(x);
    s+=x;
    if(sz(ms)>k){
        ll z=*ms.begin();
        s-=z;
        ms.erase(ms.begin());
        ls.insert(z);
    }
}
void remove(ll x){
    auto it=ms.find(x);
    if(it==ms.end()){
        ls.erase(ls.find(x));
    }
    else{
        s-=x;
        ms.erase(it);
        if(sz(ls)){
            ll z=(*ls.begin());
            s+=z;
            ms.insert(z);
            ls.erase(ls.begin());
        }
    }
}
ll mx[maxn];
void dfs(int u,int p){
    for(auto x:v[u]){
        if(x.f==p) continue;
        dfs(x.f,u);
        remove(mx[x.f]);
        add(mx[x.f]+x.s);
        MXTO(mx[u],mx[x.f]+x.s);
    }
    add(0);
}
ll ans[maxn];
void dfs2(int u,int p){
    vector<pii> asd;
    asd.pb({0,u});
    for(auto x:v[u]){
        asd.pb({mx[x.f]+x.s,x.f});
    }
    ans[u]=s;
    sort(ALL(asd)),reverse(ALL(asd));
    for(auto x:v[u]){
        if(x.f==p) continue;
        ll tmp=mx[u];
        mx[u]=asd[0].f;
        if(x.f==asd[0].s){
            mx[u]=asd[1].f;
        }
        remove(mx[x.f]+x.s);
        add(mx[x.f]);
        add(mx[u]+x.s);
        remove(mx[u]);
        dfs2(x.f,u);
        remove(mx[x.f]);
        add(mx[x.f]+x.s);
        remove(mx[u]+x.s);
        add(mx[u]);
        mx[u]=tmp;
    }
}
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n;
    cin>>n>>k;
    REP(i,n-1){
        int a,b,c;
        cin>>a>>b>>c;
        --a,--b;
        v[a].pb({b,c});
        v[b].pb({a,c});
    }
    dfs(0,-1);
    dfs2(0,-1);
    REP(i,n) cout<<ans[i]<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 4 ms 2808 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2728 KB Output is correct
12 Correct 5 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 4 ms 2808 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2728 KB Output is correct
12 Correct 5 ms 2772 KB Output is correct
13 Correct 6 ms 2900 KB Output is correct
14 Correct 5 ms 3072 KB Output is correct
15 Correct 6 ms 3028 KB Output is correct
16 Correct 6 ms 2924 KB Output is correct
17 Correct 6 ms 2900 KB Output is correct
18 Correct 4 ms 2852 KB Output is correct
19 Correct 6 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 328 ms 15088 KB Output is correct
2 Correct 351 ms 19784 KB Output is correct
3 Correct 228 ms 15020 KB Output is correct
4 Correct 301 ms 15360 KB Output is correct
5 Correct 332 ms 17000 KB Output is correct
6 Correct 311 ms 15068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 4 ms 2808 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2728 KB Output is correct
12 Correct 5 ms 2772 KB Output is correct
13 Correct 6 ms 2900 KB Output is correct
14 Correct 5 ms 3072 KB Output is correct
15 Correct 6 ms 3028 KB Output is correct
16 Correct 6 ms 2924 KB Output is correct
17 Correct 6 ms 2900 KB Output is correct
18 Correct 4 ms 2852 KB Output is correct
19 Correct 6 ms 2900 KB Output is correct
20 Correct 328 ms 15088 KB Output is correct
21 Correct 351 ms 19784 KB Output is correct
22 Correct 228 ms 15020 KB Output is correct
23 Correct 301 ms 15360 KB Output is correct
24 Correct 332 ms 17000 KB Output is correct
25 Correct 311 ms 15068 KB Output is correct
26 Correct 409 ms 16624 KB Output is correct
27 Correct 340 ms 21752 KB Output is correct
28 Correct 403 ms 22812 KB Output is correct
29 Correct 237 ms 17312 KB Output is correct
30 Correct 342 ms 17596 KB Output is correct
31 Correct 355 ms 16924 KB Output is correct
32 Correct 386 ms 19560 KB Output is correct
33 Correct 362 ms 17548 KB Output is correct
34 Correct 219 ms 17368 KB Output is correct
35 Correct 381 ms 17604 KB Output is correct
36 Correct 353 ms 23716 KB Output is correct