結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | ei1333333 |
提出日時 | 2019-10-10 00:06:41 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 9,961 bytes |
コンパイル時間 | 3,285 ms |
コンパイル使用メモリ | 242,380 KB |
実行使用メモリ | 47,432 KB |
最終ジャッジ日時 | 2024-11-18 09:19:41 |
合計ジャッジ時間 | 21,809 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 286 ms
47,432 KB |
testcase_01 | AC | 2 ms
6,816 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 | AC | 710 ms
45,456 KB |
testcase_18 | AC | 712 ms
45,432 KB |
testcase_19 | AC | 715 ms
45,440 KB |
testcase_20 | AC | 707 ms
45,364 KB |
testcase_21 | AC | 700 ms
45,504 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 741 ms
45,504 KB |
testcase_28 | AC | 750 ms
45,528 KB |
testcase_29 | AC | 777 ms
45,512 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; using int64 = long long; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } 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 T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } template< typename Monoid = int > struct SplayTree { using F = function< Monoid(Monoid, Monoid) >; using S = function< Monoid(Monoid) >; struct Node { Node *l, *r, *p; Monoid key, sum; int sz; bool rev; bool is_root() { return !p; } Node(const Monoid &key) : key(key), sum(key), sz(1), rev(false), l(nullptr), r(nullptr), p(nullptr) {} }; const F f; const S s; SplayTree(const F &f, const S &s) : f(f), s(s) {} Node *make_node(const Monoid &v = Monoid()) { return new Node(v); } void toggle(Node *t) { swap(t->l, t->r); t->sum = s(t->sum); t->rev ^= true; } void push(Node *t) { if(t->rev) { if(t->l) toggle(t->l); if(t->r) toggle(t->r); t->rev = false; } } void update(Node *t) { t->sz = 1; t->sum = t->key; if(t->l) t->sz += t->l->sz, t->sum = f(t->l->sum, t->sum); if(t->r) t->sz += t->r->sz, t->sum = f(t->sum, t->r->sum); } void rotr(Node *t) { auto *x = t->p, *y = x->p; if((x->l = t->r)) t->r->p = x; t->r = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } void rotl(Node *t) { auto *x = t->p, *y = x->p; if((x->r = t->l)) t->l->p = x; t->l = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } void splay(Node *t) { push(t); while(!t->is_root()) { auto *q = t->p; if(q->is_root()) { if(q->l == t) rotr(t); else rotl(t); } else { auto *r = q->p; if(r->l == q) { if(q->l == t) rotr(q), rotr(t); else rotl(t), rotr(t); } else { if(q->r == t) rotl(q), rotl(t); else rotr(t), rotl(t); } } } } void link(Node *child, Node *parent) { if(!child || !parent) return; splay(child); splay(parent); child->p = parent; parent->r = child; update(parent); } void cut(Node *child) { if(!child) return; splay(child); auto *parent = child->l; child->l = nullptr; parent->p = nullptr; update(child); } void evert(Node *t) { splay(t); toggle(t); push(t); } Node *find_first(Node *t) { splay(t); while(t->l) { t = t->l; push(t); } return t; } Node *find_last(Node *t) { splay(t); while(t->r) { t = t->r; push(t); } return t; } Node *find_next(Node *t) { splay(t); if(!t->r) return nullptr; t = t->r; push(t); while(t->l) { t = t->l; push(t); } return t; } Node *find_prev(Node *t) { splay(t); if(!t->l) return nullptr; t = t->l; push(t); while(t->r) { t = t->r; push(t); } return t; } }; template< typename Monoid > struct EulerTourTree : SplayTree< Monoid > { using ST = SplayTree< Monoid >; using Node = typename ST::Node; using Edge = pair< Node *, Node * >; using F = typename ST::F; using S = typename ST::S; const int V; vector< map< int, Node * > > g; EulerTourTree(int V, const F &f, const S &s) : ST(f, s), V(V), g(V) {} int left_size(Node *t) { ST::splay(t); return t->l ? t->l->sz : 0; } int ord(int a, int b) { Edge p(g[a][b], g[b][a]); modify(p); return left_size(p.first); } int ord(int a) { if(g[a].empty()) return 0; return ord(a, g[a].begin()->first); } void modify(Edge &p) { int fsz = left_size(p.first); int ssz = left_size(p.second); if(fsz > ssz) swap(p.first, p.second); } void cut(int a, int b) { Edge p(g[a][b], g[b][a]); modify(p); g[a].erase(b); g[b].erase(a); auto pre = ST::find_prev(p.first); auto nxt = ST::find_next(p.second); ST::cut(p.first); ST::cut(nxt); ST::link(nxt, pre); ST::splay(p.first); ST::cut(ST::find_next(p.first)); ST::cut(p.second); } Edge get_any_edge(int a) { assert(!g[a].empty()); Edge p(g[a].begin()->second, nullptr); p.second = g[g[a].begin()->first][a]; return p; } void link(int a, int b, const Monoid &k) { Edge p(ST::make_node(k), ST::make_node(k)); if(g[a].empty() && g[b].empty()) { g[a][b] = p.first; g[b][a] = p.second; return ST::link(p.second, p.first); } if(g[b].empty()) swap(a, b); reroot(a); if(!g[a].empty()) { ST::link(ST::find_first(g[a].begin()->second), p.first); ST::link(p.second, ST::find_last(p.first)); } else { ST::link(p.second, p.first); } Edge b2{get_any_edge(b)}; modify(b2); auto nxt = ST::find_next(b2.second); auto pre = ST::find_first(b2.first); if(b2.first == pre) pre = nullptr; else ST::cut(b2.first); ST::cut(nxt); ST::link(p.first, b2.second); ST::link(nxt, ST::find_last(p.first)); ST::link(pre, ST::find_last(p.first)); g[a][b] = p.first; g[b][a] = p.second; } void reroot(int a) { if(g[a].empty()) return; Edge p{get_any_edge(a)}; modify(p); auto first = ST::find_first(p.first); if(first != p.first) { ST::cut(p.first); ST::link(first, ST::find_last(p.first)); } } }; template< typename G > struct DoublingLowestCommonAncestor { const int LOG; vector< int > dep; const G &g; vector< vector< int > > table; vector< int64 > dist; DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())), dist(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) { dist[to] = dist[idx] + to.cost; 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]; } int64 query_dist(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; } }; 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 > >; int main() { int N, Q; cin >> N; using ETT = EulerTourTree< int >; auto f = [](int a, int b) { return a; }; auto s = [](int a) { return a; }; ETT ett(N, f, s); WeightedGraph< int64 > g(N); for(int i = 1; i < N; i++) { int a, b, c; cin >> a >> b >> c; ett.link(a, b, 0); g[a].emplace_back(b, c); g[b].emplace_back(a, c); } DoublingLowestCommonAncestor< WeightedGraph< int64 > > qs(g); qs.build(); cin >> Q; for(int i = 0; i < Q; i++) { int K; cin >> K; vector< pair< int, int > > T(K); for(auto &p : T) { cin >> p.second; p.first = ett.ord(p.second); } sort(begin(T), end(T)); int64 ret = 0; for(int j = 0; j < T.size(); j++) { ret += qs.query_dist(T[j].second, T[(j + 1) % T.size()].second); } cout << ret / 2 << endl; } }