結果
問題 | No.1418 Sum of Sum of Subtree Size |
ユーザー |
|
提出日時 | 2022-10-15 03:33:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 101 ms / 2,000 ms |
コード長 | 897 bytes |
コンパイル時間 | 2,143 ms |
コンパイル使用メモリ | 200,736 KB |
最終ジャッジ日時 | 2025-02-08 06:04:44 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for(int i = 0; i < (n); i++)using namespace std;typedef long long ll;int main(){cin.tie(0);ios::sync_with_stdio(0);int N; cin >> N;vector<vector<int>> G(N);rep(_,N-1) {int a,b; cin >> a >> b; a--; b--;G[a].push_back(b);G[b].push_back(a);}vector<int> sz(N, 1);function<void(int,int)> get_sz = [&](int v, int p) -> void {for(int to : G[v]) {if(to != p) {get_sz(to, v);sz[v] += sz[to];}}}; get_sz(0, -1);ll ans = 0;function<void(int,int)> dfs = [&](int v, int p) -> void {for(int to : G[v]) {if(to != p) {ans += 2LL * sz[to] * (N - sz[to]);dfs(to, v);}}}; dfs(0, -1);cout << ans + 1LL * N * N << endl;}