結果

問題 No.1094 木登り / Climbing tree
ユーザー simansiman
提出日時 2022-04-29 03:20:05
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 956 ms / 2,000 ms
コード長 3,315 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 147,320 KB
実行使用メモリ 75,836 KB
最終ジャッジ日時 2024-04-25 19:47:49
合計ジャッジ時間 24,147 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 891 ms
61,444 KB
testcase_02 AC 294 ms
75,836 KB
testcase_03 AC 233 ms
6,944 KB
testcase_04 AC 257 ms
29,084 KB
testcase_05 AC 404 ms
54,192 KB
testcase_06 AC 494 ms
20,596 KB
testcase_07 AC 916 ms
60,388 KB
testcase_08 AC 931 ms
60,760 KB
testcase_09 AC 937 ms
61,220 KB
testcase_10 AC 912 ms
60,672 KB
testcase_11 AC 901 ms
61,988 KB
testcase_12 AC 899 ms
60,572 KB
testcase_13 AC 918 ms
60,712 KB
testcase_14 AC 917 ms
60,488 KB
testcase_15 AC 499 ms
19,400 KB
testcase_16 AC 682 ms
61,084 KB
testcase_17 AC 587 ms
37,980 KB
testcase_18 AC 536 ms
29,344 KB
testcase_19 AC 637 ms
48,384 KB
testcase_20 AC 908 ms
60,596 KB
testcase_21 AC 597 ms
38,688 KB
testcase_22 AC 956 ms
61,112 KB
testcase_23 AC 888 ms
60,388 KB
testcase_24 AC 892 ms
60,656 KB
testcase_25 AC 890 ms
60,980 KB
testcase_26 AC 906 ms
61,592 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;
        }
      }
    }
};

int main() {
  int N;
  cin >> N;
  int a, b, c;
  vector<vector<int>> G(N);
  vector<vector<int>> C(N);
  for (int i = 1; i <= N - 1; ++i) {
    cin >> a >> b >> c;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
    C[a].push_back(c);
    C[b].push_back(c);
  }

  int root = 0;

  vector<int> p_cost(N, 0);
  vector<bool> visited(N, false);
  queue<int> que;
  queue<int> c_que;
  que.push(root);
  c_que.push(0);
  while (not que.empty()) {
    int v = que.front();
    que.pop();
    int cost = c_que.front();
    c_que.pop();

    if (visited[v]) continue;
    visited[v] = true;
    p_cost[v] = cost;

    for (int i = 0; i < G[v].size(); ++i) {
      int u = G[v][i];
      int c = C[v][i];

      que.push(u);
      c_que.push(cost + c);
    }
  }

  int Q;
  cin >> Q;

  LCA lca(N, root, G);
  int s, t;
  for (int i = 0; i < Q; ++i) {
    cin >> s >> t;
    --s;
    --t;
    int p = lca.find_parent(s, t);

    if (s == t) {
      cout << 0 << endl;
    } else {
      int c1 = p_cost[s];
      int c2 = p_cost[t];
      int c3 = p_cost[p];
      cout << c1 + c2 - 2 * c3 << endl;
    }
  }

  return 0;
}
0