Submission #858409

# Submission time Handle Problem Language Result Execution time Memory
858409 2023-10-08T12:16:53 Z thangdz2k7 Paths (RMI21_paths) C++17
36 / 100
243 ms 32792 KB
// 02033827827
#include <bits/stdc++.h>
#define int long long
#define ii pair <int, int>
#define F first
#define S second

using namespace std;

const int mod = 1e9 + 7;
const int N = 1e5 + 5;

int n, k, dem;
vector <ii> c[N];
long long ans[N], dp1[N], dp2[N], a = 0;
multiset <long long> s, Maxk;

void add(long long val){
    if (Maxk.size() >= 1 and val >= *Maxk.begin()) Maxk.insert(val), a += val;
    else s.insert(val);
}

void adv(){
    while (Maxk.size() < k and s.size() >= 1){
        //cerr << s.size() << ' ';
        a += *s.rbegin();
        Maxk.insert(*s.rbegin());
        s.erase(s.lower_bound(*s.rbegin()));
        //cerr << s.size() << '\n';
    }

    //cerr << "-1\n";
    while (Maxk.size() > k){
        //cerr << Maxk.size() << ' ';
        a -= *Maxk.begin();
        s.insert(*Maxk.begin());
        Maxk.erase(Maxk.begin());
    }
}

void del(long long val){
    if (Maxk.count(val)) Maxk.erase(Maxk.lower_bound(val)), a -= val;
    //else s.erase(s.lower_bound(val));
}

void dfs(int u, int pa){
    //add(0, ans[u]);
    dp1[u] = 0, dp2[u] = 0;
    for (ii v : c[u]){
        if (v.F == pa) continue;
        dfs(v.F, u);
        if (dp1[u] < dp1[v.F] + v.S) dp2[u] = dp1[u], dp1[u] = dp1[v.F] + v.S;
        else dp2[u] = max(dp2[u], dp1[v.F] + v.S);
    }

    bool used = false;
    for (ii v : c[u]){
        if (v.F == pa) continue;
        if (used or dp1[v.F] + v.S < dp1[u] or u == 1) add(dp1[v.F] + v.S);
        else used = true;

    }
}

void get(int u, int pa){
    //cerr << "start " << u << '\n';
    adv();
    ans[u] = a;
    for (ii v : c[u]){
        if (v.F == pa) continue;
        int val1 = dp1[v.F] + v.S;
        int val2 = dp1[v.F];
        del(dp1[v.F] + v.S);
        add(dp1[v.F]);
        long long val = dp1[u];
        if (val == dp1[v.F] + v.S) val = dp2[u];
        add(val + v.S);
        del(val);
        if (dp1[v.F] < val + v.S){
            dp2[v.F] = dp1[v.F];
            dp1[v.F] = val + v.S;
        }
        else dp2[v.F] = max(dp2[v.F], val + v.S);
        get(v.F, u);
        long long kk = 0;
        add(val1);
        del(val2);

        add(val);
        del(val + v.S);
    }
    //cerr << "end " << u << '\n';
}

void solve(){
    cin >> n >> k;
    for (int i = 1; i <= n - 1; ++ i){
        int u, v, w;
        cin >> u >> v >> w;
        c[u].push_back(ii(v, w));
        c[v].push_back(ii(u, w));
    }
    dfs(1, 0);
    get(1, 0);

    //if (k == 1) for (int i = 1; i <= n; ++ i) cout << dp1[i] << '\n';
    for (int i = 1; i <= n; ++ i) cout << ans[i] << '\n';

}

signed main(){
    //freopen("code.inp", "r", stdin);
    //freopen("code.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t --) solve();

    return 0;
}

Compilation message

Main.cpp: In function 'void adv()':
Main.cpp:24:24: 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]
   24 |     while (Maxk.size() < k and s.size() >= 1){
      |            ~~~~~~~~~~~~^~~
Main.cpp:33:24: 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]
   33 |     while (Maxk.size() > k){
      |            ~~~~~~~~~~~~^~~
Main.cpp: In function 'void get(long long int, long long int)':
Main.cpp:85:19: warning: unused variable 'kk' [-Wunused-variable]
   85 |         long long kk = 0;
      |                   ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Incorrect 2 ms 5208 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 243 ms 32792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Incorrect 2 ms 5208 KB Output isn't correct
16 Halted 0 ms 0 KB -