結果
問題 | No.399 動的な領主 |
ユーザー | yamate11 |
提出日時 | 2021-09-06 19:37:13 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 127 ms / 2,000 ms |
コード長 | 8,740 bytes |
コンパイル時間 | 2,337 ms |
コンパイル使用メモリ | 210,640 KB |
実行使用メモリ | 24,320 KB |
最終ジャッジ日時 | 2024-06-02 02:45:27 |
合計ジャッジ時間 | 5,214 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 3 ms
5,376 KB |
testcase_05 | AC | 9 ms
5,376 KB |
testcase_06 | AC | 127 ms
13,904 KB |
testcase_07 | AC | 122 ms
13,824 KB |
testcase_08 | AC | 114 ms
14,208 KB |
testcase_09 | AC | 115 ms
14,080 KB |
testcase_10 | AC | 3 ms
5,376 KB |
testcase_11 | AC | 8 ms
5,376 KB |
testcase_12 | AC | 81 ms
14,464 KB |
testcase_13 | AC | 78 ms
14,336 KB |
testcase_14 | AC | 62 ms
24,320 KB |
testcase_15 | AC | 64 ms
24,192 KB |
testcase_16 | AC | 62 ms
19,536 KB |
testcase_17 | AC | 119 ms
14,080 KB |
testcase_18 | AC | 117 ms
14,080 KB |
ソースコード
#include <bits/stdc++.h> #include <cassert> typedef long long int ll; using namespace std; // #include <atcoder/all> // using namespace atcoder; // @@ !! LIM(debug) // ---- inserted function f:<< from util.cc 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, typename T3> ostream& operator<< (ostream& os, const tuple<T1,T2,T3>& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")"; return os; } template <typename T1, typename T2, typename T3, typename T4> ostream& operator<< (ostream& os, const tuple<T1,T2,T3,T4>& t) { os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", " << get<3>(t) << ")"; return os; } template <typename T> ostream& operator<< (ostream& os, const vector<T>& v) { os << '['; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << ']'; return os; } template <typename T, typename C> ostream& operator<< (ostream& os, const set<T, C>& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template <typename T, typename C> ostream& operator<< (ostream& os, const unordered_set<T, C>& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template <typename T, typename C> ostream& operator<< (ostream& os, const multiset<T, C>& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template <typename T1, typename T2, typename C> ostream& operator<< (ostream& os, const map<T1, T2, C>& mp) { os << '['; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) os << ", "; os << it->first << ": " << it->second; } os << ']'; return os; } template <typename T1, typename T2, typename C> ostream& operator<< (ostream& os, const unordered_map<T1, T2, C>& mp) { os << '['; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) os << ", "; os << it->first << ": " << it->second; } os << ']'; return os; } template <typename T, typename T2> ostream& operator<< (ostream& os, const queue<T, T2>& orig) { queue<T, T2> que(orig); bool first = true; os << '['; while (!que.empty()) { T x = que.front(); que.pop(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template <typename T, typename T2> ostream& operator<< (ostream& os, const deque<T, T2>& orig) { deque<T, T2> que(orig); bool first = true; os << '['; while (!que.empty()) { T x = que.front(); que.pop_front(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template <typename T, typename T2, typename T3> ostream& operator<< (ostream& os, const priority_queue<T, T2, T3>& orig) { priority_queue<T, T2, T3> pq(orig); bool first = true; os << '['; while (!pq.empty()) { T x = pq.top(); pq.pop(); if (!first) os << ", "; os << x; first = false; } return os << ']'; } template <typename T> ostream& operator<< (ostream& os, const stack<T>& st) { stack<T> tmp(st); os << '['; bool first = true; while (!tmp.empty()) { T& t = tmp.top(); if (first) first = false; else os << ", "; os << t; tmp.pop(); } os << ']'; return os; } #if __cplusplus >= 201703L template <typename T> ostream& operator<< (ostream& os, const optional<T>& t) { if (t.has_value()) os << "v(" << t.value() << ")"; else os << "nullopt"; return os; } #endif ostream& operator<< (ostream& os, int8_t x) { os << (int32_t)x; return os; } // ---- end f:<< // ---- inserted library file debug.cc template <class... Args> string dbgFormat(const char* fmt, Args... args) { size_t len = snprintf(nullptr, 0, fmt, args...); char buf[len + 1]; snprintf(buf, len + 1, fmt, args...); return string(buf); } template <class Head> void dbgLog(bool with_nl, Head&& head) { cerr << head; if (with_nl) cerr << endl; } template <class Head, class... Tail> void dbgLog(bool with_nl, Head&& head, Tail&&... tail) { cerr << head << " "; dbgLog(with_nl, forward<Tail>(tail)...); } #if DEBUG #define DLOG(...) dbgLog(true, __VA_ARGS__) #define DLOGNNL(...) dbgLog(false, __VA_ARGS__) #define DFMT(...) cerr << dbgFormat(__VA_ARGS__) << endl #define DCALL(func, ...) func(__VA_ARGS__) #else #define DLOG(...) #define DLOGNNL(...) #define DFMT(...) #define DCALL(func, ...) #endif /* #if DEBUG_LIB #define DLOG_LIB(...) dbgLog(true, __VA_ARGS__) #define DLOGNNL_LIB(...) dbgLog(false, __VA_ARGS__) #define DFMT_LIB(...) cerr << dbgFormat(__VA_ARGS__) << endl #define DCALL_LIB(func, ...) func(__VA_ARGS__) #else #define DLOG_LIB(...) #define DFMT_LIB(...) #define DCALL_LIB(func, ...) #endif */ #define DUP1(E1) #E1 "=", E1 #define DUP2(E1,E2) DUP1(E1), DUP1(E2) #define DUP3(E1,...) DUP1(E1), DUP2(__VA_ARGS__) #define DUP4(E1,...) DUP1(E1), DUP3(__VA_ARGS__) #define DUP5(E1,...) DUP1(E1), DUP4(__VA_ARGS__) #define DUP6(E1,...) DUP1(E1), DUP5(__VA_ARGS__) #define DUP7(E1,...) DUP1(E1), DUP6(__VA_ARGS__) #define DUP8(E1,...) DUP1(E1), DUP7(__VA_ARGS__) #define DUP9(E1,...) DUP1(E1), DUP8(__VA_ARGS__) #define DUP10(E1,...) DUP1(E1), DUP9(__VA_ARGS__) #define DUP11(E1,...) DUP1(E1), DUP10(__VA_ARGS__) #define DUP12(E1,...) DUP1(E1), DUP11(__VA_ARGS__) #define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,NAME,...) NAME #define DUP(...) GET_MACRO(__VA_ARGS__, DUP12, DUP11, DUP10, DUP9, DUP8, DUP7, DUP6, DUP5, DUP4, DUP3, DUP2, DUP1)(__VA_ARGS__) #define DLOGK(...) DLOG(DUP(__VA_ARGS__)) #define DLOGKL(lab, ...) DLOG(lab, DUP(__VA_ARGS__)) #if DEBUG_LIB #define DLOG_LIB DLOG #define DLOGK_LIB DLOGK #define DLOGKL_LIB DLOGKL #endif // ---- end debug.cc // @@ !! LIM -- end mark -- int main(/* int argc, char *argv[] */) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(20); ll N; cin >> N; vector<vector<ll>> nbr(N); for (ll i = 0; i < N-1; i++) { ll u, v; cin >> u >> v; u--; v--; nbr[u].push_back(v); nbr[v].push_back(u); } vector<ll> parent(N); auto dfs1 = [&](auto rF, ll nd, ll pt) -> ll { parent[nd] = pt; ll ret = 1; ll smax = 0; auto& vec = nbr[nd]; if (nd == 0) vec.push_back(-1); DLOGK(nd, vec); ll sz = vec.size(); for (ll i = 0; i < sz - 1; i++) { ll c = vec[i]; if (c == pt) { swap(vec[i], vec[sz - 1]); c = vec[i]; } ll s = rF(rF, c, nd); ret += s; if (s > smax) { smax = s; swap(vec[i], vec[0]); } } vec.resize(sz - 1); return ret; }; dfs1(dfs1, 0, -1); DLOGK(parent); DLOGK(nbr); vector<ll> o2d(N), d2o(N), head(N), lev(N); { ll ser = 0; auto dfs2 = [&](auto rF, ll nd, ll hd, ll lv) -> void { DLOGKL(" dfs2", nd, hd, lv); o2d[nd] = ser; d2o[ser] = nd; head[ser] = hd; lev[ser] = lv; ser++; auto& vec = nbr[nd]; for (ll i = 0; i < (ll)vec.size(); i++) { if (i == 0) rF(rF, vec[i], hd, lv); else rF(rF, vec[i], ser, lv + 1); } }; dfs2(dfs2, 0, 0, 0); } DLOGK(o2d); DLOGK(d2o); DLOGK(head); DLOGK(lev); auto parentD = [&](ll t) -> ll { return t == 0 ? -1 : o2d[parent[d2o[t]]]; }; auto lcaD = [&](ll t1, ll t2) -> ll { auto step = [&](ll& t, ll& s) -> void { t = parentD(s); s = head[t]; }; ll s1 = head[t1]; ll s2 = head[t2]; while (lev[s1] > lev[s2]) step(t1, s1); while (lev[s1] < lev[s2]) step(t2, s2); while (s1 != s2) { step(t1, s1); step(t2, s2); } return min(t1, t2); }; // auto lca = [&](ll x, ll y) -> ll { return d2o[lcaD(o2d[x], o2d[y])]; }; vector<ll> diff(N + 1); auto func = [&](ll s, ll t, ll x) -> void { while (true) { ll h = head[s]; if (lev[t] >= lev[h]) break; diff[h] += x; diff[s + 1] -= x; s = parentD(h); } diff[t] += x; diff[s + 1] -= x; }; ll Q; cin >> Q; for (; Q > 0; Q--) { ll a, b; cin >> a >> b; a--; b--; ll ta = o2d[a], tb = o2d[b]; ll tc = lcaD(ta, tb); func(ta, tc, 1); func(tb, tc, 1); func(tc, tc, -1); } ll ans = 0; ll s = 0; for (ll i = 0; i < N; i++) { s += diff[i]; ans += s * (s + 1) / 2; } cout << ans << endl; return 0; }