結果

問題 No.898 tri-βutree
ユーザー ugis_progugis_prog
提出日時 2019-10-04 23:58:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,523 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 189,972 KB
実行使用メモリ 38,816 KB
最終ジャッジ日時 2024-04-14 12:20:14
合計ジャッジ時間 12,963 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 340 ms
38,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

template< typename G >
struct DoublingLowestCommonAncestor {
  const int LOG;
  vector< int > dep;
  const G &g;
  vector< vector< int > > table;

  DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector< int >(g.size(), -1));
  }

  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) dfs(to, idx, d + 1);
    }
  }

  void build() {
    dfs(0, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }

  int query(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    for(int i = LOG - 1; i >= 0; i--) {
      if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    
    return table[0][u];
  }
};

#include<vector>
#include<algorithm>
#include<cstdint>

template<typename GraphType>
struct EularTour{
    std::vector<std::vector<GraphType>> tree;
    std::vector<size_t> tourArr;
    std::vector<size_t> begin;
    std::vector<size_t> end;
    size_t step;

    EularTour() = delete;
    EularTour(const std::vector<std::vector<GraphType>>& Graph, const size_t GraphSize) : tree(Graph), step(0){
        begin.resize(GraphSize);
        end.resize(GraphSize);
        //visited.assign(GraphSize, false);
    }

    void makeTour(size_t v, size_t par=1e9){
        begin[v] = step;
        tourArr.emplace_back(v);
        step++;
        for(GraphType& next : tree[v]){
            if(next == par) continue;
            makeTour(next, step, v);
            tourArr.emplace_back(v);
            step++;
        }
        end[v] = step;
    }

    bool isChild(GraphType x, GraphType y){
        if(begin[x] < begin[y] && begin[y] < end[x]) return true;
        return false;
    }
};

vector<long long> wei;
vector<int> d;
void dfs(vector<vector<pair<int,long long>>>& g, int v, int p){
    for(auto e : g[v]){
        if(e.first == p) continue;
        wei[e.first] += (e.second + wei[v]);
        d[e.first] = d[v]+1;
        dfs(g, e.first, v);
    }
}


int main(){
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    vector<vector<pair<int,long long>>> g(N);
    wei.assign(N, 0);
    d.assign(N, 0);
    for(int i = 0; i < N-1; i++){
        int u,v,w;
        cin >> u >> v >> w;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
        g[u].emplace_back(make_pair(v,w));
        g[v].emplace_back(make_pair(u,w));
    }
    
    dfs(g, 0, -1);
    DoublingLowestCommonAncestor<vector<vector<int>>> lca(G);
    lca.build();
    EularTour<int> et(G, G.size());

    int Q, x, y, z;
    cin >> Q;
    for(int i = 0; i < Q; i++){
        cin >> x >> y >> z;
        vector<pair<int,int>> v = {{d[x],x}, {d[y], y}, {d[z], z}};
        sort(v.rbegin(), v.rend());
        long long W = 0;
        x = v[0].second;
        y = v[1].second;
        z = v[2].second;
        if(et.isChild(x, z)) swap(y, z);
        
        int a = lca.query(x, y);
        long long p = (wei[x] - wei[a]) + (wei[y] - wei[a]);
        W += p;
        int b = lca.query(a, z);
        long long q = (wei[a] - wei[b]) + (wei[z] - wei[b]);
        W += q;
        cout << W << endl;
    }
}
0