結果

問題 No.1094 木登り / Climbing tree
ユーザー simansiman
提出日時 2022-04-29 03:04:16
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,245 bytes
コンパイル時間 3,040 ms
コンパイル使用メモリ 108,276 KB
実行使用メモリ 75,624 KB
最終ジャッジ日時 2023-09-10 19:27:00
合計ジャッジ時間 31,006 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 974 ms
60,432 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 983 ms
60,764 KB
testcase_10 AC 994 ms
60,640 KB
testcase_11 AC 988 ms
60,900 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 997 ms
60,776 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1,029 ms
60,580 KB
testcase_23 AC 973 ms
61,224 KB
testcase_24 WA -
testcase_25 AC 977 ms
60,164 KB
testcase_26 AC 985 ms
61,004 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);

    int c1 = p_cost[s];
    int c2 = p_cost[t];
    int c3 = p_cost[p];
    cout << c1 + c2 - 2 * c3 << endl;
  }

  return 0;
}
0