# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
805157 | 2023-08-03T13:31:23 Z | Halym2007 | 열대 식물원 (Tropical Garden) (IOI11_garden) | C++11 | 0 ms | 0 KB |
#include <bits/stdc++.h> #define pb push_back #include "race.h" #include <stdio.h> #include <stdlib.h> using namespace std; const int F = 1005; vector <int> v[F]; int k, jog, par[F], cost[F][F]; 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; } //int main () { // freopen ("kk.txt", "r", stdin); // int N, K; // cin >> N >> K; // int H[N][2], L[N]; // for (int i = 0; i < N; ++i) { // cin >> H[i][0] >> H[i][1] >> L[i]; // } //// best_path(N, K, H, L); // cout << best_path(N, K, H, L); //}