답안 #786141

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
786141 2023-07-18T04:47:34 Z 반딧불(#10026) Paths (RMI21_paths) C++17
8 / 100
600 ms 14552 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, k;
vector<pair<int, ll> > link[100002];
multiset<ll> st[100002];

void add(multiset<ll> &A, multiset<ll> &B){
    if(A.size() < B.size()) A.swap(B);
    for(int x: B){
        A.insert(x);
    }
    B.clear();
    while((int)A.size() > k) A.erase(A.begin());
}

void dfs(int x, int p=-1){
    for(auto y: link[x]){
        if(y.first==p) continue;
        dfs(y.first, x);
        ll tmp = *st[y.first].rbegin();
        st[y.first].erase(prev(st[y.first].end()));
        st[y.first].insert(tmp+y.second);

        add(st[x], st[y.first]);
    }
    if(st[x].empty()){
        st[x].insert(0);
        return;
    }
}

int main(){
    scanf("%d %d", &n, &k);
    for(int i=1; i<n; i++){
        int x, y; ll c;
        scanf("%d %d %lld", &x, &y, &c);
        link[x].push_back(make_pair(y, c));
        link[y].push_back(make_pair(x, c));
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++) st[j].clear();
        dfs(i);
        ll sum = 0;
        for(auto x: st[i]) sum+=x;
        printf("%lld\n", sum);
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         scanf("%d %d %lld", &x, &y, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7340 KB Output is correct
3 Incorrect 8 ms 7252 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7340 KB Output is correct
3 Incorrect 8 ms 7252 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7340 KB Output is correct
3 Incorrect 8 ms 7252 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1064 ms 14552 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7340 KB Output is correct
3 Incorrect 8 ms 7252 KB Output isn't correct
4 Halted 0 ms 0 KB -