제출 #483511

#제출 시각아이디문제언어결과실행 시간메모리
483511Misuki경주 (Race) (IOI11_race)C++17
31 / 100
619 ms26820 KiB
#include<bits/stdc++.h>

using namespace std;
using pii = pair<int, int>;

const int SIZE = 200000;
int n, k, ans = INT_MAX;
int sub[SIZE], table[1000001];
vector<pii> G[SIZE];
bitset<SIZE> vis, use;

int calc(int V, int P) {
  sub[V] = 1;
  for(pii X : G[V])
    if (!vis[X.first] and X.first != P)
      sub[V] += calc(X.first, V);
  return sub[V];
}

int centroid(int V, int P, int sz) {
  for(pii X : G[V])
    if (!vis[X.first] and X.first != P and sub[X.first] > sz / 2)
      return centroid(X.first, V, sz);
  return V;
}

void calc2(int V, int P, int len, int cost, vector<pii> &vec) {
  if (cost > k)
    return;
  vec.emplace_back(make_pair(cost, len));
  
  for(pii X : G[V]) 
    if (!vis[X.first] and X.first != P)
      calc2(X.first, V, len + 1, cost + X.second, vec);
}

void centroidDecomposite(int V) {
  calc(V, -1);
  int C = centroid(V, -1, sub[V]);
  
  vector<int> update(1, 0);
  use[0] = true, table[0] = 0;
  for(pii X : G[C]) {
    vector<pii> tmp;
    calc2(X.first, C, 1, X.second, tmp);
    for(pii Y : tmp) {
      int cost = Y.first, len = Y.second;
      if (!use[k - cost]) continue;
      ans = min(ans, len + table[k - cost]);
    }
    for(pii Y : tmp) {
      if (!use[Y.first]) {
        table[Y.first] = Y.second, use[Y.first] = true;
        update.emplace_back(Y.first);
      } else {
        table[Y.first] = min(table[Y.first], Y.second);
      }
    }
  }

  for(int X : update)
    use[X] = false;

  vis[C] = true;
  for(pii X : G[C])
    if (!vis[X.first])
      centroidDecomposite(X.first);
}

int best_path(int N, int K, int H[][2], int L[]) {
  n = N, k = K;
  for(int i = 0; i < N - 1; i++) {
    G[H[i][0]].emplace_back(make_pair(H[i][1], L[i]));
    G[H[i][1]].emplace_back(make_pair(H[i][0], L[i]));
  }

  centroidDecomposite(0);

  return ans == INT_MAX ? -1 : ans;
}
/*
int A[SIZE - 1][2];
int B[SIZE - 1];

int main() {
  int n, k;
  cin >> n >> k;
  for(int i = 0; i < n - 1; i++)
    cin >> A[i][0] >> A[i][1];
  for(int i = 0; i < n - 1; i++)
    cin >> B[i];
  cout << best_path(n, k, A, B) << '\n';

  return 0;
}
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...