이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
const int INF = 1e9 + 5;
int ans = INF;
vector<vector<pair<int, int>>> adj;
void dfs(int node, int p, map<int, int>& mp, int& k) {
mp[0] = 0;
for(auto[x, w] : adj[node]) {
if(p == x) continue;
map<int, int> nxt;
dfs(x, node, nxt, k);
if(mp.size() < nxt.size()) swap(mp, nxt);
map<int, int> tmp;
for(auto&[x, y] : nxt) {
tmp[x + w] = nxt[x] + 1;
}
swap(nxt, tmp);
for(auto&[x, y] : nxt) {
if(mp.count(k - x)) {
ans = min(ans, y + mp[k - x]);
}
}
for(auto&[x, y] : nxt) {
if(mp.count(x)) {
mp[x] = min(mp[x], y);
} else {
mp[x] = y;
}
}
}
}
int best_path(int n, int k, int H[][2], int L[])
{
adj.resize(n);
for(int i = 0; i < n - 1; ++i) {
adj[H[i][0]].emplace_back(H[i][1], L[i]);
adj[H[i][1]].emplace_back(H[i][0], L[i]);
}
map<int, int> mp;
dfs(0, -1, mp, k);
if(ans == INF) {
return -1;
}
return ans;
}
# | 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... |