# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
805164 | Halym2007 | Tropical Garden (IOI11_garden) | 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;
#define pb push_back
const int MAX_N = 1005;
vector <int> v[MAX_N];
int k, jog, par[MAX_N], cost[MAX_N][MAX_N];
map <int, int> m;
void dfs1 (int x, int pr) {
par[x] = pr;
for (int i : v[x]) {
if (i != pr) {
dfs1 (i, x);
}
}
}
void dfs2 (int x, int pr, int edges, int baha) {
if (baha > k) return;
m[baha] = edges;
for (int i : v[x]) {
if (i != pr and i != par[x]) {
dfs2 (i, pr, edges + 1, baha + cost[x][i]);
}
}
}
void dfs (int x, int pr, int baha, int edges) {
if (baha > k or pr == -1) {
return;
}
if ((baha + cost[x][par[x]]) == k) {
if (jog == -1) jog = edges + 1;
else jog = min (jog, edges + 1);
return;
}
m.clear();
dfs2 (pr, x, 0, 0);
int jp = baha + cost[x][par[x]];
if (m.find (k - jp) != m.end()) {
if (jog == -1) jog = edges + m[k - jp] + 1;
else jog = min (jog, edges + m[k - jp] + 1);
}
dfs (pr, par[pr], baha + cost[x][par[x]], edges + 1);
}
int best_path(int N, int K, int H[][2], int L[]) {
jog = -1;
k = K;
for (int i = 0; i < N - 1 ; ++i) {
v[H[i][0]].pb (H[i][1]);
v[H[i][1]].pb (H[i][0]);
cost[H[i][0]][H[i][1]] = L[i];
cost[H[i][1]][H[i][0]] = L[i];
}
dfs1 (1, -1);
for (int i = 0; i < N; ++i) {
dfs (i, par[i], 0, 0);
}
return jog;
}