# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
674238 | mdub | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int to; int weight;
};
int n; int k;
vector<vector<Edge>> g;
vector<bool> seen;
map<int, stack<int>> mp;
vector<int> weights;
int ans;
void dfs(int iNode, int sum, int stage) {
seen[iNode] = true;
mp[sum].push(stage);
if (!mp[sum- k].empty())
ans = min(ans, stage - mp[sum- k].top());
for (auto adj: g[iNode]) {
if (!seen[adj.to]) {
dfs(adj.to, sum+adj.weight, stage + 1);
}
}
mp[sum].pop();
}
int best_path(int n2, int k2, vector<vector<int>> h, vector<int> l) {
n = n2; k = k2;
g.resize(n);
seen.assign(n, 0);
ans = 1e9;
for (int i = 0; i < n - 1; i++) {
int n1 = h[i][0], n2 = h[i][1];
g[n1].push_back({n2, l[i]});
g[n2].push_back({n1, l[i]});
}
dfs(0, 0, 0);
return ans;
}