# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
713887 | kaxzert | 경주 (Race) (IOI11_race) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define fto(i, a, b) for(int i = a; i <= b; ++i)
#define fdto(i, a, b) for(int i = a; i >= b; --i)
#define bugarr(a, i, j) cout << #a << "{" << i << "..." << j << "}:"; fto(k, i, j-1) cout << a[k] << ", "; cout << a[j] << endl;
#define ll long long
#define db double
#define ldb long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vt vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(i, a) for(auto &i : a)
#define sz(a) (int)a.size()
#define pi(a, b) pair<a, b>
#define fast ios::sync_with_stdio(false); cin.tie(0)
void setIO(string s) {
if (sz(s) != 0) {
freopen((s+".inp").c_str(),"r",stdin);
// freopen((s+".out").c_str(),"w",stdout);
}
}
void setIOusaco(string s) {
if (sz(s) != 0) {
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
template<typename T, typename V>
bool ckmin(T &a, V b) {return (b < a)? a = b, true : false;}
template<typename T, typename V>
bool ckmax(T &a, V b) {return (b > a)? a = b, true : false;}
void print(int x) {cout << x;}
void print(long long x) {cout << x;}
void print(unsigned x) {cout << x;}
void print(unsigned long long x) {cout << x;}
void print(double x) {cout << fixed << x;}
void print(long double x) {cout << fixed << x;}
void print(char x) {cout << "'" << x << "'";}
void print(string x) {cout << '"' << x << '"';}
void print(bool x) {cout << (x ? "true" : "false");}
template<typename T, typename V>
void print(const pair<T, V> &x) {cout << '{'; print(x.ff); cout << ", "; print(x.ss); cout << '}';}
template<typename T>
void print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? ", " : ""), print(i); cout << "}";}
void _print() {cout << "]" << endl;}
template <typename T, typename... V>
void _print(T t, V... v) {print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define bug(x...) cout << "[" << #x << "] = ["; _print(x)
const int maxN = 200007;
set<pair<int, int> > gr[maxN];
int len[maxN], depth[maxN], n, k;
vt<pair<int, int> > ke[maxN];
int ans = (int)1e9;
void dfs(int u, int val, int p = -1) {
len[u] = val;
trav(c, ke[u]) {
int v = c.ff;
int l = c.ss;
if (v == p) continue;
depth[v] = depth[u] + 1;
dfs(v, val+l, u);
}
}
void dfs2(int u, int p = -1) {
trav(c, ke[u]) {
int v = c.ff;
int l = c.ss;
if (v == p) continue;
dfs2(v, u);
gr[v].insert(mp(len[v], depth[v]));
auto it = gr[v].lower_bound(mp(k + len[u], -1));
if (it != gr[v].end() && (it->ff) == k + len[u]) {
if (u == 6) {bug(gr[v]);}
ans = min(ans, it->ss - depth[u]);
}
if (sz(gr[u]) < sz(gr[v])) {
swap(gr[u], gr[v]);
}
trav(o, gr[v]) {
int need = k - o.ff + len[u]*2;
auto it = gr[u].lower_bound(mp(need, -1));
if (it != gr[u].end() && (it->ff) == need) {
ans = min(ans, it->ss + o.ss - 2*depth[u]);
}
}
trav(o, gr[v]) gr[u].insert(o);
}
}
int best_path(int n_, int k_, int edges[][2], int weights[])
//setIO("race_ioi2011");
//ios::sync_with_stdio(false); cin.tie(0);
n = n_;
k = k_;
fto(i, 0, n-2) {
int u = edges[i][0];
int v = edges[i][1];
int l = weights[i];
ke[u].pb(mp(v, l));
ke[v].pb(mp(u, l));
}
dfs(0, 0);
dfs2(0);
return (ans == (int)1e9 ? -1 : ans);
}