結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー | shimarut |
提出日時 | 2021-03-13 17:30:43 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 108 ms / 2,000 ms |
コード長 | 3,423 bytes |
コンパイル時間 | 1,378 ms |
コンパイル使用メモリ | 127,272 KB |
実行使用メモリ | 14,464 KB |
最終ジャッジ日時 | 2024-10-15 08:08:34 |
合計ジャッジ時間 | 4,345 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
6,784 KB |
testcase_01 | AC | 5 ms
6,784 KB |
testcase_02 | AC | 4 ms
6,784 KB |
testcase_03 | AC | 101 ms
9,856 KB |
testcase_04 | AC | 100 ms
9,856 KB |
testcase_05 | AC | 103 ms
9,984 KB |
testcase_06 | AC | 102 ms
9,984 KB |
testcase_07 | AC | 108 ms
9,984 KB |
testcase_08 | AC | 66 ms
8,832 KB |
testcase_09 | AC | 31 ms
7,808 KB |
testcase_10 | AC | 34 ms
7,808 KB |
testcase_11 | AC | 20 ms
7,424 KB |
testcase_12 | AC | 52 ms
8,448 KB |
testcase_13 | AC | 62 ms
8,704 KB |
testcase_14 | AC | 65 ms
8,832 KB |
testcase_15 | AC | 49 ms
8,320 KB |
testcase_16 | AC | 9 ms
6,912 KB |
testcase_17 | AC | 9 ms
6,912 KB |
testcase_18 | AC | 94 ms
9,600 KB |
testcase_19 | AC | 18 ms
7,296 KB |
testcase_20 | AC | 6 ms
6,784 KB |
testcase_21 | AC | 46 ms
8,320 KB |
testcase_22 | AC | 39 ms
8,064 KB |
testcase_23 | AC | 8 ms
6,912 KB |
testcase_24 | AC | 7 ms
7,040 KB |
testcase_25 | AC | 6 ms
6,784 KB |
testcase_26 | AC | 7 ms
6,912 KB |
testcase_27 | AC | 5 ms
6,784 KB |
testcase_28 | AC | 5 ms
6,784 KB |
testcase_29 | AC | 8 ms
6,912 KB |
testcase_30 | AC | 7 ms
6,816 KB |
testcase_31 | AC | 6 ms
6,784 KB |
testcase_32 | AC | 6 ms
6,784 KB |
testcase_33 | AC | 22 ms
8,448 KB |
testcase_34 | AC | 105 ms
14,464 KB |
testcase_35 | AC | 41 ms
9,984 KB |
testcase_36 | AC | 11 ms
7,168 KB |
testcase_37 | AC | 64 ms
10,000 KB |
testcase_38 | AC | 58 ms
9,744 KB |
testcase_39 | AC | 5 ms
6,784 KB |
testcase_40 | AC | 5 ms
6,784 KB |
testcase_41 | AC | 5 ms
6,784 KB |
testcase_42 | AC | 5 ms
6,784 KB |
testcase_43 | AC | 5 ms
6,784 KB |
ソースコード
#pragma region #define _USE_MATH_DEFINES #include <iostream> #include <string> #include <algorithm> #include <cmath> #include <cstdlib> #include <vector> #include <map> #include <queue> #include <stack> #include <set> #include <list> #include <iomanip> #include <cstdint> #include <bitset> #include <sstream> #include <regex> #include <fstream> #include <array> //#include <atcoder/all> // using namespace std; //using namespace atcoder; //using mint = modint1000000007; ////using mint = modint998244353; ////using mint = modint; typedef long long ll; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using pint = pair<int, int>; using pll = pair<ll, ll>; //#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i)) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #pragma region UnionFind struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; #pragma endregion #pragma region GCD ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a % b); } #pragma endregion #pragma region LCM ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #pragma endregion #pragma region chmin template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } #pragma endregion #pragma region chmax template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } #pragma endregion #pragma region グリッド内チェック bool out(int x, int y, int h, int w) { if (x < 0 || h <= x || y < 0 || w <= y)return true; else return false; } #pragma endregion #pragma region Dijkstra vl dijkstra(vector<vector<pair<int, ll>>> v, int s) { ll INF = 1e18; int MAX = 1e6; vl res(MAX, INF); priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q; q.push({ 0,s }); while (!q.empty()) { int now; ll d; tie(d, now) = q.top(); q.pop(); if (!chmin(res[now], d))continue; for (auto p : v[now]) { int next; ll c; tie(next, c) = p; if (res[next] <= res[now] + c)continue; q.push({ res[now] + c,next }); } } return res; } #pragma endregion #pragma endregion vvi v(100100); vi depth(100100); vl dp(100100); void dfs(int now, int p, int d) { depth[now] = d; for (int next : v[now]) { if (next == p)continue; dfs(next, now, d + 1); } } ll dfs2(int now, int p) { if (dp[now])return dp[now]; dp[now] = 1; for (int next : v[now]) { if (next == p)continue; dp[now] += dfs2(next, now); } return dp[now]; } int main() { int n; cin >> n; rep(i, n - 1) { int a, b; cin >> a >> b; --a, --b; v[a].push_back(b); v[b].push_back(a); } dfs(0, -1, 0); dfs2(0, -1); ll res = 0; rep(i, n) { res += n; for (int x : v[i]) { if (depth[x] > depth[i]) { res += dp[x] * (n - dp[x]); } else { res += dp[i] * (n - dp[i]); } } } cout << res << endl; }