結果

問題 No.1833 Subway Planning
ユーザー SSRSSSRS
提出日時 2022-02-04 21:48:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,056 ms / 4,000 ms
コード長 2,650 bytes
コンパイル時間 2,830 ms
コンパイル使用メモリ 215,300 KB
実行使用メモリ 30,480 KB
最終ジャッジ日時 2023-09-02 04:42:36
合計ジャッジ時間 23,614 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 350 ms
30,396 KB
testcase_05 AC 487 ms
30,356 KB
testcase_06 AC 460 ms
30,476 KB
testcase_07 AC 1,669 ms
30,480 KB
testcase_08 AC 1,705 ms
30,356 KB
testcase_09 AC 1,943 ms
30,476 KB
testcase_10 AC 617 ms
26,920 KB
testcase_11 AC 550 ms
27,052 KB
testcase_12 AC 456 ms
26,752 KB
testcase_13 AC 545 ms
26,820 KB
testcase_14 AC 453 ms
27,672 KB
testcase_15 AC 698 ms
27,748 KB
testcase_16 AC 716 ms
27,672 KB
testcase_17 AC 2,056 ms
28,456 KB
testcase_18 AC 1,860 ms
26,376 KB
testcase_19 AC 1,079 ms
27,768 KB
testcase_20 AC 769 ms
22,936 KB
testcase_21 AC 1,262 ms
28,908 KB
testcase_22 AC 992 ms
28,572 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int LOG = 18;
const int INF = 1000000000;
int main(){
  int N;
  cin >> N;
  vector<tuple<int, int, int>> E(N - 1);
  vector<vector<int>> E2(N);
  for (int i = 0; i < N - 1; i++){
    int A, B, C;
    cin >> A >> B >> C;
    A--;
    B--;
    E[i] = make_tuple(C, A, B);
    E2[A].push_back(B);
    E2[B].push_back(A);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    for (int w : E2[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  int tv = INF, fv = -1;
  while (tv - fv > 1){
    int mid = (tv + fv) / 2;
    vector<bool> T(N, false);
    int a = -1;
    for (int i = 0; i < N - 1; i++){
      if (get<0>(E[i]) > mid){
        T[get<1>(E[i])] = true;
        T[get<2>(E[i])] = true;
        a = get<1>(E[i]);
      }
    }
    if (a == -1){
      tv = mid;
    } else {
      vector<int> d1(N, -1);
      d1[a] = 0;
      queue<int> Q1;
      Q1.push(a);
      int s;
      while (!Q1.empty()){
        int v = Q1.front();
        Q1.pop();
        if (T[v]){
          s = v;
        }
        for (int w : E2[v]){
          if (d1[w] == -1){
            d1[w] = d1[v] + 1;
            Q1.push(w);
          }
        }
      }
      vector<int> d2(N, -1);
      d2[s] = 0;
      queue<int> Q2;
      Q2.push(s);
      int t;
      while (!Q2.empty()){
        int v = Q2.front();
        Q2.pop();
        if (T[v]){
          t = v;
        }
        for (int w : E2[v]){
          if (d2[w] == -1){
            d2[w] = d2[v] + 1;
            Q2.push(w);
          }
        }
      }
      vector<int> d3(N, -1);
      d3[t] = 0;
      queue<int> Q3;
      Q3.push(t);
      while (!Q3.empty()){
        int v = Q3.front();
        Q3.pop();
        for (int w : E2[v]){
          if (d3[w] == -1){
            d3[w] = d3[v] + 1;
            Q3.push(w);
          }
        }
      }
      int D = d2[t];
      bool ok = true;
      for (int i = 0; i < N; i++){
        if (T[i]){
          if (d2[i] + d3[i] > D){
            ok = false;
          }
        }
      }
      int mn = INF;
      for (int i = 0; i < N - 1; i++){
        int A = get<1>(E[i]);
        int B = get<2>(E[i]);
        if (d2[A] + d3[B] == D - 1 || d2[B] + d3[A] == D - 1){
          mn = min(mn, get<0>(E[i]));
        }
      }
      for (int i = 0; i < N - 1; i++){
        if (get<0>(E[i]) - mn > mid){
          ok = false;
        }
      }
      if (ok){
        tv = mid;
      } else {
        fv = mid;
      }
    }
  }
  cout << tv << endl;
}
0