結果

問題 No.1833 Subway Planning
ユーザー 👑 emthrmemthrm
提出日時 2022-02-04 23:57:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 654 ms / 4,000 ms
コード長 3,581 bytes
コンパイル時間 2,580 ms
コンパイル使用メモリ 218,456 KB
実行使用メモリ 32,404 KB
最終ジャッジ日時 2023-09-02 06:11:44
合計ジャッジ時間 10,241 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 171 ms
31,896 KB
testcase_05 AC 176 ms
32,404 KB
testcase_06 AC 183 ms
29,680 KB
testcase_07 AC 241 ms
30,092 KB
testcase_08 AC 241 ms
30,848 KB
testcase_09 AC 225 ms
29,812 KB
testcase_10 AC 271 ms
28,892 KB
testcase_11 AC 264 ms
28,864 KB
testcase_12 AC 263 ms
29,032 KB
testcase_13 AC 278 ms
28,988 KB
testcase_14 AC 241 ms
29,684 KB
testcase_15 AC 295 ms
29,588 KB
testcase_16 AC 331 ms
31,256 KB
testcase_17 AC 654 ms
29,048 KB
testcase_18 AC 601 ms
28,008 KB
testcase_19 AC 567 ms
28,696 KB
testcase_20 AC 394 ms
22,860 KB
testcase_21 AC 257 ms
30,376 KB
testcase_22 AC 264 ms
28,728 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename CostType>
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

int main() {
  int n; cin >> n;
  vector<Edge<int>> edges;
  vector<vector<Edge<int>>> graph(n);
  REP(i, n - 1) {
    int a, b, c; cin >> a >> b >> c; --a; --b;
    edges.emplace_back(a, b, c);
    graph[a].emplace_back(a, b, i);
    graph[b].emplace_back(b, a, i);
  }
  vector<int> ord(n - 1);
  iota(ALL(ord), 0);
  sort(ALL(ord), [&](int a, int b) -> bool { return edges[a].cost > edges[b].cost; });
  int s = edges[ord.front()].src, t = edges[ord.front()].dst, s_par = t, t_par = s;
  set<int> s_side, t_side, ng;
  const auto f = [&](auto&& f, int par, int ver, set<int>& side) -> void {
    for (const Edge<int>& e : graph[ver]) {
      if (e.dst == par) continue;
      side.emplace(e.cost);
      f(f, ver, e.dst, side);
    }
  };
  f(f, s_par, s, s_side);
  f(f, t_par, t, t_side);
  int ans = (n - 1 == 1 ? 0 : edges[ord[1]].cost), m = edges[ord.front()].cost;
  FOR(i, 1, n - 1) {
    if (ng.count(ord[i])) break;
    const auto g = [&](auto&& g, int par, int ver, set<int>& side) -> int {
      int bad = -1, ans = -1;
      if (ver == edges[ord[i]].src) {
        ans = edges[ord[i]].src;
        bad = edges[ord[i]].dst;
      }
      if (ver == edges[ord[i]].dst) {
        ans = edges[ord[i]].dst;
        bad = edges[ord[i]].src;
      }
      for (const Edge<int>& e : graph[ver]) {
        if (e.dst == par || e.dst == bad) continue;
        side.erase(e.cost);
        if (const int tmp = g(g, ver, e.dst, side); tmp != -1) {
          chmin(m, edges[e.cost].cost);
          ans = tmp;
        } else {
          ng.emplace(e.cost);
        }
      }
      return ans;
    };
    if (s_side.count(ord[i])) {
      s_side.erase(ord[i]);
      s_par = g(g, s_par, s, s_side);
      assert(s_par != -1);
      s = (s_par == edges[ord[i]].src ? edges[ord[i]].dst : edges[ord[i]].src);
    } else if (t_side.count(ord[i])) {
      t_side.erase(ord[i]);
      t_par = g(g, t_par, t, t_side);
      assert(t_par != -1);
      t = (t_par == edges[ord[i]].src ? edges[ord[i]].dst : edges[ord[i]].src);
    }
    chmin(m, edges[ord[i]].cost);
    chmin(ans, max(edges[ord.front()].cost - m, i + 1 == n - 1 ? 0 : edges[ord[i + 1]].cost));
  }
  cout << ans << '\n';
  return 0;
}
0