結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー |
![]() |
提出日時 | 2023-10-16 12:45:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 65 ms / 2,000 ms |
コード長 | 1,045 bytes |
コンパイル時間 | 1,284 ms |
コンパイル使用メモリ | 123,164 KB |
最終ジャッジ日時 | 2025-02-17 08:02:44 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
#include<iostream> #include<vector> #include<algorithm> #include<cstring> #include<cassert> #include<cmath> #include<ctime> #include<iomanip> #include<numeric> #include<stack> #include<queue> #include<map> #include<unordered_map> #include<set> #include<unordered_set> #include<bitset> #include<random> using namespace std; int N,ch[1 << 17]; vector<int> G[1 << 17]; long long dp[1 << 17]; void dfs0(int u,int p) { ch[u] = 1; for(int v:G[u]) if(v != p) { dfs0(v,u); ch[u] += ch[v]; dp[u] += dp[v]; } dp[u] += ch[u]; } void dfs1(int u,int p) { if(p >= 0) { long long pdp = dp[p]; pdp -= dp[u]+ch[u]; dp[u] += pdp+N-ch[u]; } for(int v:G[u]) if(v != p) dfs1(v,u); } void solve() { cin >> N; for(int i = 1;i < N;i++) { int u,v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } dfs0(0,-1); dfs1(0,-1); long long ans = 0; for(int i = 0;i < N;i++) ans += dp[i]; cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; //cin >> tt; while(tt--) solve(); }