제출 #229404

#제출 시각아이디문제언어결과실행 시간메모리
229404quocnguyen1012경주 (Race) (IOI11_race)C++14
0 / 100
13 ms14464 KiB
#ifndef LOCAL
#include "race.h"
#endif // LOCAL
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 2e5 + 5;

map<ll, int> sub[maxn];
vector<ii> adj[maxn];
int N, K;
int res = 1e9;

void dfs(int u, int p = -1, int h = 0, ll w = 0)
{
  sub[u][w] = h;
  for(auto it : adj[u]) if(it.fi != p){
    dfs(it.fi, u, h + 1, w + it.se);
    int v = it.fi;
    if(sub[u].size() < sub[v].size())
      swap(sub[u], sub[v]);
    for(auto & all : sub[v]){
      if(sub[u].count(K + 2 * w - all.fi)){
        res = min(res, sub[u][K + 2 * w - all.fi] + all.se);
      }
    }
    for(auto & all : sub[v]){
      if(sub[u].count(all.fi)){
        sub[u][all.fi] = min(sub[u][all.fi], all.se);
      }
      else{
        sub[u][all.fi] = all.se;
      }
    }
    sub[v].clear();
  }
}

int best_path(int _N, int _K, int H[][2], int L[])
{
  N = _N;
  K = _K;
  for(int i = 0; i < N; ++i){
    adj[H[i][0]].eb(H[i][1], L[i]);
    adj[H[i][1]].eb(H[i][0], L[i]);
  }
  dfs(0);
  if(res == 1e9) res = -1;
  return res;
}

#ifdef LOCAL
signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> N >> K;
  for(int i = 1; i < N; ++i){
    int u, v, w; cin >> u >> v >> w;
    adj[u].eb(v, w); adj[v].eb(u, w);
  }
  dfs(0);
  cout << res;
}
#endif //LOCAL
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...