結果

問題 No.1833 Subway Planning
ユーザー KudeKude
提出日時 2022-02-04 23:53:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 4,000 ms
コード長 6,932 bytes
コンパイル時間 3,429 ms
コンパイル使用メモリ 252,640 KB
実行使用メモリ 78,020 KB
最終ジャッジ日時 2024-06-11 13:06:52
合計ジャッジ時間 8,097 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 147 ms
78,020 KB
testcase_05 AC 153 ms
78,020 KB
testcase_06 AC 180 ms
77,896 KB
testcase_07 AC 226 ms
77,484 KB
testcase_08 AC 232 ms
71,172 KB
testcase_09 AC 204 ms
77,480 KB
testcase_10 AC 146 ms
55,296 KB
testcase_11 AC 138 ms
55,496 KB
testcase_12 AC 138 ms
55,240 KB
testcase_13 AC 148 ms
55,368 KB
testcase_14 AC 135 ms
54,456 KB
testcase_15 AC 145 ms
54,516 KB
testcase_16 AC 155 ms
54,460 KB
testcase_17 AC 193 ms
54,816 KB
testcase_18 AC 179 ms
51,288 KB
testcase_19 AC 167 ms
54,684 KB
testcase_20 AC 120 ms
42,868 KB
testcase_21 AC 187 ms
68,624 KB
testcase_22 AC 171 ms
62,388 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'void {anonymous}::HLD::assign_idx(int, int, int&, int)',
    inlined from 'void {anonymous}::HLD::assign_idx(int, int, int&, int)' at main.cpp:65:23,
    inlined from 'void {anonymous}::HLD::assign_idx(int, int, int&, int)' at main.cpp:65:23,
    inlined from 'void {anonymous}::HLD::assign_idx(int, int, int&, int)' at main.cpp:65:23:
main.cpp:62:19: warning: 'mi' may be used uninitialized [-Wmaybe-uninitialized]
   62 |         assign_idx(mi, h, nxt, u);
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~
main.cpp: In member function 'void {anonymous}::HLD::assign_idx(int, int, int&, int)':
main.cpp:54:13: note: 'mi' was declared here
   54 |         int mi;
      |             ^~
In member function 'void {anonymous}::HLD::assign_idx(int, int, int&, int)',
    inlined from 'void {anonymous}::HLD::assign_idx(int, int, int&, int)' at main.cpp:65:23,
    inlined from 'void {anonymous}::HLD::assign_idx(int, int, int&, int)' at main.cpp:65:23:
main.cpp:62:19: warning: 'mi' may be used uninitialized [-Wmaybe-uninitialized]
   62 |         assign_idx(mi, h, nxt, u);
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~
main.cpp: In member function 'void {anonymous}::HLD::assign_idx(int, int, int&, int)':
main.cpp:54:13: note: 'mi' was declared here
   54 |         int mi;
      |             ^~
In member function 'void {anonymous}::HLD::assign_idx(int, int, int&, int)',
    inlined from 'void {anonymous}::HLD::assign_idx(int, int, int&, int)' at main.cpp:62:19,
    inlined from 'void {anonymous}::HLD::assign_idx(int, int, int&, int)' at main.cpp:65:23:
main.cpp:62:19: warning: 'mi' may be used uninitialized [-Wmaybe-uninitialized]
   62 |         assign_idx(mi, h, nxt, u);
      |         ~~~~~~~~~~^~~~~~~~~~~~~~~
main.cpp: In member function 'void {anonymous}::HLD::assign_idx(int, int, int&, int)':
main.cpp:54:13: note: 'mi' was declared here
   54 |         int mi;
      |             ^~
In member function 'void {anonymous}::HLD::assign_idx(int, int, int&, int)',
    inlin

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) { a = max(a, b); }
template<class T> void chmin(T& a, const T& b) { a = min(a, b); }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

struct HLD {
    std::vector<std::vector<int>> &to;
    int root, n;
    std::vector<int> sz, parent, depth, idx, ridx, head, inv;
    HLD(std::vector<std::vector<int>>& to, int root=0):
        to(to), root(root), n(to.size()),
        sz(n), parent(n), depth(n), idx(n), ridx(n), head(n), inv(n)
    {
        init_tree_data(root);
        int x = 0;
        assign_idx(root, root, x);
    }
    void init_tree_data(int u, int p=-1, int d=0) {
        parent[u] = p;
        depth[u] = d;
        int s = 1;
        for(int v: to[u]) {
            if (v == p) continue;
            init_tree_data(v, u, d+1);
            s += sz[v];
        }
        sz[u] = s;
    }
    void assign_idx(int u, int h, int &nxt, int p=-1) {
        head[u] = h;
        inv[nxt] = u;
        idx[u] = nxt++;
        if (sz[u] == 1) {
            ridx[u] = nxt;
            return;
        }
        int mxsize = 0;
        int mi;
        for(int v: to[u]) {
            if (v == p) continue;
            if (sz[v] > mxsize) {
                mxsize = sz[v];
                mi = v;
            }
        }
        assign_idx(mi, h, nxt, u);
        for(int v: to[u]) {
            if (v == p || v == mi) continue;
            assign_idx(v, v, nxt, u);
        }
        ridx[u] = nxt;
    }

    int lca(int u, int v) {
        while(head[u] != head[v]) {
            if (depth[head[u]] > depth[head[v]]) u = parent[head[u]];
            else v = parent[head[v]];
        }
        return (depth[u] < depth[v] ? u : v);
    }
    // returns (paths upto lca from x (excluding lca), those from y, lca)
    std::tuple<std::vector<std::pair<int, int>>, std::vector<std::pair<int, int>>, int> paths(int x, int y) {
        std::tuple<std::vector<std::pair<int, int>>, std::vector<std::pair<int, int>>, int> ret;
        std::vector<std::pair<int, int>>& x_paths = get<0>(ret);
        std::vector<std::pair<int, int>>& y_paths = get<1>(ret);
        int& lca = get<2>(ret);
        while(head[x] != head[y]) {
            int xhead = head[x], yhead = head[y];
            if (depth[xhead] > depth[yhead]) {
                x_paths.emplace_back(x, xhead);
                x = parent[xhead];
            } else {
                y_paths.emplace_back(y, yhead);
                y = parent[yhead];
            }
        }
        if (depth[x] > depth[y]) {
            int ychild = inv[idx[y] + 1];
            x_paths.emplace_back(x, ychild);
            x = y;
        } else if (depth[x] < depth[y]) {
            int xchild = inv[idx[x] + 1];
            y_paths.emplace_back(y, xchild);
            y = x;
        }
        lca = x;
        return ret;
    }
    
    int dist(int u, int v) {
        int w = lca(u, v);
        return depth[u] + depth[v] - 2 * depth[w];
    }
};

template <class S, S (*op)(S, S)>
struct DisjointSparseTable {
  const int n;
  const vector<unsigned char> msb;
  const vector<vector<S>> d;
  DisjointSparseTable(vector<S> a) : n(a.size()), msb(build_msb_table(n)), d(build_table(move(a))) {}

  vector<unsigned char> build_msb_table(int n) {
    if (n <= 1) return {};
    unsigned char k_max = 32 - __builtin_clz(n - 1);
    vector<unsigned char> res(1 << k_max);
    unsigned char* p = res.data();
    for (unsigned char k = 0; k < k_max; k++) {
      memset(p + (1U << k), k, 1U << k);
    }
    return res;
  }
  vector<vector<S>> build_table(vector<S> a) {
    const int n = a.size();
    vector<vector<S>> res(1);
    if (n >= 2) {
      const int i_max = n - 1, k_max = 32 - __builtin_clz(i_max);
      res.resize(k_max);
      for (int k = 1; k < k_max; k++) {
        vector<S> t(i_max >> k & 1 ? n : i_max & ~0U << k);
        for (int c = 1 << k; c < n; c += 1 << (k + 1)) {
          int l = c - (1 << k);
          int r = min(n, c + (1 << k));
          t[c - 1] = a[c - 1];
          for (int i = c - 2; i >= l; i--) t[i] = op(a[i], t[i + 1]);
          t[c] = a[c];
          for (int i = c + 1; i < r; i++) t[i] = op(t[i - 1], a[i]);
        }
        res[k] = move(t);
      }
    }
    res[0] = move(a);
    return res;
  }

  S query(int l, int r) { return query_closed(l, r - 1); }
  S query_closed(int l, int r) {
    assert(0 <= l && l <= r && r < n);
    if (l == r) return d[0][l];
    auto k = msb[l ^ r];
    return op(d[k][l], d[k][r]);
  }
};

int op_min(int x, int y) { return min(x, y); }
int op_max(int x, int y) { return max(x, y); }

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  struct E {
    int a, b, c;
  };
  VVI to(n);
  vector<E> es(n - 1);
  for(auto& [a, b, c] : es) {
    cin >> a >> b >> c;
    a--, b--;
    to[a].emplace_back(b);
    to[b].emplace_back(a);
  }
  HLD hld(to);
  VI init_vec(n);
  for(auto& [a, b, c] : es) {
    if (hld.depth[a] < hld.depth[b]) swap(a, b);
    init_vec[hld.idx[a]] = c;
  }
  constexpr int INF = 1001001001;
  DisjointSparseTable<int, op_min> RmQ(init_vec);
  DisjointSparseTable<int, op_max> RMQ(init_vec);
  vector<int> ord(n - 1);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(), [&](int i, int j) {
    return es[i].c > es[j].c;
  });
  ord.emplace_back(n - 1);
  es.push_back({-1, -1, -1});
  int u = -1, v = -1;
  int ans = INF;
  rep(i, n - 1) {
    auto [a, b, c] = es[ord[i]];
    if (i == 0) {
      u = a;
      v = b;
    } else {
      int d = hld.dist(u, v);
      if (hld.dist(a, u) + hld.dist(a, v) == d && hld.dist(b, u) + hld.dist(b, v) == d) {
        ;
      } else {
        int nu = -1, nv = -1;
        if (hld.dist(a, u) > hld.dist(b, u)) nu = a;
        else nu = b;
        if (hld.dist(a, u) > hld.dist(a, v)) nv = u;
        else nv = v;
        int nd = hld.dist(nu, nv);
        bool ok = true;
        for (int t : {a, b, u, v}) {
          if (hld.dist(t, nu) + hld.dist(t, nv) != nd) {
            ok = false;
            break;
          }
        }
        if (!ok) break;
        u = nu;
        v = nv;
      }
    }
    int mn = INF, mx = -INF;
    auto [p1, p2, _] = hld.paths(u, v);
    rep(_, 2) {
      for (auto [x, y] : p1) {
        int ix = hld.idx[x], iy = hld.idx[y];
        chmin(mn, RmQ.query_closed(iy, ix));
        chmax(mx, RMQ.query_closed(iy, ix));
      }
      swap(p1, p2);
    }
    chmin(ans, max(mx - mn, es[ord[i + 1]].c));
  }
  cout << ans << '\n';
}
0