#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template<typename T>
bool minimize(T& a, const T& b){
if(a > b){
return a = b, true;
}
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b){
return a = b, true;
}
return false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;
const int MAX = 1e5 + 5;
int N, K;
vector<pair<int, int>> adj[MAX];
namespace subtask1{
bool check(){
return N <= 18;
}
int par[20], par_e[20];
bool vis[20];
vector<int> masks;
void dfs(int u, int p){
par[u] = p;
for(auto [v, w] : adj[u]) if(v != p){
par_e[v] = w;
dfs(v, u);
}
}
long long calc_up(int u){
long long s = 0;
while(!vis[u]){
vis[u] = true;
s += par_e[u];
u = par[u];
}
return s;
}
long long solve_root(int rt){
par_e[rt] = 0;
dfs(rt, -1);
long long ans = 0;
for(auto msk : masks){
fill(vis, vis + N, false);
vis[rt] = 0;
long long cur = 0;
for(int i = 0; i < N; ++i){
if(msk >> i & 1){
cur += calc_up(i);
}
}
ans = max(ans, cur);
}
return ans;
}
long long ans[20];
void solve(){
for(int mask = (1 << K) - 1; mask < (1 << N); ++mask){
if(__builtin_popcount(mask) == K){
masks.push_back(mask);
}
}
for(int r = 0; r < N; ++r){
ans[r] = solve_root(r);
}
for(int r = 0; r < N; ++r) cout << ans[r] << '\n';
}
}
//namespace subtask234{ //O(N^2 * log * 2) with low time limit :((
// bool check(){
// return N <= 2000;
// }
//
// struct fenwick_tree{
// vector<long long> bit;
// fenwick_tree(int n) : bit(n + 1, 0) {}
//
// void update(int i, long long v){
// for(; i < (int)bit.size(); i += i & (-i)) bit[i] += v;
// }
//
// void update(int l, int r, long long v){
// update(l, +v);
// update(r + 1, -v);
// }
//
// long long query(int i){
// long long s = 0;
// for(; i > 0; i -= i & (-i)) sum += bit[i];
// return s;
// }
//
// long long query(int l, int r){
// return query(r) - query(l - 1);
// }
// };
//
// void dfs(int u, int p){
// tin[u] = ++timer_dfs;
// for(auto [v, w] : adj[u]) if(v != p){
// par[v] = u;
// dfs(v, u);
// }
// tout[u] = timer_dfs;
// }
//
// void solve(){
// for(int r = 0; r < N; ++r){
// timer_dfs = 0;
// dfs(r, -1);
// }
// }
//}
namespace subtask5{
bool check(){
return K == 1;
}
long long dp_down[MAX], dp_up[MAX];
void dfs_down(int u, int p){
for(auto [v, w] : adj[u]) if(v != p){
dfs_down(v, u);
maximize(dp_down[u], dp_down[v] + w);
}
}
void dfs_up(int u, int p){
vector<long long> pref, suff;
for(auto [v, w] : adj[u]) if(v != p){
pref.push_back(dp_down[v] + w);
suff.push_back(dp_down[v] + w);
}
for(int i = 1; i < sz(pref); ++i) pref[i] = max(pref[i - 1], pref[i]);
for(int i = sz(suff) - 2; i >= 0; --i) suff[i] = max(suff[i + 1], suff[i]);
int i = 0;
for(auto [v, w] : adj[u]){
dp_up[v] = dp_up[u];
if(i > 0) maximize(dp_up[v], pref[i - 1] + w);
if(i + 1 < sz(suff)) maximize(dp_up[v], suff[i + 1] + w);
dfs_up(v, u);
}
}
void solve(){
dfs_down(0, -1);
dfs_up(0, -1);
for(int i = 0; i < N; ++i){
cout << max(dp_down[i], dp_up[i]) << '\n';
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
file("B");
cin >> N >> K;
for(int i = 1; i < N; ++i){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
if(subtask1::check()) return subtask1::solve(), 0;
// if(subtask234::check()) return subtask234::solve(), 0;
if(subtask5::check()) return subtask5::solve(), 0;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:10:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | #define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:199:5: note: in expansion of macro 'file'
199 | file("B");
| ^~~~
Main.cpp:10:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | #define file(name) if(fopen(name".inp", "r")){ freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:199:5: note: in expansion of macro 'file'
199 | file("B");
| ^~~~
# | 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... |