結果

問題 No.898 tri-βutree
ユーザー koyoprokoyopro
提出日時 2020-01-02 16:45:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 566 ms / 4,000 ms
コード長 2,668 bytes
コンパイル時間 1,649 ms
コンパイル使用メモリ 164,848 KB
実行使用メモリ 39,552 KB
最終ジャッジ日時 2024-04-26 09:08:50
合計ジャッジ時間 12,540 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 339 ms
39,552 KB
testcase_01 AC 3 ms
6,144 KB
testcase_02 AC 4 ms
6,016 KB
testcase_03 AC 4 ms
6,144 KB
testcase_04 AC 4 ms
6,016 KB
testcase_05 AC 4 ms
6,144 KB
testcase_06 AC 4 ms
5,888 KB
testcase_07 AC 542 ms
36,096 KB
testcase_08 AC 557 ms
35,968 KB
testcase_09 AC 552 ms
35,968 KB
testcase_10 AC 551 ms
36,096 KB
testcase_11 AC 552 ms
35,968 KB
testcase_12 AC 547 ms
35,968 KB
testcase_13 AC 546 ms
36,096 KB
testcase_14 AC 554 ms
35,968 KB
testcase_15 AC 555 ms
36,096 KB
testcase_16 AC 547 ms
35,968 KB
testcase_17 AC 566 ms
36,096 KB
testcase_18 AC 559 ms
35,968 KB
testcase_19 AC 557 ms
35,968 KB
testcase_20 AC 556 ms
35,968 KB
testcase_21 AC 552 ms
35,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)

int dxy[] = {0, 1, 0, -1, 0};

/*================================*/

//struct Edge {
//    int parent, child;
//    int w = -1;
//    
//    Edge(int p, int c, int w=-1): parent(p), child(c), w(w) {};
//};

struct Edge {
    int src, to, w;
    Edge(int u, int v, int w): src(u), to(v), w(w) {};
};

vector< vector<Edge> > E(111111);
int W[111111];
int D[111111];
int P[111111][20];

class Graph {
    int N;
    public: Graph(int n): N(n) {}
    
    void setup() {
        REP(i,N)REP(j,20) P[i][j]=-1;
        dfs();
        doubling();
    }
    
    void doubling() {
        FOR(j, 1, 20) {
            REP(i, N) {
                P[i][j] = (P[i][j-1] == -1) ? -1 : P[P[i][j-1]][j-1];
            }
        }
    }
    
    void add_edge(int u, int v, int w) {
        E[u].push_back(*new Edge(u,v,w));
        E[v].push_back(*new Edge(v,u,w));
    }
    
    void dfs(int parent=-1, int i=0, int weight=0, int depth=0) {
        P[i][0] = parent;
        W[i] = weight;
        D[i] = depth;
        for (auto e : E[i]) {
            if (e.to == parent) continue;
            dfs(i, e.to, weight + e.w, depth+1);
        }
    }
    
    int lca(int u, int v) {
        // depth調整
        if (D[u] > D[v]) swap(u, v);
        RFOR(i, 0, 20) {
            if ((D[v] - D[u])&(1<<i)) v = P[v][i];
        }
        if (u == v) return u;
        
        // lca
        RFOR(i, 0, 20) {
            if (P[v][i] != P[u][i]) {
                v = P[v][i];
                u = P[u][i];
            }
        }
        return P[u][0];
    }
    
    int route_weight(int u, int v) {
        return W[u] + W[v] - W[lca(u, v)] * 2;
    }
};

int N,Q;

signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin>>N;
    
    Graph G(N);
    
    int u,v,w;
    REP(i, N-1) {
        cin >> u >> v >> w;
        G.add_edge(u, v, w);
    }

    G.setup();
    
    cin >> Q;
    int x,y,z;
    REP(i, Q) {
        cin>>x>>y>>z;
        int ans = (G.route_weight(x, y) + G.route_weight(y, z) + G.route_weight(z, x))/2;
        out("%lld\n", ans);
    }
    
    return 0;
}
0