結果

問題 No.1833 Subway Planning
ユーザー 👑 emthrmemthrm
提出日時 2022-02-04 23:39:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,407 bytes
コンパイル時間 2,716 ms
コンパイル使用メモリ 216,368 KB
実行使用メモリ 31,860 KB
最終ジャッジ日時 2023-09-02 06:08:09
合計ジャッジ時間 11,646 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 RE -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 167 ms
31,736 KB
testcase_05 AC 175 ms
31,860 KB
testcase_06 AC 185 ms
29,784 KB
testcase_07 AC 233 ms
30,268 KB
testcase_08 AC 241 ms
30,876 KB
testcase_09 AC 224 ms
29,860 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 240 ms
29,556 KB
testcase_15 AC 293 ms
29,708 KB
testcase_16 AC 322 ms
31,196 KB
testcase_17 AC 593 ms
28,928 KB
testcase_18 AC 534 ms
27,532 KB
testcase_19 AC 545 ms
28,600 KB
testcase_20 AC 367 ms
22,904 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 1 ms
4,376 KB
testcase_24 RE -
testcase_25 RE -
権限があれば一括ダウンロードができます

ソースコード

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 {
      if (ver == edges[ord[i]].src || ver == edges[ord[i]].dst) return ver;
      int ans = -1;
      for (const Edge<int>& e : graph[ver]) {
        if (e.dst == par) 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