結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー | stoq |
提出日時 | 2021-03-06 13:24:43 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 113 ms / 2,000 ms |
コード長 | 4,731 bytes |
コンパイル時間 | 2,275 ms |
コンパイル使用メモリ | 220,480 KB |
実行使用メモリ | 51,200 KB |
最終ジャッジ日時 | 2024-10-08 10:52:52 |
合計ジャッジ時間 | 5,665 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 113 ms
15,872 KB |
testcase_04 | AC | 79 ms
15,872 KB |
testcase_05 | AC | 80 ms
16,000 KB |
testcase_06 | AC | 84 ms
15,872 KB |
testcase_07 | AC | 80 ms
15,744 KB |
testcase_08 | AC | 51 ms
11,776 KB |
testcase_09 | AC | 24 ms
7,168 KB |
testcase_10 | AC | 26 ms
7,680 KB |
testcase_11 | AC | 14 ms
5,504 KB |
testcase_12 | AC | 41 ms
10,112 KB |
testcase_13 | AC | 47 ms
11,264 KB |
testcase_14 | AC | 48 ms
11,648 KB |
testcase_15 | AC | 37 ms
9,344 KB |
testcase_16 | AC | 6 ms
5,248 KB |
testcase_17 | AC | 5 ms
5,248 KB |
testcase_18 | AC | 78 ms
14,720 KB |
testcase_19 | AC | 14 ms
5,504 KB |
testcase_20 | AC | 4 ms
5,248 KB |
testcase_21 | AC | 37 ms
9,216 KB |
testcase_22 | AC | 32 ms
8,576 KB |
testcase_23 | AC | 4 ms
5,248 KB |
testcase_24 | AC | 4 ms
5,248 KB |
testcase_25 | AC | 3 ms
5,248 KB |
testcase_26 | AC | 4 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 2 ms
5,248 KB |
testcase_29 | AC | 5 ms
5,248 KB |
testcase_30 | AC | 5 ms
5,248 KB |
testcase_31 | AC | 3 ms
5,248 KB |
testcase_32 | AC | 3 ms
5,248 KB |
testcase_33 | AC | 23 ms
13,952 KB |
testcase_34 | AC | 110 ms
51,200 KB |
testcase_35 | AC | 43 ms
23,424 KB |
testcase_36 | AC | 9 ms
5,248 KB |
testcase_37 | AC | 67 ms
16,792 KB |
testcase_38 | AC | 60 ms
15,680 KB |
testcase_39 | AC | 2 ms
5,248 KB |
testcase_40 | AC | 2 ms
5,248 KB |
testcase_41 | AC | 2 ms
5,248 KB |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
ソースコード
#define MOD_TYPE 1 #pragma region Macros #include <bits/stdc++.h> using namespace std; #if 0 #include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using Int = boost::multiprecision::cpp_int; using lld = boost::multiprecision::cpp_dec_float_100; #endif #if 1 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using pld = pair<ld, ld>; template <typename Q_type> using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>; constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353); constexpr int INF = (int)1e9 + 10; constexpr ll LINF = (ll)4e18; constexpr double PI = acos(-1.0); constexpr double EPS = 1e-7; constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0}; #define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i) #define rep(i, n) REP(i, 0, n) #define REPI(i, m, n) for (int i = m; i < (int)(n); ++i) #define repi(i, n) REPI(i, 0, n) #define MP make_pair #define MT make_tuple #define YES(n) cout << ((n) ? "YES" : "NO") << "\n" #define Yes(n) cout << ((n) ? "Yes" : "No") << "\n" #define possible(n) cout << ((n) ? "possible" : "impossible") << "\n" #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n" #define all(v) v.begin(), v.end() #define NP(v) next_permutation(all(v)) #define dbg(x) cerr << #x << ":" << x << "\n"; struct io_init { io_init() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(30) << setiosflags(ios::fixed); }; } io_init; template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } template <typename A, size_t N, typename T> inline void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template <typename T, typename U> constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept { is >> p.first >> p.second; return is; } template <typename T, typename U> constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept { os << p.first << " " << p.second; return os; } #pragma endregion template <typename T> struct ReRooting { using F = function<T(T, int)>; using F2 = function<T(T, T)>; int V; vector<vector<int>> G; vector<vector<T>> dp; // dp_v = g(merge(f(dp_c1,c1), f(dp_c2,c2), ..., f(dp_ck,ck)), v) F f, g; F2 merge; T mi; // identity of merge ReRooting() {} ReRooting(int V, F f, F2 merge, T mi, F g) : V(V), f(f), merge(merge), mi(mi), g(g) { G.resize(V); dp.resize(V); } void read_graph(int index = 1) { int a, b; for (int i = 0; i < V - 1; ++i) { cin >> a >> b; a -= index, b -= index; G[a].emplace_back(b); G[b].emplace_back(a); } } void add_edge(int a, int b) { G[a].emplace_back(b); G[b].emplace_back(a); } T dfs1(int p, int v) { T res = mi; for (int i = 0; i < G[v].size(); ++i) { if (G[v][i] == p) continue; dp[v][i] = dfs1(v, G[v][i]); res = merge(res, f(dp[v][i], G[v][i])); } return g(res, v); } void dfs2(int p, int v, T from_par) { for (int i = 0; i < G[v].size(); ++i) { if (G[v][i] == p) { dp[v][i] = from_par; break; } } vector<T> pR(G[v].size() + 1); pR[G[v].size()] = mi; for (int i = G[v].size(); i > 0; --i) pR[i - 1] = merge(pR[i], f(dp[v][i - 1], G[v][i - 1])); T pL = mi; for (int i = 0; i < G[v].size(); ++i) { if (G[v][i] != p) { T val = merge(pL, pR[i + 1]); dfs2(v, G[v][i], g(val, v)); } pL = merge(pL, f(dp[v][i], G[v][i])); } } void calc(int root = 0) { for (int i = 0; i < V; ++i) dp[i].resize(G[i].size()); dfs1(-1, root); dfs2(-1, root, mi); } T solve(int v) { T ans = mi; for (int i = 0; i < G[v].size(); ++i) ans = merge(ans, f(dp[v][i], G[v][i])); return g(ans, v); } }; void solve() { int n; cin >> n; auto f = [](pll p, int temp) { return p; }; auto merge = [](pll p, pll q) { return pll{p.first + q.first, p.second + q.second}; }; auto g = [](pll p, int temp) { return pll{p.first + 1, p.second + p.first + 1}; }; ReRooting<pll> re(n, f, merge, pll{0, 0}, g); re.read_graph(); re.calc(); ll sum = 0; rep(i, n) sum += re.solve(i).second; cout << sum << "\n"; } int main() { solve(); }