結果
問題 | No.898 tri-βutree |
ユーザー | renjyaku_int |
提出日時 | 2020-03-27 21:56:08 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 200 ms / 4,000 ms |
コード長 | 4,319 bytes |
コンパイル時間 | 1,952 ms |
コンパイル使用メモリ | 186,216 KB |
実行使用メモリ | 31,292 KB |
最終ジャッジ日時 | 2024-11-08 23:33:36 |
合計ジャッジ時間 | 7,074 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 90 ms
31,292 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 198 ms
25,724 KB |
testcase_08 | AC | 193 ms
25,604 KB |
testcase_09 | AC | 200 ms
25,596 KB |
testcase_10 | AC | 192 ms
25,728 KB |
testcase_11 | AC | 191 ms
25,728 KB |
testcase_12 | AC | 183 ms
25,716 KB |
testcase_13 | AC | 186 ms
25,724 KB |
testcase_14 | AC | 187 ms
25,600 KB |
testcase_15 | AC | 184 ms
25,724 KB |
testcase_16 | AC | 185 ms
25,592 KB |
testcase_17 | AC | 185 ms
25,724 KB |
testcase_18 | AC | 184 ms
25,720 KB |
testcase_19 | AC | 179 ms
25,728 KB |
testcase_20 | AC | 183 ms
25,520 KB |
testcase_21 | AC | 187 ms
25,592 KB |
ソースコード
//#include <tourist> #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> p; const int INF = 1e9; const ll LINF = ll(1e18) + 1; const int MOD = 1000000007; const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(v) v.begin(), v.end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) \ { \ cout << x << ' '; \ } \ cout << endl; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } //cout<<fixed<<setprecision(15);有効数字15桁 //-std=c++14 ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int n, m; vector<int> a; template <typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template <typename T> using Matrix = vector<vector<T>>; 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]; } }; vector<vector<pair<ll,ll>>> v; vector<ll> dep; void dfs(int k,int prev,ll d){ dep[k]=d; rep(i,v[k].size()){ if(v[k][i].first==prev)continue; dfs(v[k][i].first,k,d+v[k][i].second); } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; v.resize(n); UnWeightedGraph g(n); for (int i = 0; i < n-1; i++) { ll temp,temp1,temp2; cin >> temp>>temp1>>temp2; v[temp].push_back(make_pair(temp1,temp2)); v[temp1].push_back(make_pair(temp,temp2)); g[temp].push_back(temp1); g[temp1].push_back(temp); } dep.resize(n); dfs(0,-1,0); DoublingLowestCommonAncestor<UnWeightedGraph> lca(g); lca.build(); int q; cin>>q; vector<ll> ans; while(q--){ ll x,y,z; cin>>x>>y>>z; ll sum=0; int temp=lca.query(x,y); sum+=dep[x]-dep[temp]; sum+=dep[y]-dep[temp]; temp=lca.query(z,y); sum+=dep[z]-dep[temp]; sum+=dep[y]-dep[temp]; temp=lca.query(x,z); sum+=dep[x]-dep[temp]; sum+=dep[z]-dep[temp]; ans.push_back(sum/2); } rep(i,ans.size()) cout << ans[i] << "\n"; }