답안 #539114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
539114 2022-03-18T12:24:28 Z joelau Paths (RMI21_paths) C++14
100 / 100
328 ms 33104 KB
#include <bits/stdc++.h>
using namespace std;

long long N,K, id[100005], sum = 0, ans[100005], chain[100005];
vector< pair<long long,long long> > lst[100005];
multiset<long long> dp[100005], low, high;

void add (long long x) {
    if (x <= *high.begin()) low.insert(x);
    else {
        high.insert(x);
        sum += x;
        while (high.size() > K) {
            long long v = *high.begin(); high.erase(high.begin());
            sum -= v;
            low.insert(v);
        }
    }
}

void rmv (long long x) {
    if (low.find(x) != low.end()) low.erase(low.find(x));
    else if (high.find(x) != high.end()){
        high.erase(high.find(x));
        sum -= x;
        while (!low.empty() && high.size() < K) {
            long long v = *prev(low.end()); low.erase(prev(low.end()));
            high.insert(v);
            sum += v;
        }
    }
}

void dfs (long long x, long long p) {
    tuple<long long,long long,long long> most = make_tuple(-1,-1,-1);
    for (auto v: lst[x]) if (v.first != p) {
        dfs(v.first,x);
        most = max(most,make_tuple((long long)dp[id[v.first]].size(),v.first,v.second));
    }
    if (most == make_tuple(-1,-1,-1)) {
        id[x] = x;
        dp[x].insert(0);
    }
    else {
        long long a = get<1>(most), b = get<2>(most);
        id[x] = id[a];
        long long tmp = *prev(dp[id[x]].end());
        dp[id[x]].erase(prev(dp[id[x]].end()));
        dp[id[x]].insert(tmp+b);
        chain[a] = tmp+b;
        for (auto v: lst[x]) if (v.first != p && v.first != a) {
            for (auto it = dp[id[v.first]].begin(); it != prev(dp[id[v.first]].end()); ++it) dp[id[x]].insert(*it);
            long long tmp = *prev(dp[id[v.first]].end()) + v.second;
            dp[id[x]].insert(tmp);
            chain[v.first] = tmp;
        }
    }
}

void dfs2 (long long x, long long p, long long pchain) {
    ans[x] = sum;
    long long most = pchain, secondmost = 0;
    for (auto v: lst[x]) if (v.first != p) {
        if (chain[v.first] > most) secondmost = most, most = chain[v.first];
        else if (chain[v.first] > secondmost) secondmost = chain[v.first];
    }
    for (auto v: lst[x]) if (v.first != p) {
        long long nchain = most + v.second;
        if (chain[v.first] == most) nchain = secondmost + v.second;
        add(chain[v.first] - v.second);
        rmv(chain[v.first]);
        add(nchain);
        rmv(nchain - v.second);
        dfs2(v.first,x,nchain);
        add(nchain - v.second);
        rmv(nchain);
        add(chain[v.first]);
        rmv(chain[v.first] - v.second);
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> K;
    for (long long i = 0; i < N-1; ++i) {
        long long u,v,w; cin >> u >> v >> w; u--, v--;
        lst[u].emplace_back(v,w), lst[v].emplace_back(u,w);
    }
    dfs(0,-1);
    long long cnt = 0;
    for (auto it = dp[id[0]].begin(); it != dp[id[0]].end(); ++it, ++cnt) {
        if (dp[id[0]].size() - cnt <= K) high.insert(*it), sum += *it;
        else low.insert(*it);
    }
    dfs2(0,-1,0);
    for (long long i = 0; i < N; ++i) cout << ans[i] << '\n';

    return 0;
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:13:28: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   13 |         while (high.size() > K) {
      |                ~~~~~~~~~~~~^~~
Main.cpp: In function 'void rmv(long long int)':
Main.cpp:26:44: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   26 |         while (!low.empty() && high.size() < K) {
      |                                ~~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:92:36: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'long long int' [-Wsign-compare]
   92 |         if (dp[id[0]].size() - cnt <= K) high.insert(*it), sum += *it;
      |             ~~~~~~~~~~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7376 KB Output is correct
5 Correct 4 ms 7376 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7376 KB Output is correct
5 Correct 4 ms 7376 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 6 ms 7508 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 6 ms 7520 KB Output is correct
11 Correct 6 ms 7508 KB Output is correct
12 Correct 5 ms 7516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7376 KB Output is correct
5 Correct 4 ms 7376 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 6 ms 7508 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 6 ms 7520 KB Output is correct
11 Correct 6 ms 7508 KB Output is correct
12 Correct 5 ms 7516 KB Output is correct
13 Correct 8 ms 7776 KB Output is correct
14 Correct 7 ms 7772 KB Output is correct
15 Correct 7 ms 7636 KB Output is correct
16 Correct 7 ms 7756 KB Output is correct
17 Correct 10 ms 7568 KB Output is correct
18 Correct 8 ms 7636 KB Output is correct
19 Correct 8 ms 7784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 296 ms 32564 KB Output is correct
2 Correct 261 ms 29120 KB Output is correct
3 Correct 161 ms 20364 KB Output is correct
4 Correct 270 ms 27232 KB Output is correct
5 Correct 292 ms 28928 KB Output is correct
6 Correct 259 ms 26944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7376 KB Output is correct
5 Correct 4 ms 7376 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 6 ms 7508 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 6 ms 7520 KB Output is correct
11 Correct 6 ms 7508 KB Output is correct
12 Correct 5 ms 7516 KB Output is correct
13 Correct 8 ms 7776 KB Output is correct
14 Correct 7 ms 7772 KB Output is correct
15 Correct 7 ms 7636 KB Output is correct
16 Correct 7 ms 7756 KB Output is correct
17 Correct 10 ms 7568 KB Output is correct
18 Correct 8 ms 7636 KB Output is correct
19 Correct 8 ms 7784 KB Output is correct
20 Correct 296 ms 32564 KB Output is correct
21 Correct 261 ms 29120 KB Output is correct
22 Correct 161 ms 20364 KB Output is correct
23 Correct 270 ms 27232 KB Output is correct
24 Correct 292 ms 28928 KB Output is correct
25 Correct 259 ms 26944 KB Output is correct
26 Correct 314 ms 32628 KB Output is correct
27 Correct 328 ms 29192 KB Output is correct
28 Correct 292 ms 29772 KB Output is correct
29 Correct 192 ms 20696 KB Output is correct
30 Correct 313 ms 27392 KB Output is correct
31 Correct 320 ms 27088 KB Output is correct
32 Correct 285 ms 28808 KB Output is correct
33 Correct 280 ms 33104 KB Output is correct
34 Correct 139 ms 19012 KB Output is correct
35 Correct 267 ms 27516 KB Output is correct
36 Correct 290 ms 28812 KB Output is correct