結果

問題 No.898 tri-βutree
ユーザー simansiman
提出日時 2022-10-05 08:02:35
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 779 ms / 4,000 ms
コード長 3,397 bytes
コンパイル時間 1,442 ms
コンパイル使用メモリ 110,944 KB
実行使用メモリ 40,072 KB
最終ジャッジ日時 2023-08-30 01:30:39
合計ジャッジ時間 18,426 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 465 ms
40,072 KB
testcase_01 AC 8 ms
13,200 KB
testcase_02 AC 9 ms
13,296 KB
testcase_03 AC 9 ms
13,296 KB
testcase_04 AC 10 ms
13,356 KB
testcase_05 AC 9 ms
13,364 KB
testcase_06 AC 10 ms
13,396 KB
testcase_07 AC 763 ms
33,456 KB
testcase_08 AC 768 ms
33,020 KB
testcase_09 AC 779 ms
33,832 KB
testcase_10 AC 770 ms
33,448 KB
testcase_11 AC 768 ms
33,120 KB
testcase_12 AC 762 ms
33,184 KB
testcase_13 AC 776 ms
33,204 KB
testcase_14 AC 766 ms
34,268 KB
testcase_15 AC 771 ms
32,896 KB
testcase_16 AC 773 ms
34,000 KB
testcase_17 AC 772 ms
33,112 KB
testcase_18 AC 777 ms
32,940 KB
testcase_19 AC 777 ms
33,832 KB
testcase_20 AC 769 ms
32,848 KB
testcase_21 AC 768 ms
32,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_V = 500000;

struct Node {
  int v;
  int depth;

  Node(int v = -1, int depth = -1) {
    this->v = v;
    this->depth = depth;
  }

  bool operator<(const Node &n) const {
    return depth < n.depth;
  }
};

class StaticRMQ {
  public:
    vector <Node> dat;
    int N;

    void init(int n) {
      N = 1;

      while (N < n) N *= 2;

      for (int i = 0; i < 2 * N - 1; ++i) {
        dat.push_back(Node(-1, MAX_V));
      }
    }

    void update(int k, Node a) {
      k += N - 1;
      dat[k] = a;

      while (k > 0) {
        k = (k - 1) / 2;
        dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
      }
    }

    ll find_min(int l, int r) {
      return query(l, r, 0, 0, N).v;
    }

  private:

    Node query(int a, int b, int k, int l, int r) {
      if (r <= a || b <= l) return Node(-1, MAX_V);

      if (a <= l && r <= b) {
        return dat[k];
      } else {
        Node vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        Node vr = query(a, b, k * 2 + 2, (l + r) / 2, r);

        return min(vl, vr);
      }
    }
};

class LCA {
  public:
    vector<int> vs;
    vector<int> depth;
    vector<int> id;
    vector <vector<int> > G;
    StaticRMQ rmq;

    LCA(int N, int root, vector <vector<int> > _G) {
      G = _G;
      int k = 0;
      vs = vector<int>(2 * N - 1);
      depth = vector<int>(2 * N - 1);
      id = vector<int>(N, -1);
      dfs(root, -1, 0, k);
      rmq.init(k);

      for (int i = 0; i < k; ++i) {
        rmq.update(i, Node(vs[i], depth[i]));
      }
    }

    int find_parent(int u, int v) {
      return rmq.find_min(min(id[u], id[v]), max(id[u], id[v]));
    }

  private:
    void dfs(int v, int p, int d, int &k) {
      id[v] = k;
      vs[k] = v;
      depth[k++] = d;

      for (int i = 0; i < G[v].size(); ++i) {
        if (G[v][i] != p) {
          dfs(G[v][i], v, d + 1, k);
          vs[k] = v;
          depth[k++] = d;
        }
      }
    }
};

vector<ll> W[100010];
ll dist[100010];
vector<vector<int>> G(100010);

void dfs(int v, int parent, ll w) {
  dist[v] = w;

  for (int i = 0; i < G[v].size(); ++i) {
    int u = G[v][i];
    ll nw = w + W[v][i];
    if (u == parent) continue;

    dfs(u, v, nw);
  }
}

void setup_dist() {
  memset(dist, 0, sizeof(dist));

  dfs(0, -1, 0);
}

ll calc_cost(int x, int y, int z, LCA &lca) {
  int p = lca.find_parent(x, y);
  ll fw = dist[x] + dist[y] - 2 * dist[p];
  int sp = lca.find_parent(p, z);
  ll sw = dist[p] + dist[z] - 2 * dist[sp];
  // fprintf(stderr, "x: %d, y: %d, p: %d, fw: %lld\n", x, y, p, fw);

  return fw + sw;
}

int main() {
  int N;
  cin >> N;

  for (int i = 0; i < N - 1; ++i) {
    ll u, v, w;
    cin >> u >> v >> w;
    G[u].push_back(v);
    G[v].push_back(u);
    W[u].push_back(w);
    W[v].push_back(w);
  }

  setup_dist();
  LCA lca(N, 0, G);
  int Q;
  cin >> Q;

  for (int i = 0; i < Q; ++i) {
    int x, y, z;
    cin >> x >> y >> z;

    ll c1 = calc_cost(x, y, z, lca);
    ll c2 = calc_cost(x, z, y, lca);
    ll c3 = calc_cost(y, z, x, lca);
    // fprintf(stderr, "c1: %lld, c2: %lld, c3: %lld\n", c1, c2, c3);
    cout << min(c1, min(c2, c3)) << endl;
  }

  return 0;
}
0