답안 #537654

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537654 2022-03-15T10:57:41 Z maomao90 Paths (RMI21_paths) C++17
100 / 100
252 ms 13900 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, int> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

#define INF 1000000005
#define LINF 1000000000000000005ll
#define MAXN 100005

int n, k;
vii adj[MAXN];

multiset<ll> st;
multiset<ll, greater<ll>> ot;
ll cur;
void insert(ll x) {
    st.insert(x);
    cur += x;
    if (st.size() > k) {
        cur -= *st.begin();
        ot.insert(*st.begin());
        st.erase(st.begin());
    }
}
void erase(ll x) {
    auto it = st.find(x);
    if (it != st.end()) {
        cur -= x;
        st.erase(it);
        if (!ot.empty()) {
            cur += *ot.begin();
            st.insert(*ot.begin());
            ot.erase(ot.begin());
        }
    } else {
        //if (ot.find(x) == ot.end()) return;
        ot.erase(ot.find(x));
    }
}

ll mx[MAXN];
void dfs(int u, int p) {
    mx[u] = 0;
    bool leaf = 1;
    for (auto [v, w] : adj[u]) {
        if (v == p) continue;
        leaf = 0;
        dfs(v, u);
        cerr << u << ' ' << v << '\n';
        erase(mx[v]);
        cerr << u << ' ' << v << '\n';
        insert(mx[v] + w);
        mxto(mx[u], mx[v] + w);
    }
    if (leaf) {
        insert(0);
    }
}

ll ans[MAXN];
void reroot(int u, int p) {
    pll fmx = MP(0, u), smx = MP(0, u);
    for (auto [v, w] : adj[u]) {
        if (mx[v] + w >= fmx.FI) {
            smx = fmx;
            fmx = MP(mx[v] + w, v);
        } else if (mx[v] + w > smx.FI) {
            smx = MP(mx[v] + w, v);
        }
    }
    cerr << u << ": " << cur << '\n';
    for (int i : st) {
        cerr << i << ' ';
    }
    cerr << '\n';
    ans[u] = cur;
    for (auto [v, w] : adj[u]) {
        if (v == p) continue;
        insert(mx[v]);
        erase(mx[v] + w);
        ll omx = mx[u];
        if (v == fmx.SE) {
            mx[u] = smx.FI;
        } else {
            mx[u] = fmx.FI;
        }
        insert(mx[u] + w);
        if (mx[u] > 0) {
            erase(mx[u]);
        }
        reroot(v, u);
        if (mx[u] > 0) {
            insert(mx[u]);
        }
        erase(mx[u] + w);
        mx[u] = omx;
        insert(mx[v] + w);
        erase(mx[v]);
    }
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n >> k;
    REP (i, 1, n) {
        int a, b, c; cin >> a >> b >> c;
        adj[a].pb(MP(b, c));
        adj[b].pb(MP(a, c));
    }
    dfs(1, -1);
    reroot(1, -1);
    REP (i, 1, n + 1) {
        cout << ans[i] << '\n';
    }
    return 0;
}

Compilation message

Main.cpp: In function 'void insert(ll)':
Main.cpp:46:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   46 |     if (st.size() > k) {
      |         ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2644 KB Output is correct
8 Correct 4 ms 2644 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2644 KB Output is correct
8 Correct 4 ms 2644 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 5 ms 2772 KB Output is correct
14 Correct 4 ms 2900 KB Output is correct
15 Correct 6 ms 2772 KB Output is correct
16 Correct 5 ms 2772 KB Output is correct
17 Correct 4 ms 2772 KB Output is correct
18 Correct 4 ms 2772 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 225 ms 11408 KB Output is correct
2 Correct 226 ms 13496 KB Output is correct
3 Correct 141 ms 9100 KB Output is correct
4 Correct 207 ms 11444 KB Output is correct
5 Correct 206 ms 12416 KB Output is correct
6 Correct 208 ms 11668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 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 2644 KB Output is correct
8 Correct 4 ms 2644 KB Output is correct
9 Correct 3 ms 2772 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 5 ms 2772 KB Output is correct
14 Correct 4 ms 2900 KB Output is correct
15 Correct 6 ms 2772 KB Output is correct
16 Correct 5 ms 2772 KB Output is correct
17 Correct 4 ms 2772 KB Output is correct
18 Correct 4 ms 2772 KB Output is correct
19 Correct 4 ms 2772 KB Output is correct
20 Correct 225 ms 11408 KB Output is correct
21 Correct 226 ms 13496 KB Output is correct
22 Correct 141 ms 9100 KB Output is correct
23 Correct 207 ms 11444 KB Output is correct
24 Correct 206 ms 12416 KB Output is correct
25 Correct 208 ms 11668 KB Output is correct
26 Correct 252 ms 11868 KB Output is correct
27 Correct 251 ms 13516 KB Output is correct
28 Correct 245 ms 13900 KB Output is correct
29 Correct 163 ms 9328 KB Output is correct
30 Correct 246 ms 11932 KB Output is correct
31 Correct 243 ms 10772 KB Output is correct
32 Correct 251 ms 12628 KB Output is correct
33 Correct 248 ms 11796 KB Output is correct
34 Correct 145 ms 9036 KB Output is correct
35 Correct 245 ms 11892 KB Output is correct
36 Correct 201 ms 13900 KB Output is correct