結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー | ぷら |
提出日時 | 2021-03-05 22:39:56 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 100 ms / 2,000 ms |
コード長 | 2,836 bytes |
コンパイル時間 | 1,454 ms |
コンパイル使用メモリ | 169,272 KB |
実行使用メモリ | 18,944 KB |
最終ジャッジ日時 | 2024-10-07 03:25:54 |
合計ジャッジ時間 | 4,147 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
6,016 KB |
testcase_01 | AC | 3 ms
5,760 KB |
testcase_02 | AC | 3 ms
6,016 KB |
testcase_03 | AC | 94 ms
12,928 KB |
testcase_04 | AC | 100 ms
12,864 KB |
testcase_05 | AC | 90 ms
12,960 KB |
testcase_06 | AC | 93 ms
12,928 KB |
testcase_07 | AC | 92 ms
12,880 KB |
testcase_08 | AC | 58 ms
10,496 KB |
testcase_09 | AC | 28 ms
8,064 KB |
testcase_10 | AC | 31 ms
8,192 KB |
testcase_11 | AC | 17 ms
7,168 KB |
testcase_12 | AC | 49 ms
9,728 KB |
testcase_13 | AC | 56 ms
10,368 KB |
testcase_14 | AC | 62 ms
10,496 KB |
testcase_15 | AC | 44 ms
9,344 KB |
testcase_16 | AC | 8 ms
6,272 KB |
testcase_17 | AC | 8 ms
6,272 KB |
testcase_18 | AC | 89 ms
12,288 KB |
testcase_19 | AC | 17 ms
7,168 KB |
testcase_20 | AC | 5 ms
6,016 KB |
testcase_21 | AC | 45 ms
9,216 KB |
testcase_22 | AC | 37 ms
8,832 KB |
testcase_23 | AC | 7 ms
6,016 KB |
testcase_24 | AC | 6 ms
6,144 KB |
testcase_25 | AC | 5 ms
6,016 KB |
testcase_26 | AC | 6 ms
6,144 KB |
testcase_27 | AC | 4 ms
5,888 KB |
testcase_28 | AC | 5 ms
5,888 KB |
testcase_29 | AC | 7 ms
6,272 KB |
testcase_30 | AC | 7 ms
6,272 KB |
testcase_31 | AC | 5 ms
6,016 KB |
testcase_32 | AC | 5 ms
6,144 KB |
testcase_33 | AC | 21 ms
8,960 KB |
testcase_34 | AC | 90 ms
18,944 KB |
testcase_35 | AC | 39 ms
11,392 KB |
testcase_36 | AC | 10 ms
6,784 KB |
testcase_37 | AC | 59 ms
12,536 KB |
testcase_38 | AC | 53 ms
12,160 KB |
testcase_39 | AC | 3 ms
5,888 KB |
testcase_40 | AC | 3 ms
5,888 KB |
testcase_41 | AC | 3 ms
5,760 KB |
testcase_42 | AC | 3 ms
5,760 KB |
testcase_43 | AC | 3 ms
5,888 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pcc = pair<char,char>; using pii = pair<int,int>; using pll = pair<ll,ll>; using pdd = pair<ld,ld>; using tuplis = array<ll,3>; template<class T> using prique = priority_queue<T,vector<T>,greater<T>>; #define rep(i,n) for(ll i = 0; i < n; i++) #define rrep(i,n) for(ll i = n-1; i >= 0; i--) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define Sort(a) sort(all(a)) #define Rev(a) reverse(all(a)) #define Uniq(a) sort(all(a));a.erase(unique(all(a)),a.end()) const ll LINF = 1001001001001001001; const int INF = 1001001001; const int MOD = 1000000007; const int MODD = 998244353; const ld PI = 3.1415926535897932; const ll dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; const ll dy[] = {1, 0, -1, 0, 1, 1, -1, -1}; inline ll popcnt(ll a){ return __builtin_popcountll(a); } inline ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } inline ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll gcd(ll a,ll b){ if(a%b == 0){return b; } else{ return gcd(b,a%b); } } ll lcm(ll a,ll b){ return a/gcd(a,b)*b; } template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; } template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; } template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; } template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; } ll N,cnt[100005],used[100005],cnt2[100005],used2[100005],cnt3[100005]; vector<int>ki[100005]; ll dfs(int now) { ll sum = 0; used[now] = true; rep(i,ki[now].size()) { if(used[ki[now][i]]) { continue; } sum += dfs(ki[now][i]); } return cnt[now] = sum+1; } ll dfs2(int now) { ll sum = 0; used2[now] = true; rep(i,ki[now].size()) { if(used2[ki[now][i]]) { continue; } sum += dfs2(ki[now][i]); } return cnt2[now] = sum+cnt[now]; } ll ans = 0; void dfs3(int now) { ll sum = 0; rep(i,ki[now].size()) { if(cnt[ki[now][i]] > cnt[now]) { sum += cnt3[ki[now][i]]-cnt2[now]-cnt[now]; } else { sum += cnt2[ki[now][i]]; } } cnt3[now] = sum+N; ans += cnt3[now]; rep(i,ki[now].size()) { if(cnt[ki[now][i]] > cnt[now]) { continue; } else { dfs3(ki[now][i]); } } } int main() { cin >> N; rep(i,N-1) { int A,B; cin >> A >> B; A--;B--; ki[A].push_back(B); ki[B].push_back(A); } dfs(0); dfs2(0); dfs3(0); cout << ans << endl; }