답안 #525113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525113 2022-02-10T18:02:59 Z two_sides Paths (RMI21_paths) C++17
100 / 100
195 ms 22508 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 100005;

ll dp[N], ans[N]; int root, n, k;
vector<pair<int, int>> adj[N];
multiset<ll> big, small;
ll sum_big, sum_small;

void erase(ll x) {
    if (small.count(x)) {
        small.erase(small.find(x));
        sum_small -= x;
    } else {
        big.erase(big.find(x));
        sum_big -= x;
    }
}

void insert(ll x) {
    if (big.empty() || x < *big.begin()) {
        small.insert(x);
        sum_small += x;
    } else {
        big.insert(x);
        sum_big += x;
    }
}

void dfs1(int u) {
    int best = 0;
    for (auto e : adj[u]) {
        int v, w; tie(v, w) = e;
        adj[v].erase(find(adj[v].begin(),
        adj[v].end(), make_pair(u, w)));
        dfs1(v);
        if (dp[u] < dp[v] + w) {
            dp[u] = dp[v] + w; best = v;
        }
    }
    for (auto e : adj[u]) {
        int v, w; tie(v, w) = e;
        if (v != best) insert(dp[v] + w);
    }
}

void adjust() {
    while (big.size() > k) {
        auto it = big.begin();
        sum_big -= *it;
        sum_small += *it;
        big.erase(it);
        small.insert(*it);
    }
    while (small.size() && big.size() < k) {
        auto it = prev(small.end());
        sum_small -= *it;
        sum_big += *it;
        small.erase(it);
        big.insert(*it);
    }
}

void dfs2(int u, ll up) {
    adjust(); ans[u] = sum_big;
    vector<ll> pre(1), suf(1);
    for (int i = 0; i < adj[u].size(); i++) {
        int v, w; tie(v, w) = adj[u][i];
        pre.push_back(max(pre.back(), dp[v] + w));
    }
    for (int i = adj[u].size() - 1; i >= 0; i--) {
        int v, w; tie(v, w) = adj[u][i];
        suf.push_back(max(suf.back(), dp[v] + w));
    }
    reverse(suf.begin(), suf.end());
    for (int i = 0; i < adj[u].size(); i++) {
        int v, w; tie(v, w) = adj[u][i];
        ll best = max({up, pre[i], suf[i + 1]});
        erase(best); insert(best + w);
        erase(dp[v] + w); insert(dp[v]);
        dfs2(v, best + w);
        erase(best + w); insert(best);
        erase(dp[v]); insert(dp[v] + w);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> k;
    if (n == 1) {
        cout << "0\n"; return 0;
    }
    if (n == 2) {
        int u, v, w;
        cin >> u >> v >> w;
        cout << w << '\n' << w << '\n';
        return 0;
    }
    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
    }
    for (int i = 1; i <= n; i++)
        if (adj[i].size() > 1)
            root = i;
    dfs1(root); insert(dp[root]);
    dfs2(root, 0);
    for (int i = 1; i <= n; i++)
        cout << ans[i] << '\n';
}

Compilation message

Main.cpp: In function 'void adjust()':
Main.cpp:52:23: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |     while (big.size() > k) {
      |            ~~~~~~~~~~~^~~
Main.cpp:59:39: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   59 |     while (small.size() && big.size() < k) {
      |                            ~~~~~~~~~~~^~~
Main.cpp: In function 'void dfs2(int, ll)':
Main.cpp:71:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |     for (int i = 0; i < adj[u].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
Main.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     for (int i = 0; i < adj[u].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2812 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 2 ms 2672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2812 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 2 ms 2672 KB Output is correct
13 Correct 4 ms 2840 KB Output is correct
14 Correct 4 ms 3020 KB Output is correct
15 Correct 3 ms 2892 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 4 ms 2848 KB Output is correct
19 Correct 5 ms 2860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 187 ms 13548 KB Output is correct
2 Correct 195 ms 18664 KB Output is correct
3 Correct 146 ms 11204 KB Output is correct
4 Correct 176 ms 13652 KB Output is correct
5 Correct 183 ms 16068 KB Output is correct
6 Correct 180 ms 14000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2672 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 3 ms 2764 KB Output is correct
9 Correct 2 ms 2812 KB Output is correct
10 Correct 2 ms 2764 KB Output is correct
11 Correct 3 ms 2764 KB Output is correct
12 Correct 2 ms 2672 KB Output is correct
13 Correct 4 ms 2840 KB Output is correct
14 Correct 4 ms 3020 KB Output is correct
15 Correct 3 ms 2892 KB Output is correct
16 Correct 4 ms 2796 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 4 ms 2848 KB Output is correct
19 Correct 5 ms 2860 KB Output is correct
20 Correct 187 ms 13548 KB Output is correct
21 Correct 195 ms 18664 KB Output is correct
22 Correct 146 ms 11204 KB Output is correct
23 Correct 176 ms 13652 KB Output is correct
24 Correct 183 ms 16068 KB Output is correct
25 Correct 180 ms 14000 KB Output is correct
26 Correct 174 ms 13952 KB Output is correct
27 Correct 175 ms 18244 KB Output is correct
28 Correct 177 ms 18540 KB Output is correct
29 Correct 130 ms 11368 KB Output is correct
30 Correct 179 ms 14064 KB Output is correct
31 Correct 167 ms 12260 KB Output is correct
32 Correct 186 ms 17868 KB Output is correct
33 Correct 181 ms 13876 KB Output is correct
34 Correct 110 ms 11244 KB Output is correct
35 Correct 175 ms 14100 KB Output is correct
36 Correct 151 ms 22508 KB Output is correct