結果
問題 | No.1212 Second Path |
ユーザー | SHIJOU |
提出日時 | 2020-09-04 15:27:08 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 920 ms / 3,000 ms |
コード長 | 5,912 bytes |
コンパイル時間 | 3,178 ms |
コンパイル使用メモリ | 232,072 KB |
実行使用メモリ | 211,712 KB |
最終ジャッジ日時 | 2024-11-25 21:30:11 |
合計ジャッジ時間 | 34,482 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 446 ms
209,408 KB |
testcase_01 | AC | 857 ms
208,000 KB |
testcase_02 | AC | 899 ms
203,700 KB |
testcase_03 | AC | 918 ms
204,412 KB |
testcase_04 | AC | 920 ms
194,572 KB |
testcase_05 | AC | 890 ms
194,864 KB |
testcase_06 | AC | 497 ms
178,316 KB |
testcase_07 | AC | 658 ms
177,788 KB |
testcase_08 | AC | 676 ms
177,816 KB |
testcase_09 | AC | 647 ms
177,768 KB |
testcase_10 | AC | 665 ms
177,776 KB |
testcase_11 | AC | 651 ms
177,788 KB |
testcase_12 | AC | 638 ms
177,916 KB |
testcase_13 | AC | 630 ms
177,792 KB |
testcase_14 | AC | 641 ms
177,732 KB |
testcase_15 | AC | 645 ms
177,792 KB |
testcase_16 | AC | 643 ms
177,800 KB |
testcase_17 | AC | 433 ms
208,772 KB |
testcase_18 | AC | 230 ms
165,956 KB |
testcase_19 | AC | 237 ms
166,128 KB |
testcase_20 | AC | 232 ms
166,152 KB |
testcase_21 | AC | 235 ms
166,196 KB |
testcase_22 | AC | 240 ms
166,180 KB |
testcase_23 | AC | 197 ms
165,788 KB |
testcase_24 | AC | 210 ms
166,268 KB |
testcase_25 | AC | 213 ms
166,060 KB |
testcase_26 | AC | 210 ms
166,100 KB |
testcase_27 | AC | 217 ms
165,932 KB |
testcase_28 | AC | 215 ms
165,940 KB |
testcase_29 | AC | 732 ms
211,712 KB |
testcase_30 | AC | 737 ms
211,584 KB |
testcase_31 | AC | 771 ms
211,584 KB |
testcase_32 | AC | 608 ms
174,608 KB |
testcase_33 | AC | 461 ms
172,544 KB |
testcase_34 | AC | 601 ms
177,016 KB |
testcase_35 | AC | 287 ms
166,692 KB |
testcase_36 | AC | 535 ms
175,040 KB |
testcase_37 | AC | 533 ms
174,016 KB |
testcase_38 | AC | 526 ms
174,576 KB |
testcase_39 | AC | 406 ms
170,588 KB |
testcase_40 | AC | 245 ms
166,196 KB |
testcase_41 | AC | 539 ms
174,736 KB |
testcase_42 | AC | 146 ms
161,920 KB |
testcase_43 | AC | 151 ms
162,048 KB |
testcase_44 | AC | 150 ms
162,048 KB |
testcase_45 | AC | 476 ms
178,320 KB |
testcase_46 | AC | 476 ms
178,320 KB |
testcase_47 | AC | 476 ms
178,324 KB |
ソースコード
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >; const int INF = 0xccccccc; const ll LINF = 922337203685477580LL; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} template<class T> struct lca { int n, root, l; vector<vector<int> > to, par; vector<vector<T> > co; vector<int> dep; vector<T> costs; lca(int n):n(n), to(n), co(n), dep(n), costs(n) { l = 0; while((1<<l) < n) ++l; par = vector<vector<int> >(n+1, vector<int>(l, n)); } void add(int a, int b, T c=0) { to[a].push_back(b); co[a].push_back(c); to[b].push_back(a); co[b].push_back(c); } void dfs(int v, int d=0, T c=0, int p=-1) { if(p != -1) par[v][0] = p; dep[v] = d; costs[v] = c; for(int i = 0; i < to[v].size(); ++i) { int u = to[v][i]; if(u == p) continue; dfs(u, d+1, c+co[v][i], v); } } void init(int _root=0) { root = _root; dfs(root); for(int i = 0; i < l-1; ++i) { for(int v = 0; v < n; ++v) { par[v][i+1] = par[par[v][i]][i]; } } } int lca_(int a, int b) { if(dep[a] > dep[b]) swap(a, b); int gap = dep[b]-dep[a]; for(int i = l-1; i >= 0; --i) { int len = 1<<i; if(gap >= len) { gap -= len; b = par[b][i]; } } if(a == b) return a; for(int i = l-1; i >= 0; --i) { int na = par[a][i]; int nb = par[b][i]; if(na != nb) { a = na; b = nb; } } return par[a][0]; } int length(int a, int b) { int c = lca_(a, b); return dep[a]+dep[b]-dep[c]*2; } T dist(int a, int b) { int c = lca_(a, b); return costs[a]+costs[b]-costs[c]*2; } }; template<class T> struct SegTree { using FX = function<T(T, T)>; int n; FX fx; const T ex; vector<T> dat; SegTree(int n_, FX fx_, T ex_):fx(fx_), ex(ex_), n(1) { while(n < n_) n <<= 1; dat.assign((n<<1)-1, ex); } inline int chld(int k) {return (k<<1)+1;} inline int chrd(int k) {return (k<<1)+2;} void update(int i, T x) { i += n-1; dat[i] = x; while(i) { i = (i-1)>>1; dat[i] = fx(dat[chld(i)], dat[chrd(i)]); } } inline T query(int a, int b) {return query(a, b, 0, 0, n);} T query(int a, int b, int k, int l, int r) { if(r <= a || b <= l) return ex; if(a <= l && r <= b) return dat[k]; T vl = query(a, b, chld(k), l, (l+r)>>1); T vr = query(a, b, chrd(k), (l+r)>>1, r); return fx(vl, vr); } const T &operator[](int idx) const {return dat[idx+n-1];} }; struct edge { int to, cost; }; #define N 100100 //head int n; vector<edge> G[N]; int q; int x[N], y[N], z[N]; vi dat1[N]; vector<P> dat2[N]; queue<int> info1[N], info2[N]; lca<ll> L(N); SegTree<int> rng(N, [](const int i, const int j)->int{return min(i, j);}, 0x7fffffff); void dfs(int i=0, int j=-1) { list<P> lst; for(edge &e:G[i]) { int ne = e.to; list<P>::iterator itr = lst.begin(); while(true) { if(itr == lst.end()) { if(lst.size() < 3) { lst.emplace_back(e.cost, ne); } break; } if(itr->first > e.cost) { lst.emplace(itr, e.cost, ne); if(lst.size() > 3) lst.pop_back(); break; } itr = next(itr); } } while(lst.size() < 3) lst.emplace_back(0x7fffffff, -10); for(edge &e:G[i]) { int ne = e.to; if(ne == j) continue; list<P>::iterator itr = lst.begin(); while(itr->second == j or itr->second == ne) itr = next(itr); rng.update(L.dep[i], itr->first); dfs(ne, i); } for(int x:dat1[i]) { if(x == i) { info1[i].emplace(0x7fffffff); continue; } list<P>::iterator itr = lst.begin(); while(itr->second == j) itr = next(itr); if(L.dep[i]-1 == L.dep[x]) { info1[i].emplace(itr->first); } else { info1[i].emplace(min(itr->first, rng.query(L.dep[x]+1, L.dep[i]))); } } for(P x:dat2[i]) { list<P>::iterator itr = lst.begin(); while(true) { if(itr->second == -10) break; int u = L.lca_(x.first, itr->second); int v = L.lca_(x.second, itr->second); if((u != i and u != j) or (v != i and v != j)) { itr = next(itr); continue; } break; } info2[i].emplace(itr->first); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; rep(i, n-1) { int u, v, w; cin >> u >> v >> w; u--; v--; L.add(u, v, w); G[u].emplace_back((edge){v, w}); G[v].emplace_back((edge){u, w}); } L.init(); cin >> q; rep(i, q) { cin >> x[i] >> y[i]; x[i]--; y[i]--; z[i] = L.lca_(x[i], y[i]); dat1[x[i]].emplace_back(z[i]); dat1[y[i]].emplace_back(z[i]); dat2[z[i]].emplace_back(x[i], y[i]); } dfs(); rep(i, q) { //cout << P(info1[x[i]].front(), info1[y[i]].front()) << ' ' << info2[z[i]].front() << endl; int u = min({info1[x[i]].front(), info1[y[i]].front(), info2[z[i]].front()}); info1[x[i]].pop(); info1[y[i]].pop(); info2[z[i]].pop(); if(u == 0x7fffffff) cout << "-1\n"; else cout << u*2+L.dist(x[i], y[i]) << '\n'; //cout << L.dist(x[i], y[i]) << "\n\n"; } }