//-------------dilanyan------------\\
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
//------------------Kargpefines--------------------\\
#define ll long long
#define pb push_back
#define all(u) (u).begin(), (u).end()
#define pqueue priority_queue
#define upper upper_bound
#define lower lower_bound
#define umap unordered_map
#define uset unordered_set
void KarginSet(string name = "") {
ios_base::sync_with_stdio(false); cin.tie(NULL);
if (name.size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
//-------------------KarginConstants------------------\\
const ll mod = 1e9 + 7;
const ll inf = 2e9;
//-------------------KarginCode-----------------------\\
const int N = 70005, M = 1000005;
ll n, k;
vector<pair<int, int>> g[N];
ll ans[N];
int s[N];
vector<int> ord;
void dfs(int u, int p) {
ord.pb(u);
for (auto it : g[u]) {
int v = it.first;
if (v == p) continue;
dfs(v, u);
}
}
ll l[N], r[N];
ll pref[N], suf[N];
void KarginSolve() {
cin >> n >> k;
map<pair<int, int>, ll> mp;
for (int i = 0;i < n - 1;i++) {
int u, v, l;
cin >> u >> v >> l;
g[u].pb({ v,l }), g[v].pb({ u,l });
mp[{u, v}] = mp[{v, u}] = l;
}
for (int i = 0;i < n;i++) {
if (g[i].size() == 1) {
dfs(i, -1);
break;
}
}
for (int i = 1;i < n;i++) {
pref[i] = pref[i - 1] + mp[{ord[i], ord[i - 1]}];
}
for (int i = n - 2;i >= 0;i--) {
suf[i] = suf[i + 1] + mp[{ord[i], ord[i + 1]}];
}
for (int i = 0;i < n;i++) l[i] = 1;
for (int i = 0;i < n - 1;i++) {
int lx = i + 1, rx = n - 1, j = i + 1;
while (lx <= rx) {
int m = (lx + rx) / 2;
if (pref[m] - pref[i] <= k) {
lx = m + 1;
j = m;
}
else rx = m - 1;
}
l[j] += l[i];
}
for (int i = 0; i < n;i++) r[i] = 1;
for (int i = n - 1;i > 0;i--) {
int lx = 0, rx = i - 1, j = i - 1;
while (lx <= rx) {
int m = (lx + rx) / 2;
if (suf[m] - suf[i] <= k) {
rx = m - 1;
j = m;
}
else lx = m + 1;
}
r[j] += r[i];
}
for (int i = 0;i < n;i++) {
ans[ord[i]] = (l[i] - 1) * (n - 1 - i) + (r[i] - 1) * i;
}
for (int i = 0;i < n;i++) cout << ans[i] << '\n';
}
int main() {
KarginSet();
int test = 1;
//cin >> test;
while (test--) {
KarginSolve();
}
return 0;
}
Compilation message
Main.cpp:1:1: warning: multi-line comment [-Wcomment]
1 | //-------------dilanyan------------\\
| ^
Main.cpp:8:1: warning: multi-line comment [-Wcomment]
8 | //------------------Kargpefines--------------------\\
| ^
Main.cpp:27:1: warning: multi-line comment [-Wcomment]
27 | //-------------------KarginConstants------------------\\
| ^
Main.cpp:32:1: warning: multi-line comment [-Wcomment]
32 | //-------------------KarginCode-----------------------\\
| ^
Main.cpp: In function 'void KarginSet(std::string)':
Main.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4960 KB |
Output is correct |
2 |
Correct |
134 ms |
20332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4960 KB |
Output is correct |
4 |
Correct |
134 ms |
20332 KB |
Output is correct |
5 |
Correct |
130 ms |
20440 KB |
Output is correct |
6 |
Correct |
113 ms |
20816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
104 ms |
18040 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
104 ms |
18040 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4952 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |