Submission #2095687


Source Code Expand

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

vector<PII> g[100010];
map<PII, int> mp;
signed main(void)
{
  int n, d;
  cin >> n >> d;
  REP(i, n-1) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    g[a].PB({b, c});
    g[b].PB({a, c});
    mp[{a, b}] = 1;
  }

  VI dist(n), edge(n);
  function<int(int,int)> dfs1 = [&](int v, int p) -> int {
    if(p != -1 && g[v].size() == 1) return 0;
    for(PII &i: g[v]) if(i.first != p) {
      edge[v] += (mp.find(make_pair(v, i.first)) != mp.end());
      edge[v] += dfs1(i.first, v);
      chmax(dist[v], dist[i.first] + i.second);
    }
    return edge[v];
  };

  dfs1(0, -1);
  // cout << dist << endl << edge << endl;

  // 頂点iから最も遠い頂点までの距離
  VI len(n);
  function<void(int,int,int)> dfs2 = [&](int v, int d_par, int p) {
    vector<PII> d_child;
    d_child.emplace_back(0, -1);
    for(PII &e : g[v]) {
      if(e.first == p) d_child.emplace_back(d_par + e.second, e.first);
      else d_child.emplace_back(dist[e.first] + e.second, e.first);
    }
    sort(d_child.rbegin(), d_child.rend());
    len[v] = d_child[0].first;
    for(PII &e : g[v]) {
      if(e.first == p) continue;
      dfs2(e.first, d_child[d_child[0].second == e.first].first, v);
    }
  };

  dfs2(0, -1, -1);
  // cout << len << endl;

  bool flag = false;
  REP(i, n) if(len[i] <= d) flag = true;
  if(!flag) {
    cout << -1 << endl;
    return 0;
  }

  // 頂点iに向かうのに辺を反転しないといけない本数
  VI ans(n), d_child(n);
  function<void(int,int,int)> dfs3 = [&](int v, int d_par, int p) {
    // cout << v << " " << d_par << " " << p << endl;
    for(PII &e : g[v]) {
      int tmp = (mp.find(make_pair(v, e.first)) != mp.end());
      if(e.first == p) {
        ans[v] += d_par + tmp;
        d_child[e.first] = d_par + tmp;
      } else {
        ans[v] += edge[e.first] + tmp;
        d_child[e.first] = edge[e.first] + tmp;
      }
      // cout << e << " " << ans[v] << endl;
    }
    for(PII &e : g[v]) {
      if(e.first == p) continue;
      // ans[v] から e.firstの部分木方向のを引いたやつ
      dfs3(e.first, ans[v] - d_child[e.first], v);
    }
  };

  dfs3(0, 0, -1);
  // cout << ans << endl;

  int ret = INF;
  REP(i, n) {
    if(len[i] <= d) chmin(ret, ans[i]);
  }
  cout << ret << endl;

  return 0;
}

Submission Info

Submission Time
Task E - 限界集落
User ferin_tech
Language C++14 (GCC 5.4.1)
Score 700
Code Size 3476 Byte
Status AC
Exec Time 243 ms
Memory 39168 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 1
AC × 45
Set Name Test Cases
Sample 00_sample_00
All 00_sample_00, 01_random_00, 01_random_01, 01_random_02, 01_random_03, 01_random_04, 01_random_05, 01_random_06, 01_random_07, 01_random_08, 01_random_09, 02_path_00, 02_path_01, 02_path_02, 02_path_03, 02_path_04, 02_path_05, 02_path_06, 02_path_07, 02_path_08, 02_path_09, 02_path_10, 02_path_11, 02_path_12, 02_path_13, 02_path_14, 03_uni_00, 03_uni_01, 03_uni_02, 03_uni_03, 03_uni_04, 03_uni_05, 03_uni_06, 03_uni_07, 03_uni_08, 03_uni_09, 03_uni_10, 03_uni_11, 03_uni_12, 03_uni_13, 03_uni_14, hand_00, hand_01, hand_02, hand_03
Case Name Status Exec Time Memory
00_sample_00 AC 2 ms 2560 KB
01_random_00 AC 54 ms 7040 KB
01_random_01 AC 91 ms 9856 KB
01_random_02 AC 198 ms 17024 KB
01_random_03 AC 84 ms 10752 KB
01_random_04 AC 118 ms 11904 KB
01_random_05 AC 109 ms 11136 KB
01_random_06 AC 9 ms 3200 KB
01_random_07 AC 150 ms 14080 KB
01_random_08 AC 96 ms 10240 KB
01_random_09 AC 172 ms 15104 KB
02_path_00 AC 239 ms 32512 KB
02_path_01 AC 231 ms 33664 KB
02_path_02 AC 226 ms 30080 KB
02_path_03 AC 227 ms 30976 KB
02_path_04 AC 236 ms 36864 KB
02_path_05 AC 237 ms 38784 KB
02_path_06 AC 184 ms 28672 KB
02_path_07 AC 240 ms 33536 KB
02_path_08 AC 238 ms 37504 KB
02_path_09 AC 230 ms 33536 KB
02_path_10 AC 193 ms 39168 KB
02_path_11 AC 232 ms 33792 KB
02_path_12 AC 238 ms 37504 KB
02_path_13 AC 243 ms 34048 KB
02_path_14 AC 233 ms 30592 KB
03_uni_00 AC 145 ms 18928 KB
03_uni_01 AC 144 ms 18928 KB
03_uni_02 AC 146 ms 18928 KB
03_uni_03 AC 145 ms 18928 KB
03_uni_04 AC 146 ms 18928 KB
03_uni_05 AC 145 ms 18928 KB
03_uni_06 AC 147 ms 18928 KB
03_uni_07 AC 147 ms 18928 KB
03_uni_08 AC 147 ms 18928 KB
03_uni_09 AC 138 ms 18928 KB
03_uni_10 AC 146 ms 18928 KB
03_uni_11 AC 145 ms 18928 KB
03_uni_12 AC 145 ms 18928 KB
03_uni_13 AC 147 ms 18928 KB
03_uni_14 AC 145 ms 18928 KB
hand_00 AC 2 ms 2560 KB
hand_01 AC 2 ms 2560 KB
hand_02 AC 2 ms 2560 KB
hand_03 AC 2 ms 2560 KB