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;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vb;
typedef set<ll> sll;
#define IOS cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false)
#define INF(dtype) numeric_limits<dtype>::max()
#define NINF(dtype) numeric_limits<dtype>::min()
#define fi first
#define se second
#define pb push_back
typedef vector<vb> vvb;
typedef vector<vvb> v3b;
typedef vector<v3b> v4b;
typedef vector<string> vs;
// struct state {
// int i, p;
// int fuel;
// };
// void compute_depth(int i, int d, const vvi& adj, vi& depth) {
// depth[i] = d;
// // cout << i << " " << d << endl;
// for(int j : adj[i]) {
// compute_depth(j, d + 1, adj, depth);
// }
// }
// int compute_size(int i, const vvi& adj, vi& tree_sz) {
// int& ans = tree_sz[i];
// ans += 1;
// for(int j : adj[i]) {
// ans += compute_size(j, adj, tree_sz);
// }
// return ans;
// }
ll solve(ll i, ll n, ll k) {
ll nl = i / k;
ll nr = (n - i - 1) / k;
return nl * (n - i - 1) + nr * i;
}
vll solve(ll n, ll k, const vvpll& adj) {
vll ans(n, 0ll);
// Find the starting point (i.e., the point of outdegree 1)
ll s = 0;
for(ll i = 0; i < n; i++) {
if(adj[i].size() == 1) {
s = i;
break;
}
}
ll ind = 0;
ans[s] = solve(0, n, k);
ll prev = s;
for(ll i = 0; i < n - 1; i++) {
// Goto next
// cout << s << " " << prev << endl;
if(adj[s][0].fi != prev) {
prev = s;
s = adj[s][0].fi;
} else {
prev = s;
s = adj[s][1].fi;
}
ind++;
ans[s] = solve(ind, n, k);
// cout << s << " ";
}
// cout << endl;
return ans;
}
int main() {
ll n, k; cin >> n >> k;
vvpll adj;
for(ll i = 0; i < n; i++) {
vpll adjr;
adj.pb(adjr);
}
for(ll i = 0; i < n - 1; i++) {
ll u, v, l;
cin >> u >> v >> l;
adj[u].pb({v, l});
adj[v].pb({u, l});
}
vll ans = solve(n, k, adj);
for(ll v : ans) cout << v << "\n";
cout << flush;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |