답안 #1050276

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1050276 2024-08-09T08:28:42 Z underwaterkillerwhale Paths (RMI21_paths) C++17
0 / 100
52 ms 25216 KB
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 1e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9 + 7;
const ll BASE = 137;


struct Edge {
    ll v, w, idx;
};

int n, K, root;
int nleaf = 0;
vector<Edge> ke[N];
int in[N], out[N];
ll D[N];
bool candidate[N];
pair<int,int> par[N];
ll Ans[N];
ll added = 0;

struct Segment_Tree {
    int m;
    pll st[N << 2];
    ll lz[N << 2];

    void init (int n) {
        m = n;
    }

    void down (int id) {
        rep (i, id << 1, id << 1 | 1) {
            st[i].fs += lz[id];
            lz[i] += lz[id];
        }
        lz[id] = 0;
    }

    void update (int id, int l, int r, int u, int v, ll val) {
        if (l > v || r < u) return;
        if (l >= u && r <= v) {
            lz[id] += val;
            st[id].fs += val;
            return;
        }
        down(id);
        int mid = l + r>> 1;
        update (id << 1, l, mid, u, v, val);
        update (id << 1 | 1, mid + 1, r, u, v, val);
        st[id] = max(st[id << 1], st[id << 1 | 1]);
    }

    void Assign (int id, int l, int r, int pos, int lab, ll val) {
        if (l > pos || r < pos) return;
        if (l == r) {
            st[id].fs = val;
            st[id].se = lab;
            return;
        }
        int mid = l + r>> 1;
        Assign (id << 1, l, mid, pos, lab, val);
        Assign (id << 1 | 1, mid + 1, r, pos, lab, val);
        st[id] = max(st[id << 1], st[id << 1 | 1]);
    }
    void update (int u, int v, ll val) {
        update (1, 1, m, u, v, val);
    }
    void Assign (int pos, int lab, ll val) {
        Assign (1, 1, m, pos, lab, val);
    }
}ST;

void pdfs (int u, int p, ll Dist = 0) {
    static int time_dfs = 0;
    if (SZ(ke[u]) == 1) nleaf++;
    in[u] = ++time_dfs;
    ST.Assign (in[u], u, Dist);
    iter (&id, ke[u]) {
        ll v = id.v, w = id.w, idx = id.idx;
        if (v != p) {
            pdfs (v, u, Dist + w);
            par[v] = MP(u, w);
        }
    }
    out[u] = time_dfs;
}

void dfs2 (int u, int p) {
    iter (&id, ke[u]) {
        ll v = id.v, w = id.w, idx = id.idx;
        if (v == p) continue;
        if (candidate[v] == 0) Ans[v] = Ans[u] + w;
        else Ans[v] = Ans[u] + added * (SZ(ke[v]) == 1);
        dfs2 (v, u);
    }
}

void solution () {
    cin >> n >> K;
    rep (i, 1, n - 1) {
        ll u, v, w;
        cin >> u >> v >> w;
        ke[u].push_back({v, w, i});
        ke[v].push_back({u, w, i});
    }
    if (n == 2) {
        rep (i, 1, K) cout << ke[1][0].w <<"\n";
        return;
    }
    if (SZ(ke[1]) == 1) {
        iter (&id, ke[1]) {
            if (SZ(ke[id.v]) > 1) {
                root = id.v;
            }
        }
    }
    else root = 1;
    ST.init(n);
    pdfs(root, 0);
    K = min(K, nleaf);
    ll res = 0;
    candidate[root] = 1;
    rep (step, 1, K) {
        int X = ST.st[1].se;
//        cout << ST.st[1].fs <<" a\n";
        while (X != root) {
            if (candidate[X] == 1) break;
            candidate[X] = 1;
            res += par[X].se;
            ST.update (in[X], out[X], -par[X].se);
            X = par[X].fs;
        }
    }
    Ans[root] = res;
    added = ST.st[1].fs;
    dfs2 (root, 0);
    rep (i, 1, n) cout << Ans[i] <<"\n";
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +2
2 + 8 * 2 - 9

*/

Compilation message

Main.cpp: In member function 'void Segment_Tree::update(int, int, int, int, int, long long int)':
Main.cpp:65:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   65 |         int mid = l + r>> 1;
      |                   ~~^~~
Main.cpp: In member function 'void Segment_Tree::Assign(int, int, int, int, int, long long int)':
Main.cpp:78:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   78 |         int mid = l + r>> 1;
      |                   ~~^~~
Main.cpp: In function 'void pdfs(int, int, long long int)':
Main.cpp:97:32: warning: unused variable 'idx' [-Wunused-variable]
   97 |         ll v = id.v, w = id.w, idx = id.idx;
      |                                ^~~
Main.cpp: In function 'void dfs2(int, int)':
Main.cpp:108:32: warning: unused variable 'idx' [-Wunused-variable]
  108 |         ll v = id.v, w = id.w, idx = id.idx;
      |                                ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Incorrect 2 ms 13148 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Incorrect 2 ms 13148 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Incorrect 2 ms 13148 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Incorrect 2 ms 13148 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 52 ms 25216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Incorrect 2 ms 13148 KB Output isn't correct
3 Halted 0 ms 0 KB -