Submission #2067081


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

struct edge
{
  int to, cost;
  bool type;
};

int N, D;
vector< edge > g[100000];
int max_dist[100000];
int change[100000];

void dfs(int idx, int par)
{
  for(auto &e : g[idx]) {
    if(e.to == par) continue;
    dfs(e.to, idx);
    max_dist[idx] = max(max_dist[idx], max_dist[e.to] + e.cost);
    change[idx] += change[e.to];
    change[idx] += e.type;
  }
}

int ans = -1;

void dfs2(int idx, int par, int dist, int come)
{
  vector< pair< int, int > > curr_dist{{dist, -1}};
  int need = come;
  for(auto &e : g[idx]) {
    if(e.to == par) continue;
    curr_dist.emplace_back(max_dist[e.to] + e.cost, e.to);
    need += change[e.to];
    need += e.type;
  }
  sort(curr_dist.rbegin(), curr_dist.rend());

  if(curr_dist[0].first <= D) {
    if(~ans) ans = min(ans, need);
    else ans = need;
  }
  for(auto &e : g[idx]) {
    if(e.to == par) continue;
    dfs2(e.to, idx, curr_dist[curr_dist[0].second == e.to].first + e.cost, need - change[e.to] + (e.type ? -1 : 1));
  }
}


int main()
{
  cin >> N >> D;
  for(int i = 0; i < N - 1; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    --x, --y;
    g[x].emplace_back((edge) {y, z, true});
    g[y].emplace_back((edge) {x, z, false});
  }

  int root = -1;
  for(int i = 0; i < N; i++) {
    if(g[i].size() >= 2) root = i;
  }

  if(root == -1) {
    if(g[0][0].cost > D) cout << -1 << endl;
    else cout << 0 << endl;
    return (0);
  }

  dfs(0, -1);
  dfs2(0, -1, 0, 0);
  cout << ans << endl;
}

Submission Info

Submission Time
Task E - 限界集落
User ei13333
Language C++14 (GCC 5.4.1)
Score 700
Code Size 1582 Byte
Status AC
Exec Time 118 ms
Memory 22528 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 32 ms 4096 KB
01_random_01 AC 51 ms 4992 KB
01_random_02 AC 103 ms 7296 KB
01_random_03 AC 48 ms 4864 KB
01_random_04 AC 66 ms 5632 KB
01_random_05 AC 61 ms 5376 KB
01_random_06 AC 6 ms 2816 KB
01_random_07 AC 83 ms 6400 KB
01_random_08 AC 54 ms 5120 KB
01_random_09 AC 90 ms 6656 KB
02_path_00 AC 111 ms 17920 KB
02_path_01 AC 112 ms 18688 KB
02_path_02 AC 111 ms 16256 KB
02_path_03 AC 111 ms 16896 KB
02_path_04 AC 115 ms 20864 KB
02_path_05 AC 114 ms 22272 KB
02_path_06 AC 114 ms 15232 KB
02_path_07 AC 111 ms 18688 KB
02_path_08 AC 113 ms 21504 KB
02_path_09 AC 111 ms 18688 KB
02_path_10 AC 117 ms 22528 KB
02_path_11 AC 118 ms 18816 KB
02_path_12 AC 113 ms 21376 KB
02_path_13 AC 116 ms 18944 KB
02_path_14 AC 108 ms 16640 KB
03_uni_00 AC 100 ms 8756 KB
03_uni_01 AC 101 ms 8756 KB
03_uni_02 AC 101 ms 8756 KB
03_uni_03 AC 100 ms 8756 KB
03_uni_04 AC 101 ms 8756 KB
03_uni_05 AC 101 ms 8756 KB
03_uni_06 AC 97 ms 8756 KB
03_uni_07 AC 101 ms 8756 KB
03_uni_08 AC 101 ms 8756 KB
03_uni_09 AC 95 ms 8756 KB
03_uni_10 AC 101 ms 8756 KB
03_uni_11 AC 101 ms 8756 KB
03_uni_12 AC 101 ms 8756 KB
03_uni_13 AC 101 ms 8756 KB
03_uni_14 AC 101 ms 8756 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