# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
891237 | fanwen | Paths (RMI21_paths) | C++17 | 1028 ms | 15704 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define file(name) \
if(fopen(name".inp", "r")) \
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
const int MAX = 3e5 + 5;
int n, k;
long long dp[MAX];
vector <pair <int, int>> adj[MAX];
void dfs(int u, int p, vector <long long> &res) {
for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
int v = tmp.fi, w = tmp.se;
dfs(v, u, res);
dp[u] = max(dp[u], dp[v] + w);
}
bool oke = false;
for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
int v = tmp.fi, w = tmp.se;
if(dp[u] != dp[v] + w || oke) res.emplace_back(dp[v] + w);
else oke = true;
}
}
void you_make_it(void) {
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
for (int i = 1; i <= n; ++i) {
fill(dp + 1, dp + n + 1, 0);
vector <long long> res;
dfs(i, 0, res);
res.push_back(dp[i]);
sort(res.begin(), res.end(), greater <long long>());
long long sum = 0;
for (int j = 0; j < min((int) res.size(), k); ++j) sum += res[j];
cout << sum << '\n';
}
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("paths");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
컴파일 시 표준 에러 (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... |