답안 #538714

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538714 2022-03-17T15:08:29 Z siewjh Paths (RMI21_paths) C++17
100 / 100
345 ms 24172 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100'005;
typedef long long ll;
vector<pair<int, ll>> adjlist[MAXN];
ll maxof[MAXN], ans[MAXN];
int nodes, choose;
ll curr = 0;
multiset<ll> cset, rejset;
void add(ll x){
    curr += x;
    cset.insert(x);
    if (cset.size() > choose){
        auto av = cset.begin();
        curr -= *av;
        rejset.insert(*av);
        cset.erase(av);
    }
}
void rem(ll x){
    if (cset.find(x) == cset.end()) rejset.erase(rejset.find(x));
    else{
        curr -= x;
        cset.erase(cset.find(x));
        if (!rejset.empty()){
            auto av = prev(rejset.end());
            curr += *av;
            cset.insert(*av);
            rejset.erase(av);
        }
    }
}
void dfs(int x, int par){
    for (auto nxt : adjlist[x]){
        int nn = nxt.first; ll nd = nxt.second;
        if (nn == par) continue;
        dfs(nn, x);
        rem(maxof[nn]); add(maxof[nn] + nd);
        maxof[x] = max(maxof[x], maxof[nn] + nd);
    }
    add(0);
}
void dfs2(int x, int par){
    ans[x] = curr;
    vector<pair<ll, int>> comp;
    comp.push_back({0, x});
    for (auto nxt : adjlist[x]) comp.push_back({maxof[nxt.first] + nxt.second, nxt.first});
    sort(comp.begin(), comp.end(), greater<pair<ll, int>>());
    auto f = comp[0], s = comp[1];
    for (auto nxt : adjlist[x]){
        int nn = nxt.first; ll nd = nxt.second;
        if (nn == par) continue;
        ll holder = maxof[x];
        if (f.second == nn) maxof[x] = s.first;
		else maxof[x] = f.first;
		rem(maxof[nn] + nd); add(maxof[nn]);
		rem(maxof[x]); add(maxof[x] + nd);
		dfs2(nn, x);
		rem(maxof[x] + nd); add(maxof[x]);
		rem(maxof[nn]); add(maxof[nn] + nd);
		maxof[x] = holder;
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> nodes >> choose;
    for (int i = 1; i < nodes; i++){
        int a, b; ll c; cin >> a >> b >> c;
        adjlist[a].push_back({b, c});
        adjlist[b].push_back({a, c});
    }
    dfs(1, -1);
    dfs2(1, -1);
    for (int i = 1; i <= nodes; i++) cout << ans[i] << '\n';
    return 0;
}

Compilation message

Main.cpp: In function 'void add(ll)':
Main.cpp:13:21: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   13 |     if (cset.size() > choose){
      |         ~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2672 KB Output is correct
7 Correct 2 ms 2672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2672 KB Output is correct
7 Correct 2 ms 2672 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 4 ms 2944 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 4 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2672 KB Output is correct
7 Correct 2 ms 2672 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 4 ms 2944 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 4 ms 2816 KB Output is correct
13 Correct 5 ms 2864 KB Output is correct
14 Correct 5 ms 3072 KB Output is correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 5 ms 2900 KB Output is correct
17 Correct 5 ms 2900 KB Output is correct
18 Correct 6 ms 2900 KB Output is correct
19 Correct 6 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 16420 KB Output is correct
2 Correct 275 ms 21336 KB Output is correct
3 Correct 225 ms 16292 KB Output is correct
4 Correct 276 ms 16512 KB Output is correct
5 Correct 291 ms 18328 KB Output is correct
6 Correct 272 ms 16324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2672 KB Output is correct
7 Correct 2 ms 2672 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 4 ms 2944 KB Output is correct
10 Correct 3 ms 2772 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 4 ms 2816 KB Output is correct
13 Correct 5 ms 2864 KB Output is correct
14 Correct 5 ms 3072 KB Output is correct
15 Correct 5 ms 2944 KB Output is correct
16 Correct 5 ms 2900 KB Output is correct
17 Correct 5 ms 2900 KB Output is correct
18 Correct 6 ms 2900 KB Output is correct
19 Correct 6 ms 2900 KB Output is correct
20 Correct 279 ms 16420 KB Output is correct
21 Correct 275 ms 21336 KB Output is correct
22 Correct 225 ms 16292 KB Output is correct
23 Correct 276 ms 16512 KB Output is correct
24 Correct 291 ms 18328 KB Output is correct
25 Correct 272 ms 16324 KB Output is correct
26 Correct 345 ms 17548 KB Output is correct
27 Correct 309 ms 22204 KB Output is correct
28 Correct 305 ms 23084 KB Output is correct
29 Correct 241 ms 17352 KB Output is correct
30 Correct 323 ms 17536 KB Output is correct
31 Correct 319 ms 16936 KB Output is correct
32 Correct 331 ms 19716 KB Output is correct
33 Correct 317 ms 17660 KB Output is correct
34 Correct 230 ms 17332 KB Output is correct
35 Correct 332 ms 17576 KB Output is correct
36 Correct 297 ms 24172 KB Output is correct