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;
using ll = long long;
vector<vector<pair<int, int>>> g;
void dfs(int v, int p, vector<ll>& d) {
for (auto [x, w] : g[v]) {
if (x == p) continue;
d[x] = d[v] + w;
dfs(x, v, d);
}
}
int main() {
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
g.resize(n + 1);
for (int i = 0; i < n - 1; i++) {
int u, v, c;
cin >> u >> v >> c;
g[u].push_back({v, c});
g[v].push_back({u, c});
}
vector<ll> da(n + 1), db(n + 1);
dfs(1, 0, da);
int a = max_element(da.begin(), da.end()) - da.begin();
da.assign(n + 1, 0);
dfs(a, 0, da);
int b = max_element(da.begin(), da.end()) - da.begin();
dfs(b, 0, db);
for (int i = 1; i <= n; i++) {
cout << max(da[i], db[i]) << "\n";
}
return 0;
}
# | 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... |