답안 #525930

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525930 2022-02-13T08:37:03 Z Josia Paths (RMI21_paths) C++14
36 / 100
600 ms 28704 KB
#include <bits/stdc++.h>

using namespace std;
#define int int64_t



vector<pair<int, int>> parents;
vector<vector<pair<int, int>>> graph;
vector<int> order;
int n, k;


void generateParents(int v, int p, int weight) {
    if (parents[v].first != -1) return;

    parents[v].first = p;
    parents[v].second = weight;

    for (auto u: graph[v]) {
        if (parents[u.first].first != -1) continue;
        generateParents(u.first, v, u.second);
    }
    order.push_back(v);
}



void merge(multiset<int> &A, multiset<int> &B) {
    if (A.size() > B.size()) A.swap(B);
    for (int e: A) B.insert(e);
}



int stl(int v) {
    vector<multiset<int>> tree(n);  // offset, options

    for (int i = 0; i<n; i++) {
        if (graph[i].size() == 1 && i!=v) tree[i].insert(0);
    }

    order.pop_back();

    for (int i: order) {
        int newElem = *tree[i].rbegin() + parents[i].second;
        tree[i].erase(*tree[i].rbegin());
        tree[i].insert(newElem);

        merge(tree[i], tree[parents[i].first]);
    }

    // cerr << "root element: ";

    // for (int i: tree[v]) {
    //     cerr << i << " ";
    // } cerr << "\n";


    int res = 0;

    auto it = tree[v].end();

    for (int i = 0; i<k; i++) {
        it--;
        res += *it;
    }

    return res;
}



void solve() {
    
    cin >> n >> k;
    graph.resize(n);

    for (int i = 0; i<n-1; i++) {
        int a, b, c; cin >> a >> b >> c;
        a--; b--;
        graph[a].push_back({b, c});
        graph[b].push_back({a, c});
    }


    for (int i = 0; i<n; i++) {
        // cerr << i << " ----------------------\n";

        parents.clear();
        parents.assign(n, {-1, 0});
        order.clear();

        generateParents(i, -2, 0);

        // cerr << "order: ";
        // for (auto i: order) {
        //     cerr << i << " ";
        // } cerr << "\n";

        // cerr << "parents: ";
        // for (auto i: parents) {
        //     cerr << i.first << " ";
        // } cerr << "\n";

        assert(order.back() == i);

        int res = stl(i);

        cout << res << "\n";
        // cerr << "result: " << res << "\n";
    }
    


}

signed main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 9 ms 356 KB Output is correct
4 Correct 7 ms 348 KB Output is correct
5 Correct 8 ms 356 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 6 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 9 ms 356 KB Output is correct
4 Correct 7 ms 348 KB Output is correct
5 Correct 8 ms 356 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 6 ms 332 KB Output is correct
8 Correct 287 ms 548 KB Output is correct
9 Correct 158 ms 512 KB Output is correct
10 Correct 100 ms 480 KB Output is correct
11 Correct 182 ms 644 KB Output is correct
12 Correct 142 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 9 ms 356 KB Output is correct
4 Correct 7 ms 348 KB Output is correct
5 Correct 8 ms 356 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 6 ms 332 KB Output is correct
8 Correct 287 ms 548 KB Output is correct
9 Correct 158 ms 512 KB Output is correct
10 Correct 100 ms 480 KB Output is correct
11 Correct 182 ms 644 KB Output is correct
12 Correct 142 ms 492 KB Output is correct
13 Execution timed out 1077 ms 940 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1051 ms 28704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 9 ms 356 KB Output is correct
4 Correct 7 ms 348 KB Output is correct
5 Correct 8 ms 356 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 6 ms 332 KB Output is correct
8 Correct 287 ms 548 KB Output is correct
9 Correct 158 ms 512 KB Output is correct
10 Correct 100 ms 480 KB Output is correct
11 Correct 182 ms 644 KB Output is correct
12 Correct 142 ms 492 KB Output is correct
13 Execution timed out 1077 ms 940 KB Time limit exceeded
14 Halted 0 ms 0 KB -