Submission #541451

# Submission time Handle Problem Language Result Execution time Memory
541451 2022-03-23T16:02:26 Z eecs Paths (RMI21_paths) C++17
68 / 100
600 ms 19600 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 100010;
int n, K;
ll down[maxn], up[maxn], ans[maxn];
vector<array<int, 2>> G[maxn];

namespace DS {
multiset<ll> S;

void ins(ll x) {
    S.insert(x);
}

void del(ll x) {
    S.erase(S.find(x));
}

ll query() {
    ll s = 0;
    auto it = S.rbegin();
    for (int i = 0; i < K && it != S.rend(); i++) s += *it++;
    return s;
}
} // namespace DS

int main() {
    scanf("%d %d", &n, &K);
    for (int i = 1, u, v, w; i < n; i++) {
        scanf("%d %d %d", &u, &v, &w);
        G[u].push_back({v, w}), G[v].push_back({u, w});
    }
    function<void(int, int)> dfs1 = [&](int u, int fa) {
        for (auto &e : G[u]) if (e[0] ^ fa) {
            dfs1(e[0], u), down[u] = max(down[u], down[e[0]] + e[1]);
        }
    };
    function<void(int, int)> dfs2 = [&](int u, int fa) {
        vector<ll> W = {0};
        for (auto &e : G[u]) if (e[0] ^ fa) {
            W.push_back(down[e[0]] + e[1]);
        }
        sort(W.begin(), W.end(), greater<>());
        for (int i = 1; i < W.size(); i++) {
            DS::ins(W[i]);
        }
        for (auto &e : G[u]) if (e[0] ^ fa) {
            up[e[0]] = max(up[u], down[e[0]] + e[1] == W[0] ? W[1] : W[0]) + e[1];
            dfs2(e[0], u);
        }
    };
    function<void(int, int)> dfs3 = [&](int u, int fa) {
        ans[u] = DS::query();
        for (auto &e : G[u]) if (e[0] ^ fa) {
            DS::ins(up[e[0]]), DS::ins(down[e[0]]);
            DS::del(up[e[0]] - e[1]), DS::del(down[e[0]] + e[1]);
            dfs3(e[0], u);
            DS::del(up[e[0]]), DS::del(down[e[0]]);
            DS::ins(up[e[0]] - e[1]), DS::ins(down[e[0]] + e[1]);
        }
    };
    dfs1(1, 0), dfs2(1, 0);
    DS::ins(down[1]), dfs3(1, 0);
    for (int i = 1; i <= n; i++) {
        printf("%lld\n", ans[i]);
    }
    return 0;
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:46:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |         for (int i = 1; i < W.size(); i++) {
      |                         ~~^~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     scanf("%d %d", &n, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:32:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         scanf("%d %d %d", &u, &v, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 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 2644 KB Output is correct
7 Correct 2 ms 2664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 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 2644 KB Output is correct
7 Correct 2 ms 2664 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2700 KB Output is correct
11 Correct 4 ms 2796 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 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 2644 KB Output is correct
7 Correct 2 ms 2664 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2700 KB Output is correct
11 Correct 4 ms 2796 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 14 ms 2912 KB Output is correct
14 Correct 6 ms 3056 KB Output is correct
15 Correct 4 ms 2928 KB Output is correct
16 Correct 11 ms 2900 KB Output is correct
17 Correct 10 ms 2900 KB Output is correct
18 Correct 5 ms 2900 KB Output is correct
19 Correct 18 ms 2912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 270 ms 16672 KB Output is correct
2 Correct 267 ms 19600 KB Output is correct
3 Correct 189 ms 16220 KB Output is correct
4 Correct 268 ms 16596 KB Output is correct
5 Correct 246 ms 17840 KB Output is correct
6 Correct 233 ms 16596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2660 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 2644 KB Output is correct
7 Correct 2 ms 2664 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2700 KB Output is correct
11 Correct 4 ms 2796 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 14 ms 2912 KB Output is correct
14 Correct 6 ms 3056 KB Output is correct
15 Correct 4 ms 2928 KB Output is correct
16 Correct 11 ms 2900 KB Output is correct
17 Correct 10 ms 2900 KB Output is correct
18 Correct 5 ms 2900 KB Output is correct
19 Correct 18 ms 2912 KB Output is correct
20 Correct 270 ms 16672 KB Output is correct
21 Correct 267 ms 19600 KB Output is correct
22 Correct 189 ms 16220 KB Output is correct
23 Correct 268 ms 16596 KB Output is correct
24 Correct 246 ms 17840 KB Output is correct
25 Correct 233 ms 16596 KB Output is correct
26 Execution timed out 1092 ms 15432 KB Time limit exceeded
27 Halted 0 ms 0 KB -