답안 #858405

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
858405 2023-10-08T11:31:11 Z thangdz2k7 Paths (RMI21_paths) C++17
56 / 100
130 ms 15792 KB
#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){
    //cout << 1 << ' ' << val << '\n';
    if (val >= *Maxk.begin()) Maxk.insert(val), a += val;
    else s.insert(val);
}

void adv(){
    while (Maxk.size() < k and s.size() >= 1){
        a += *s.rbegin();
        Maxk.insert(*s.rbegin());
        s.erase(s.lower_bound(*s.rbegin()));
    }
    while (Maxk.size() > k){
        a -= *Maxk.begin();
        s.insert(*Maxk.begin());
        Maxk.erase(Maxk.begin());
    }
}

void del(long long val){
    //cout << 0 << ' ' << val << '\n';
    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){
    adv();
    ans[u] = a;
    for (ii v : c[u]){
        if (v.F == pa) continue;
        //cout << v.F << endl;
        int val1 = dp1[v.F] + v.S;
        int val2 = dp1[v.F];
        del(dp1[v.F] + v.S);
        add(dp1[v.F]);
        //if (v.F == 4) cout << -111111 << endl;
        long long val = dp1[u];
        if (val == dp1[v.F] + v.S) val = dp2[u];
        add(val + v.S);
        del(val);
        //cout << val << endl;
        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);
    }
}

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);

    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:29: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]
   29 |     while (Maxk.size() > k){
      |            ~~~~~~~~~~~~^~~
Main.cpp: In function 'void get(long long int, long long int)':
Main.cpp:83:19: warning: unused variable 'kk' [-Wunused-variable]
   83 |         long long kk = 0;
      |                   ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4988 KB Output is correct
4 Correct 1 ms 4980 KB Output is correct
5 Correct 1 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4988 KB Output is correct
4 Correct 1 ms 4980 KB Output is correct
5 Correct 1 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 2 ms 5240 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 4952 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4988 KB Output is correct
4 Correct 1 ms 4980 KB Output is correct
5 Correct 1 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 2 ms 5240 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 4952 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 2 ms 5072 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 3 ms 5192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 130 ms 15792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4988 KB Output is correct
4 Correct 1 ms 4980 KB Output is correct
5 Correct 1 ms 4952 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 2 ms 5240 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 4952 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 3 ms 5100 KB Output is correct
15 Correct 2 ms 5072 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 3 ms 5192 KB Output is correct
20 Incorrect 130 ms 15792 KB Output isn't correct
21 Halted 0 ms 0 KB -