# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
900580 | mayankfrost | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> mll;
#define N 200'005
#define L 1'000'005
#define MOD 1000000007
#define FOR(i, n) for (i = 0; i < n; i++)
#define FORR(i, a, b) for (i = a; i <= b; i++)
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define FREOPEN \
freopen("yinyang.in", "r", stdin); \
freopen("yinyang.out", "w", stdout);
#define gohome \
cout << "NO\n"; \
return;
#define arprt(x) \
for (auto it : x) \
cout << it << " "; \
cout << "\n";
void solve();
vpll edges[N];
vll torem;
ll sz[N], maxd, l, ans, cnt[L];
bool rem[N], calc;
const int BUF = 100'001;
ll find_sz(ll v, ll p)
{
sz[v] = 1;
for (auto [it, x] : edges[v])
if (it != p && !rem[it])
sz[v] += find_sz(it, v);
return sz[v];
}
ll get_centroid(ll v, ll p, ll trsz)
{
for (auto [it, x] : edges[v])
if (it != p && !rem[it] && sz[it] * 2 > trsz)
return get_centroid(it, v, trsz);
return v;
}
void process(ll v, ll p, ll s, ll d)
{
if (s > l)
return;
torem.push_back(s);
if (calc)
{
if (cnt[l - s] < INT_MAX)
ans = min(ans, d + cnt[l - s]);
}
else
cnt[s] = min(cnt[s], d);
for (auto [u, c] : edges[v])
{
if (u == p || rem[u])
continue;
process(u, v, s + c, d + 1);
}
}
void centroid_decomp(ll v)
{
ll centroid = get_centroid(v, -1, find_sz(v, -1));
rem[centroid] = true;
for (auto [u, c] : edges[centroid])
{
if (rem[u])
continue;
calc = true;
process(u, -1, c, 1);
calc = false;
process(u, -1, c, 1);
}
for (auto it : torem)
cnt[it] = INT_MAX;
torem.clear();
for (auto [it, x] : edges[centroid])
if (!rem[it])
centroid_decomp(it);
}
int main()
{
FASTIO
// FREOPEN
int T;
T = 1;
// cin >> T;
while (T--)
{
solve();
}
return 0;
}
void solve()
{
ll i, j, n, u, v, c;
cin >> n >> l;
FOR(i, n - 1)
{
cin >> u >> v >> c;
u--, v--;
edges[u].push_back({v, c});
edges[v].push_back({u, c});
}
for (i = 1; i < L; i++)
cnt[i] = INT_MAX;
centroid_decomp(0);
if (ans == INT_MAX)
ans = -1;
cout << ans;
}