Submission #871280

# Submission time Handle Problem Language Result Execution time Memory
871280 2023-11-10T11:44:56 Z Ahmed57 Paths (RMI21_paths) C++17
100 / 100
250 ms 24172 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
pair<long long,long long> vl[100001];
long long ans[100001];set<int> leafs;
vector<pair<int,long long>> adj[100001];
void dfs(int i,int pr,int la){
    if(adj[i].size()==1)leafs.insert(i);
    if(adj[i].size()==1&&i!=1){
        ans[i] = la;vl[i] = {la,i};
        return ;
    }
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        dfs(j.first,i,j.second);
        vl[i] = max(vl[i],vl[j.first]);
    }
    ans[vl[i].second]+=la;
    vl[i].first+=la;
}multiset<long long> used,unused;
long long all = 0;int n,k;
void add(long long x){
    all+=x;
    used.insert(x);
    if(used.size()>k){
        all-=*used.begin();
        unused.insert(*used.begin());
        used.erase(used.begin());
    }
}
void rem(long long x){
    if(x<(*used.begin())){
        unused.erase(unused.lower_bound(x));
    }else{
        all-=x;
        used.erase(used.lower_bound(x));
        while(used.size()<k&&unused.size()>0){
            auto it = unused.end();it--;
            used.insert(*it);
            all+=(*it);
            unused.erase(it);
        }
    }
}
long long v[100001];
pair<long long,long long> pref[100001],suf[100001];
void reroot(int i,int pr,pair<long long,long long> pa){
    v[i] = all;
    if(adj[i].size()==1&&i!=1)return;
    if(n==1&&i==1)return;
    if(i==1&&adj[i].size()==1){
        pa = {0,i};
    }
    pair<long long,long long> ma = pa;
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        pref[j.first] = ma;
        ma = max(ma,make_pair(vl[j.first].first,vl[j.first].second));
    }
    ma = pa;
    reverse(adj[i].begin(),adj[i].end());
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        suf[j.first] = ma;
        ma = max(ma,make_pair(vl[j.first].first,vl[j.first].second));
    }
    reverse(adj[i].begin(),adj[i].end());
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        rem(ans[vl[j.first].second]);
        ans[vl[j.first].second]-=j.second;
        add(ans[vl[j.first].second]);
        pair<int,int> xd = max(pref[j.first],suf[j.first]);
        rem(ans[xd.second]);
        ans[xd.second]+=j.second;
        add(ans[xd.second]);
        xd.first+=j.second;
        reroot(j.first,i,xd);
        rem(ans[xd.second]);
        ans[xd.second]-=j.second;
        add(ans[xd.second]);
        rem(ans[vl[j.first].second]);
        ans[vl[j.first].second]+=j.second;
        add(ans[vl[j.first].second]);
    }
}
signed main(){
    cin>>n>>k;
    for(int i =0;i<n-1;i++){
        int a,b,c;cin>>a>>b>>c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }
    dfs(1,0,0);
    for(auto i:leafs){
        add(ans[i]);
    }
    reroot(1,0,{0,0});
    for(int i = 1;i<=n;i++){
        cout<<v[i]<<"\n";
    }
}
/*
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 add(long long int)':
Main.cpp:25:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   25 |     if(used.size()>k){
      |        ~~~~~~~~~~~^~
Main.cpp: In function 'void rem(long long int)':
Main.cpp:37:26: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   37 |         while(used.size()<k&&unused.size()>0){
      |               ~~~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8636 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8644 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8636 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8644 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 3 ms 8792 KB Output is correct
9 Correct 4 ms 8792 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8636 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8644 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 3 ms 8792 KB Output is correct
9 Correct 4 ms 8792 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8768 KB Output is correct
13 Correct 5 ms 8796 KB Output is correct
14 Correct 4 ms 8908 KB Output is correct
15 Correct 4 ms 8884 KB Output is correct
16 Correct 6 ms 8912 KB Output is correct
17 Correct 5 ms 8928 KB Output is correct
18 Correct 3 ms 8796 KB Output is correct
19 Correct 7 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 230 ms 19892 KB Output is correct
2 Correct 221 ms 23888 KB Output is correct
3 Correct 156 ms 18000 KB Output is correct
4 Correct 213 ms 21964 KB Output is correct
5 Correct 247 ms 22916 KB Output is correct
6 Correct 209 ms 22368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 1 ms 8636 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8644 KB Output is correct
7 Correct 1 ms 8540 KB Output is correct
8 Correct 3 ms 8792 KB Output is correct
9 Correct 4 ms 8792 KB Output is correct
10 Correct 3 ms 8540 KB Output is correct
11 Correct 3 ms 8796 KB Output is correct
12 Correct 3 ms 8768 KB Output is correct
13 Correct 5 ms 8796 KB Output is correct
14 Correct 4 ms 8908 KB Output is correct
15 Correct 4 ms 8884 KB Output is correct
16 Correct 6 ms 8912 KB Output is correct
17 Correct 5 ms 8928 KB Output is correct
18 Correct 3 ms 8796 KB Output is correct
19 Correct 7 ms 8796 KB Output is correct
20 Correct 230 ms 19892 KB Output is correct
21 Correct 221 ms 23888 KB Output is correct
22 Correct 156 ms 18000 KB Output is correct
23 Correct 213 ms 21964 KB Output is correct
24 Correct 247 ms 22916 KB Output is correct
25 Correct 209 ms 22368 KB Output is correct
26 Correct 240 ms 22572 KB Output is correct
27 Correct 241 ms 23632 KB Output is correct
28 Correct 225 ms 24172 KB Output is correct
29 Correct 188 ms 18144 KB Output is correct
30 Correct 241 ms 22400 KB Output is correct
31 Correct 238 ms 20052 KB Output is correct
32 Correct 222 ms 23400 KB Output is correct
33 Correct 236 ms 22452 KB Output is correct
34 Correct 156 ms 17732 KB Output is correct
35 Correct 250 ms 22472 KB Output is correct
36 Correct 199 ms 23400 KB Output is correct