# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
525676 |
2022-02-12T13:46:44 Z |
Josia |
Paths (RMI21_paths) |
C++17 |
|
600 ms |
351376 KB |
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
vector<pair<int, int>> parents;
vector<vector<pair<int, int>>> graph;
vector<int> order;
int n, k;
void generateParents(int v, int p, int weight) {
if (parents[v].first != -1) return;
parents[v].first = p;
parents[v].second = weight;
for (auto u: graph[v]) {
if (parents[u.first].first != -1) continue;
generateParents(u.first, v, u.second);
}
order.push_back(v);
}
int stl(int v) {
vector<multiset<int>> tree(n, {0}); // offset, options
vector<int> pointers;
for (int i = 0; i<n; i++) pointers.push_back(i);
order.pop_back();
for (int i: order) {
int newElem = *prev(tree[pointers[i]].end()) + parents[i].second;
tree[pointers[i]].erase(*prev(tree[pointers[i]].end()));
tree[pointers[i]].insert(newElem);
if (tree[pointers[i]].size() < tree[pointers[parents[i].first]].size()) {
swap(pointers[i], pointers[parents[i].first]);
}
auto it = tree[pointers[i]].begin();
while (it != tree[pointers[i]].end()) {
tree[pointers[parents[i].first]].insert(*it);
it = next(it);
}
}
cerr << "root element: ";
for (int i: tree[pointers[v]]) {
cerr << i << " ";
} cerr << "\n";
int res = 0;
auto it = tree[pointers[v]].end();
for (int i = 0; i<k; i++) {
it = prev(it);
res += *it;
}
return res;
}
void solve() {
cin >> n >> k;
graph.resize(n);
for (int i = 0; i<n-1; i++) {
int a, b, c; cin >> a >> b >> c;
a--; b--;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
for (int i = 0; i<n; i++) {
// cerr << i << " ----------------------\n";
parents.clear();
parents.assign(n, {-1, 0});
order.clear();
generateParents(i, -2, 0);
// cerr << "order: ";
// for (auto i: order) {
// cerr << i << " ";
// } cerr << "\n";
// cerr << "parents: ";
// for (auto i: parents) {
// cerr << i.first << " ";
// } cerr << "\n";
assert(order.back() == i);
int res = stl(i);
cout << res << "\n";
// cerr << "result: " << res << "\n";
}
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
160 ms |
656 KB |
Output is correct |
4 |
Correct |
290 ms |
1128 KB |
Output is correct |
5 |
Correct |
297 ms |
980 KB |
Output is correct |
6 |
Correct |
208 ms |
772 KB |
Output is correct |
7 |
Correct |
167 ms |
708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
160 ms |
656 KB |
Output is correct |
4 |
Correct |
290 ms |
1128 KB |
Output is correct |
5 |
Correct |
297 ms |
980 KB |
Output is correct |
6 |
Correct |
208 ms |
772 KB |
Output is correct |
7 |
Correct |
167 ms |
708 KB |
Output is correct |
8 |
Execution timed out |
1082 ms |
3668 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
160 ms |
656 KB |
Output is correct |
4 |
Correct |
290 ms |
1128 KB |
Output is correct |
5 |
Correct |
297 ms |
980 KB |
Output is correct |
6 |
Correct |
208 ms |
772 KB |
Output is correct |
7 |
Correct |
167 ms |
708 KB |
Output is correct |
8 |
Execution timed out |
1082 ms |
3668 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1105 ms |
351376 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
160 ms |
656 KB |
Output is correct |
4 |
Correct |
290 ms |
1128 KB |
Output is correct |
5 |
Correct |
297 ms |
980 KB |
Output is correct |
6 |
Correct |
208 ms |
772 KB |
Output is correct |
7 |
Correct |
167 ms |
708 KB |
Output is correct |
8 |
Execution timed out |
1082 ms |
3668 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |