結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー | sgsw |
提出日時 | 2021-08-14 23:08:30 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 57 ms / 2,000 ms |
コード長 | 2,721 bytes |
コンパイル時間 | 3,634 ms |
コンパイル使用メモリ | 253,748 KB |
実行使用メモリ | 19,200 KB |
最終ジャッジ日時 | 2024-10-06 01:03:51 |
合計ジャッジ時間 | 5,595 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 56 ms
10,240 KB |
testcase_04 | AC | 57 ms
10,240 KB |
testcase_05 | AC | 55 ms
10,112 KB |
testcase_06 | AC | 55 ms
10,240 KB |
testcase_07 | AC | 54 ms
10,112 KB |
testcase_08 | AC | 35 ms
7,936 KB |
testcase_09 | AC | 16 ms
5,632 KB |
testcase_10 | AC | 18 ms
5,760 KB |
testcase_11 | AC | 11 ms
5,248 KB |
testcase_12 | AC | 28 ms
6,912 KB |
testcase_13 | AC | 33 ms
7,552 KB |
testcase_14 | AC | 34 ms
7,808 KB |
testcase_15 | AC | 26 ms
6,784 KB |
testcase_16 | AC | 5 ms
5,248 KB |
testcase_17 | AC | 4 ms
5,248 KB |
testcase_18 | AC | 47 ms
9,472 KB |
testcase_19 | AC | 9 ms
5,248 KB |
testcase_20 | AC | 3 ms
5,248 KB |
testcase_21 | AC | 25 ms
6,656 KB |
testcase_22 | AC | 22 ms
6,144 KB |
testcase_23 | AC | 4 ms
5,248 KB |
testcase_24 | AC | 3 ms
5,248 KB |
testcase_25 | AC | 3 ms
5,248 KB |
testcase_26 | AC | 3 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 2 ms
5,248 KB |
testcase_29 | AC | 4 ms
5,248 KB |
testcase_30 | AC | 4 ms
5,248 KB |
testcase_31 | AC | 3 ms
5,248 KB |
testcase_32 | AC | 3 ms
5,248 KB |
testcase_33 | AC | 12 ms
6,912 KB |
testcase_34 | AC | 56 ms
19,200 KB |
testcase_35 | AC | 23 ms
10,112 KB |
testcase_36 | AC | 5 ms
5,248 KB |
testcase_37 | AC | 32 ms
9,988 KB |
testcase_38 | AC | 29 ms
9,432 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 |
ソースコード
#include<bits/stdc++.h> /* C++(GCC++17) Template for Programming-Contest. author : sgsw generated : 2021/08/14 when : 22:55:57 */ using namespace std; #pragma GCC optimize("Ofast") #define ll long long #define ld long double #define rep(i, a, b) for (int i = a; i < b; i++) #define rep1(i, a, b) for (int i = (b) - 1; i >= a; i--) #define endl '\n' #define vvii vector<vector<int>> #define vi vector<int> #define vvll vector<vector<ll>> #define vl vector<ll> template<class T> ostream& operator << (ostream &s, vector<T> &P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T>bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;} template <class T>bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;} template <class T = int>T gcd(T a, T b){return (b == 0) ? a : gcd(b, a % b);} template <class T = int>T lcm(T a, T b){return a / gcd(a, b) * b;} template<class T = int>T powMod(T x, T k, T m) {if (k == 0){return (T)1;}if (k % 2 == 0) {return powMod(x*x % m, k/2, m);}else{return x*powMod(x, k-1, m) % m;}} template <class T = int>T extgcd(T a,T b,T &x,T &y){T g = a;x = 1;y = 0;if (b != 0) {g = extgcd(b, a % b, y, x), y -= (a / b) * x;}return g;} template<class T = int> T invMod(T a,T m){T x,y;if (extgcd(a, m, x, y) == 1) {return (x + m) % m;}else{return -1;}} using Pii = pair<int,int>; using Pll = pair<ll,ll>; const int inf = 1e9; signed main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<vector<int>> g(n); rep(i,0,n - 1){ int u,v; cin >> u >> v; u--;v--; g[u].emplace_back(v); g[v].emplace_back(u); } vector<int> child(n); vector<int> par(n,-1); vector<int> dist(n,inf); int S = 0;dist[S] = 0; ll ans = 0; //step 1. dfs_1 function<void(int)> dfs = [&](int u){ for (int v : g[u]){ if (dist[v] == inf){ dist[v] = dist[u] + 1; par[v] = u; dfs(v); } } child[u]++; for (int v : g[u]){ if (v == par[u])continue; child[u] += child[v]; } ans += child[u]; }; dfs(S); //step 2. dfs_2 ll final_ans = 0; dist.assign(n,inf); dist[S] = 0; function<void(int)> dfs2 = [&](int u){ final_ans = (final_ans + ans); for (int v : g[u]){ if (dist[v] == inf){ dist[v] = dist[u] + 1; ans += n - 2 * child[v]; dfs2(v); } } ans -= n - 2 * child[u]; }; dfs2(S); printf("%ld\n",final_ans); return 0; }