結果
| 問題 |
No.1418 Sum of Sum of Subtree Size
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-05 21:55:06 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 48 ms / 2,000 ms |
| コード長 | 1,767 bytes |
| コンパイル時間 | 1,402 ms |
| コンパイル使用メモリ | 137,316 KB |
| 最終ジャッジ日時 | 2025-01-19 10:56:25 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<vector<int>> g(N);
for(int i = 1; i < N; ++i){
int A, B;
cin >> A >> B;
--A, --B;
g[A].emplace_back(B);
g[B].emplace_back(A);
}
ll ans = (ll)N * N;
vector<int> subtree_size(N, 1);
auto dfs = [&](auto&& self, int from = 0, int par = -1) -> int {
for(int to : g[from]){
if(to != par){
subtree_size[from] += self(self, to, from);
}
}
ans += (ll)subtree_size[from] * (N - subtree_size[from]);
for(int to : g[from]){
if(to != par){
ans += (ll)(N - subtree_size[to]) * subtree_size[to];
}
}
return subtree_size[from];
};
dfs(dfs);
cout << ans << endl;
return 0;
}