# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1080897 | duduFreire | 경주 (Race) (IOI11_race) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(int i=(int)(a);i < (int)(b);i++)
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define sz(x) ((int)(x).size())
#ifdef LOCAL
#define debug(var) cerr << #var << ": " << var << endl
#else
#define debug(var)
#endif
using namespace std;
template<class T, class U> auto &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; }
template<class T, class U> auto &operator<<(ostream &os, pair<T, U> const& p) { return os << '{' << p.first << ' ' << p.second << '}'; }
const auto ES = "", SEP = " ";
template<class T> auto &operator>>(istream& is, vector<T> &c) { for (auto &x : c) is >> x; return is; }
template<class T> auto &operator<<(ostream& os, vector<T> const &c) { auto sep = ES; for (auto x : c) os << sep << x, sep = SEP; return os; }
template<class T> auto &operator<<(ostream& os, set<T> const &c) { auto sep = ES; for (auto x : c) os << sep << x, sep = SEP; return os; }
template<class T> auto &operator<<(ostream& os, multiset<T> const &c) { auto sep = ES; for (auto x : c) os << sep << x, sep = SEP; return os; }
template<class T> auto &operator<<(ostream& os, unordered_set<T> const &c) { auto sep = ES; for (auto x : c) os << sep << x, sep = SEP; return os; }
template<class T> auto &operator<<(ostream& os, deque<T> const &c) { auto sep = ES; for (auto x : c) os << sep << x, sep = SEP; return os; }
template<class K, class V> auto &operator<<(ostream& os, map<K,V> const &c) { auto sep = ES; for (auto x : c) os << sep << x, sep = SEP; return os; }
template<class K, class V> auto &operator<<(ostream& os, unordered_map<K,V> const &c) { auto sep = ES; for (auto x : c) os << sep << x, sep = SEP; return os; }
int ceil(int a, int b) {
return (a+b-1)/b;
}
constexpr int MAX=2e5;
constexpr int MOD=1e9+7;
constexpr int oo=0x3f3f3f3f3f3f3f3f;
/*
*/
int k;
struct Centroid {
vector<vector<pii>> g;
int rt, n;
vi rmv, ssz, par;
int ans=oo;
Centroid(vector<vector<pii>> ag, int art) : g(ag), rt(art), n(g.size()), rmv(n,0), ssz(n,0), par(n,0) {
centroid_tree(art);
}
int sizes(int a, int p=-1) {
int ans=1;
for(auto&[b,w]:g[a]) {
if (b == p or rmv[b]) continue;
ans+=sizes(b,a);
}
return ssz[a]=ans;
}
void calcdists(int a, int d, int p, vi& dists) {
dists[d]++;
for(auto&[b,w] : g[a]) {
if (rmv[b] or b==p) continue;
calcdists(b, d+1, a,dists);
}
}
int centroid(int a, int tsize, int p=-1) {
for(auto&[b,w]:g[a]) {
if (rmv[b] or b==p) continue;
if (ssz[b] * 2 > tsize) return centroid(b,tsize, a);
}
return a;
}
void centroid_tree(int a, int p=-1) {
int c=centroid(a, sizes(a));
rmv[c]=true;
par[c]=p;
solvesub(c);
for(auto&[b,w] : g[c]) {
if (rmv[b]) continue;
centroid_tree(b, c);
}
}
// do not visit removed guys (if rmv[b] continue)
void solvesub(int a) {
map<int,int> best, rbest;
auto dfs=[&](auto self, int a, int sum, int h, int p) -> void {
if (sum <= k) {
if (!rbest.count(sum) or rbest[sum] > h) rbest[sum]=h;
}
for (auto [b,w] : g[a]) if (!rmv[b] and b!=p) {
self(self,b,sum+w,h+1,a);
}
};
for (auto& [b,w] : g[a]) if (!rmv[b]) {
dfs(dfs, b, w, 1, a);
map<int,int> nbest;
for (auto [val, h] : rbest) {
if (best.count(k-val)) {
ans=min(ans, best[k-val]+h);
}
if (!best.count(val) or best[val] > h) nbest[val]=h;
}
for (auto [val, h] : nbest) best[val]=h;
map<int,int> gbg; swap(rbest, gbg);
}
}
};
void solve()
{
int n;cin>>n>>k;
vector<vector<pii>> g(n);
rep(i,1,n) {
int a,b,w;cin>>a>>b>>w;
g[a].eb(b,w);
g[b].eb(a,w);
}
Centroid centroid(g, 0);
cout << (centroid.ans == oo ? -1 : centroid.ans) << endl;
}
signed main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);cin.tie(0);
#endif
int t=1;
//cin>>t;
while(t--) solve();
}