Submission #676167

#TimeUsernameProblemLanguageResultExecution timeMemory
676167vovamrPaths (RMI21_paths)C++17
68 / 100
1085 ms30336 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

const int N = 1e5 + 10;
const int lg = 17;

int n, k;
ve<pii> gr[N];

int up[N][lg];
ll d[N], act[N];

int mx[N], mxu[N];

ll sum = 0;
multiset<ll> st;

inline void add(const ll &x) {
	st.insert(x);
}
inline void del(const ll &x) {
	st.erase(st.find(x));
}
inline ll get() {
	auto it = --st.end();
	int cnt = 0; ll s = 0;
	while (cnt < k) {
		s += *it; --it;
		++cnt;
	} return s;
}

inline void dfs(int v, int p) {
	if (v == p) d[v] = 0;

	up[v][0] = p;
	for (int i = 1; i < lg; ++i) up[v][i] = up[up[v][i - 1]][i - 1];

	mx[v] = v;
	for (auto &[to, w] : gr[v]) {
		if (to == p) continue;
		d[to] = d[v] + w;
		dfs(to, v);
		if (d[mx[to]] > d[mx[v]]) mx[v] = mx[to];
	}
}
inline void dfs1(int v, int p) {
	if (sz(gr[v]) == 1) {
		int u = v;
		for (int i = lg - 1; ~i; --i) {
			if (mx[up[u][i]] == v) {
				u = up[u][i];
			}
		}
		act[v] = d[v] - d[up[u][0]];
	}
	else act[v] = 0;

	for (auto &[to, w] : gr[v]) {
		if (to == p) continue;
		dfs1(to, v);
	}
}

pair<pll,pll> dpd[N];

inline void dfs2(int v, int p) {
	dpd[v] = {{-1, -1}, {-1, -1}};
	if (sz(gr[v]) == 1 && v != p) dpd[v].fi = {0, v};

	for (auto &[to, w] : gr[v]) {
		if (to == p) continue;
		dfs2(to, v);

		chmax(dpd[v].se, mpp(dpd[to].fi.fi + w, dpd[to].fi.se));
		if (dpd[v].se > dpd[v].fi) swap(dpd[v].fi, dpd[v].se);
	}
}

pll dpu[N];
inline void dfs3(int v, int p, int pw) {
	if (v == p) dpu[v] = {0, v};
	else {
		chmax(dpu[v], mpp(dpu[p].fi + pw, dpu[p].se));

		if (dpd[p].fi.fi == dpd[v].fi.fi + pw) {
			chmax(dpu[v], mpp(dpd[p].se.fi + pw, dpd[p].se.se));
		}
		else {
			chmax(dpu[v], mpp(dpd[p].fi.fi + pw, dpd[p].fi.se));
		}
	}

	for (auto &[to, w] : gr[v]) {
		if (to == p) continue;
		dfs3(to, v, w);
	}
}
inline void trans() {
	for (int i = 0; i < n; ++i) {
		mxu[i] = dpu[i].se;
	}
}

ll answer[N];

inline void dfs_reroot(int v, int p) {
	answer[v] = get();

	for (auto &[to, w] : gr[v]) {
		if (to == p) continue;

		del(act[mx[to]]);
		del(act[mxu[to]]);
		act[mx[to]] -= w;
		act[mxu[to]] += w;
		add(act[mx[to]]);
		add(act[mxu[to]]);

		dfs_reroot(to, v);

		del(act[mx[to]]);
		del(act[mxu[to]]);
		act[mxu[to]] -= w;
		act[mx[to]] += w;
		add(act[mx[to]]);
		add(act[mxu[to]]);
	}
}

inline void solve() {
	cin >> n >> k;
	for (int i = 1; i < n; ++i) {
		int v, u, c;
		cin >> v >> u >> c, --v, --u;
		gr[v].pb({u, c}), gr[u].pb({v, c});
	}

	dfs(0, 0);
	dfs1(0, 0);
	dfs2(0, 0);
	dfs3(0, 0, 0);
	trans();

/*
	for (int i = 0; i < n; ++i) {
		cout << "vertex: " << i + 1 << ":" << '\n';
		cout << "deepest: (" << dpd[i].fi.se + 1 << " = " << dpd[i].fi.fi << "), ";
		cout << "(" << dpd[i].se.se + 1 << " = " << dpd[i].se.fi << ")" << '\n';
		cout << "going up: (" << dpu[i].se + 1 << " = " << dpu[i].fi << ")" << '\n';
	}
*/

	for (int i = 0; i < n; ++i) {
		add(act[i]);
	}
	dfs_reroot(0, 0);

	for (int i = 0; i < n; ++i) cout << answer[i] << '\n';
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...