結果
問題 | No.1212 Second Path |
ユーザー | SSRS |
提出日時 | 2020-08-30 16:57:01 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 5,664 bytes |
コンパイル時間 | 3,379 ms |
コンパイル使用メモリ | 240,244 KB |
実行使用メモリ | 81,916 KB |
最終ジャッジ日時 | 2024-11-15 11:40:05 |
合計ジャッジ時間 | 127,040 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,397 ms
52,544 KB |
testcase_01 | TLE | - |
testcase_02 | TLE | - |
testcase_03 | TLE | - |
testcase_04 | TLE | - |
testcase_05 | TLE | - |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | TLE | - |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | AC | 1,189 ms
52,284 KB |
testcase_18 | AC | 344 ms
53,128 KB |
testcase_19 | AC | 350 ms
11,252 KB |
testcase_20 | AC | 336 ms
53,380 KB |
testcase_21 | AC | 278 ms
11,060 KB |
testcase_22 | AC | 314 ms
53,256 KB |
testcase_23 | AC | 172 ms
10,624 KB |
testcase_24 | AC | 198 ms
6,816 KB |
testcase_25 | AC | 199 ms
6,820 KB |
testcase_26 | AC | 202 ms
6,820 KB |
testcase_27 | AC | 197 ms
6,824 KB |
testcase_28 | AC | 197 ms
5,996 KB |
testcase_29 | TLE | - |
testcase_30 | TLE | - |
testcase_31 | TLE | - |
testcase_32 | TLE | - |
testcase_33 | AC | 2,636 ms
26,532 KB |
testcase_34 | TLE | - |
testcase_35 | AC | 1,027 ms
9,084 KB |
testcase_36 | TLE | - |
testcase_37 | TLE | - |
testcase_38 | TLE | - |
testcase_39 | AC | 2,021 ms
21,120 KB |
testcase_40 | AC | 540 ms
5,844 KB |
testcase_41 | TLE | - |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
testcase_44 | AC | 2 ms
5,248 KB |
testcase_45 | TLE | - |
testcase_46 | TLE | - |
testcase_47 | TLE | - |
ソースコード
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const int LOG = 18; struct lowest_common_ancestor{ vector<int> d; vector<vector<int>> p; lowest_common_ancestor(){ } lowest_common_ancestor(vector<int> &P, vector<vector<int>> &C){ int N = P.size(); d = vector<int>(N, 0); queue<int> Q; Q.push(0); while (!Q.empty()){ int v = Q.front(); Q.pop(); for (int w : C[v]){ d[w] = d[v] + 1; Q.push(w); } } p = vector<vector<int>>(LOG, vector<int>(N, -1)); for (int i = 0; i < N; i++){ p[0][i] = P[i]; } for (int i = 1; i < LOG; i++){ for (int j = 0; j < N; j++){ if (p[i - 1][j] != -1){ p[i][j] = p[i - 1][p[i - 1][j]]; } } } } int query(int u, int v){ if (d[u] > d[v]){ swap(u, v); } for (int k = 0; k < LOG; k++){ if ((d[v] - d[u]) >> k & 1){ v = p[k][v]; } } if (u == v){ return u; } for (int k = LOG - 1; k >= 0; k--){ if (p[k][u] != p[k][v]){ u = p[k][u]; v = p[k][v]; } } return p[0][u]; } }; template <typename T> struct binary_indexed_tree{ int N; vector<T> BIT; binary_indexed_tree(){ } binary_indexed_tree(int n){ N = 1; while (N < n){ N *= 2; } BIT = vector<T>(N + 1, 0); } void add(int i, T x){ i++; while (i <= N){ BIT[i] += x; i += i & -i; } } T sum(int i){ T ans = 0; while (i > 0){ ans += BIT[i]; i -= i & -i; } return ans; } T query(int L, int R){ return sum(R) - sum(L); } }; template <typename T> struct euler_tour{ lowest_common_ancestor G; vector<T> A; vector<int> left; vector<int> right; binary_indexed_tree<T> BIT; void dfs(vector<vector<int>> &c, int v){ left[v] = A.size(); A.push_back(0); for (int w : c[v]){ dfs(c, w); } right[v] = A.size(); A.push_back(0); } euler_tour(vector<int> &p, vector<vector<int>> &c){ int N = p.size(); G = lowest_common_ancestor(p, c); left = vector<int>(N); right = vector<int>(N); dfs(c, 0); BIT = binary_indexed_tree<int>(N * 2); } void add(int v){ BIT.add(left[v], 1); BIT.add(right[v], -1); } T query(int v, int w){ int u = G.query(v, w); return BIT.query(left[u], left[v] + 1) + BIT.query(left[u] + 1, left[w] + 1); } }; template <typename T> struct weighted_tree_distance{ vector<int> d; vector<T> s; vector<vector<int>> next; weighted_tree_distance(vector<vector<pair<T, int>>> &E){ int N = E.size(); d = vector<int>(N, 0); s = vector<T>(N, 0); next = vector<vector<int>>(LOG, vector<int>(N, -1)); queue<int> Q; Q.push(0); while (!Q.empty()){ int v = Q.front(); Q.pop(); for (auto e : E[v]){ T c = e.first; int w = e.second; if (w != next[0][v]){ next[0][w] = v; d[w] = d[v] + 1; s[w] = s[v] + c; Q.push(w); } } } for (int i = 1; i < LOG; i++){ for (int j = 0; j < N; j++){ if (next[i - 1][j] != -1){ next[i][j] = next[i - 1][next[i - 1][j]]; } } } } int lca(int u, int v){ if (d[u] < d[v]){ swap(u, v); } for (int i = 0; i < LOG; i++){ if ((d[u] - d[v]) >> i & 1){ u = next[i][u]; } } if (u == v){ return u; } else { for (int i = LOG - 1; i >= 0; i--){ if (next[i][u] != next[i][v]){ u = next[i][u]; v = next[i][v]; } } return next[0][u]; } } int dist1(int u, int v){ return d[u] + d[v] - 2 * d[lca(u, v)]; } T dist2(int u, int v){ return s[u] + s[v] - 2 * s[lca(u, v)]; } }; int main(){ int N; cin >> N; vector<vector<pair<long long, int>>> E(N); vector<tuple<int, int, int>> edges; for (int i = 0; i < N - 1; i++){ int u, v, w; cin >> u >> v >> w; u--; v--; E[u].push_back(make_pair(w, v)); E[v].push_back(make_pair(w, u)); edges.push_back(make_tuple(w, u, v)); } vector<int> p(N, -1); vector<vector<int>> c(N); vector<long long> a(N, 0); queue<int> q; q.push(0); while (!q.empty()){ int v = q.front(); q.pop(); for (auto e : E[v]){ int d = e.first; int w = e.second; if (w != p[v]){ p[w] = v; c[v].push_back(w); a[w] = d; q.push(w); } } } int Q; cin >> Q; vector<int> x(Q), y(Q); for (int i = 0; i < Q; i++){ cin >> x[i] >> y[i]; x[i]--; y[i]--; } sort(edges.begin(), edges.end()); vector<int> tv(Q, N); vector<int> fv(Q, 0); while (1){ bool upd = false; vector<vector<int>> check(N - 1); for (int i = 0; i < Q; i++){ if (tv[i] - fv[i] > 1){ upd = true; int mid = (tv[i] + fv[i]) / 2; check[mid - 1].push_back(i); } } if (!upd){ break; } euler_tour<int> T1(p, c), T2(p, c); vector<bool> used(N, false); for (int i = 0; i < N - 1; i++){ int u = get<1>(edges[i]); int v = get<2>(edges[i]); T1.add(u); T1.add(v); if (u == p[v]){ T2.add(v); used[v] = true; } else { T2.add(u); used[u] = true; } for (int id : check[i]){ int a = T1.query(x[id], y[id]); int b = T2.query(x[id], y[id]); int l = T1.G.query(x[id], y[id]); if (used[l]){ b--; } if (a > b * 2){ tv[id] = i + 1; } else { fv[id] = i + 1; } } } } weighted_tree_distance<long long> T(E); for (int i = 0; i < Q; i++){ if (tv[i] == N){ cout << -1 << endl; } else { cout << T.dist2(x[i], y[i]) + get<0>(edges[tv[i] - 1]) * 2 << endl; } } }