답안 #765310

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765310 2023-06-24T10:36:58 Z Ronin13 Paths (RMI21_paths) C++17
100 / 100
231 ms 19108 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;

const int nmax = 200001;

vector <pair<int, ll> > g[nmax];
ll mx[nmax];

multiset <ll> s, l;
ll ans[nmax];
ll cur;
int n, k;

void add(ll val){
    l.insert(val);
    cur += val;
    if(l.size() > k){
        ll x = *l.begin();
        l.erase(l.begin());
        cur -= x;
        s.insert(x);
    }
}

void rem(ll val){
    if(l.find(val) != l.end()){
        l.erase(l.find(val));
        cur -= val;
        if(l.size() == k) return;
        if(!s.empty()){
            cur += *s.rbegin();
            l.insert(*s.rbegin());
            s.erase(s.find(*s.rbegin()));
        }
    }
    else{
        s.erase(s.find(val));
    }
}

void dfs(int v, int e){

    for(auto& [to, l] : g[v]){
        if(to == e) continue;
        dfs(to, v);
        mx[v] = max(mx[v], mx[to] + l);
    }

    bool x = false;
    for(auto& [to, l] : g[v]){
        if(to == e) continue;
        if(mx[to] + l == mx[v]){
            if(x) add(mx[to] + l);
            else x = true;
        }
        else{
            add(mx[to] + l);
        }
    }
}

void reroot(int v, int e, ll U){
    ll Mx = U, Mx1 = -1e15;

    ans[v] = cur;
    for(auto& [to, l] : g[v]){
        if(to == e) continue;
        if(mx[to] + l > Mx){
            Mx1 = Mx;
            Mx = mx[to] + l;
        }
        else Mx1 = max(Mx1, mx[to] + l);
    }
    for(auto& [to ,l] : g[v]){
        if(to == e) continue;
        rem(mx[to] + l);
        add(mx[to]);
        if(mx[to] + l == Mx){
            rem(Mx1);
            add(Mx1 + l);
            reroot(to, v, Mx1 + l);
        }
        else rem(Mx), add(Mx + l), reroot(to, v, Mx + l);
        if(mx[to] + l == Mx){
            rem(Mx1 + l);
            add(Mx1);
        }
        else rem(Mx + l), add(Mx);
        add(mx[to] + l);
        rem(mx[to]);
    }
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n;
     cin >> k;
    for(int i = 1; i < n ;i++){
        int u, v, w; cin >> u >> v >> w;
        g[u].pb({v, w});
        g[v].pb({u, w});
    }
    dfs(1, 1);
    //cout << 1;
    add(mx[1]);
    add(0);

    reroot(1, 1, 0);
    for(int i= 1; i <= n ;i++)
        cout << ans[i] << "\n";
    return 0;
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:25:17: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   25 |     if(l.size() > k){
      |        ~~~~~~~~~^~~
Main.cpp: In function 'void rem(long long int)':
Main.cpp:37:21: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |         if(l.size() == k) return;
      |            ~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5028 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5028 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 5044 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5064 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5028 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 5044 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5064 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 6 ms 5176 KB Output is correct
14 Correct 4 ms 5296 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 6 ms 5168 KB Output is correct
17 Correct 5 ms 5172 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 159 ms 17320 KB Output is correct
2 Correct 138 ms 18804 KB Output is correct
3 Correct 127 ms 15176 KB Output is correct
4 Correct 143 ms 17208 KB Output is correct
5 Correct 138 ms 17992 KB Output is correct
6 Correct 183 ms 17268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5032 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5028 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 3 ms 5044 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5064 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 6 ms 5176 KB Output is correct
14 Correct 4 ms 5296 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 6 ms 5168 KB Output is correct
17 Correct 5 ms 5172 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 4 ms 5204 KB Output is correct
20 Correct 159 ms 17320 KB Output is correct
21 Correct 138 ms 18804 KB Output is correct
22 Correct 127 ms 15176 KB Output is correct
23 Correct 143 ms 17208 KB Output is correct
24 Correct 138 ms 17992 KB Output is correct
25 Correct 183 ms 17268 KB Output is correct
26 Correct 203 ms 17640 KB Output is correct
27 Correct 188 ms 18756 KB Output is correct
28 Correct 184 ms 19108 KB Output is correct
29 Correct 147 ms 15400 KB Output is correct
30 Correct 231 ms 17584 KB Output is correct
31 Correct 179 ms 16164 KB Output is correct
32 Correct 204 ms 18208 KB Output is correct
33 Correct 197 ms 17624 KB Output is correct
34 Correct 137 ms 15128 KB Output is correct
35 Correct 174 ms 17676 KB Output is correct
36 Correct 158 ms 19064 KB Output is correct