Submission #626957

# Submission time Handle Problem Language Result Execution time Memory
626957 2022-08-12T03:03:37 Z abcvuitunggio Paths (RMI21_paths) C++17
100 / 100
261 ms 17952 KB
#include <iostream>
#include <vector>
#include <set>
#define int long long
using namespace std;
int n,k,u,v,w,dp[100001],dp2[100001],res[100001],x;
vector <pair <int, int> > ke[100001];
multiset <int> s,s2;
void add(int val){
    s.insert(val);
    x+=val;
    if (s.size()>k){
        s2.insert(*s.begin());
        x-=*s.begin();
        s.erase(s.begin());
    }
}
void del(int val){
    if (s2.find(val)!=s2.end()){
        s2.erase(s2.find(val));
        return;
    }
    if (s.find(val)!=s.end()){
        s.erase(s.find(val));
        x-=val;
        s.insert(*--s2.end());
        x+=*--s2.end();
        s2.erase(--s2.end());
    }
}
void dfs(int u, int p){
    for (auto [v,w]:ke[u])
        if (v!=p){
            dfs(v,u);
            dp[u]=max(dp[u],dp[v]+w);
        }
}
void dfs2(int u, int p){
    int mx=-1,mx2=-1,v1=-1;
    for (auto [v,w]:ke[u])
        if (v!=p){
            if (dp[v]+w>mx){
                mx2=mx;
                mx=dp[v]+w;
                v1=v;
            }
            else if (dp[v]+w>mx2)
                mx2=dp[v]+w;
        }
    for (auto [v,w]:ke[u])
        if (v!=p){
            dp2[v]=max(dp2[u],(v==v1?mx2:mx))+w;
            dfs2(v,u);
            if (v!=v1)
                add(dp[v]+w);
        }
}
void dfs3(int u, int p){
    res[u]=x;
    for (auto [v,w]:ke[u])
        if (v!=p){
            del(dp[v]+w);
            add(dp[v]);
            del(dp2[v]-w);
            add(dp2[v]);
            dfs3(v,u);
            add(dp[v]+w);
            del(dp[v]);
            add(dp2[v]-w);
            del(dp2[v]);
        }
}
signed main(){
    ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
    cin >> n >> k;
    for (int i=1;i<n;i++){
        cin >> u >> v >> w;
        ke[u].push_back({v,w});
        ke[v].push_back({u,w});
    }
    dfs(1,1);
    add(dp[1]);
    dfs2(1,1);
    dfs3(1,1);
    for (int i=1;i<=n;i++)
        cout << res[i] << '\n';
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:12:17: 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]
   12 |     if (s.size()>k){
      |         ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2676 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 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2676 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 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2676 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 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 5 ms 2900 KB Output is correct
14 Correct 4 ms 2944 KB Output is correct
15 Correct 5 ms 2948 KB Output is correct
16 Correct 5 ms 2900 KB Output is correct
17 Correct 5 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 5 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 13572 KB Output is correct
2 Correct 201 ms 15404 KB Output is correct
3 Correct 148 ms 11488 KB Output is correct
4 Correct 214 ms 13544 KB Output is correct
5 Correct 206 ms 14412 KB Output is correct
6 Correct 204 ms 13580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2676 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 2 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 5 ms 2900 KB Output is correct
14 Correct 4 ms 2944 KB Output is correct
15 Correct 5 ms 2948 KB Output is correct
16 Correct 5 ms 2900 KB Output is correct
17 Correct 5 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 5 ms 2900 KB Output is correct
20 Correct 224 ms 13572 KB Output is correct
21 Correct 201 ms 15404 KB Output is correct
22 Correct 148 ms 11488 KB Output is correct
23 Correct 214 ms 13544 KB Output is correct
24 Correct 206 ms 14412 KB Output is correct
25 Correct 204 ms 13580 KB Output is correct
26 Correct 261 ms 16076 KB Output is correct
27 Correct 223 ms 17484 KB Output is correct
28 Correct 241 ms 17868 KB Output is correct
29 Correct 150 ms 13712 KB Output is correct
30 Correct 242 ms 15980 KB Output is correct
31 Correct 244 ms 14648 KB Output is correct
32 Correct 240 ms 16804 KB Output is correct
33 Correct 243 ms 16076 KB Output is correct
34 Correct 139 ms 13648 KB Output is correct
35 Correct 243 ms 16024 KB Output is correct
36 Correct 228 ms 17952 KB Output is correct