# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1108695 | _fractal | Paths (RMI21_paths) | C++14 | 1041 ms | 37204 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define make_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 Rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e6 + 200;
const int M = 1e6;
const int inf = 2e9 + 3;
const ll INF = 1e18;
int n, k;
vector<pair<int, int>> g[N];
int cw[N];
long long cost[N];
int anc[N];
int r;
void dfs(int v = r, int p = 0, long long W = 0, int last = 0) {
anc[v] = p;
cw[v] = last;
cost[v] = W;
for (auto [to, w] : g[v]) {
if (to == p) continue;
dfs(to, v, W + w, w);
}
}
void redfs(int v = r, int p = 0) {
for (auto [to, w] : g[v]) {
if (to == p) continue;
cost[to] = cost[v] + cw[to];
redfs(to, v);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1, v, u, c; i < n; ++i) {
cin >> v >> u >> c;
g[v].emplace_back(u, c);
g[u].emplace_back(v, c);
}
for (r = 1; r <= n; ++r) {
for (int i = 1; i <= n; ++i) {
cost[i] = cw[i] = anc[i] = 0;
}
dfs();
long long ans = 0;
for (int T = 1; T <= k; ++T) {
int mx = 0;
for (int i = 1; i <= n; ++i) {
if (cost[i] > cost[mx])
mx = i;
}
ans += cost[mx];
while (mx) {
cw[mx] = 0;
mx = anc[mx];
}
redfs();
}
cout << ans << '\n';
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |