結果
問題 |
No.1418 Sum of Sum of Subtree Size
|
ユーザー |
![]() |
提出日時 | 2021-02-06 14:15:08 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 855 bytes |
コンパイル時間 | 692 ms |
コンパイル使用メモリ | 74,732 KB |
最終ジャッジ日時 | 2025-01-18 13:31:29 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 WA * 6 |
ソースコード
// 想定 WA #include <vector> #include <iostream> using namespace std; vector<int> to[100000]; int main() { int N; cin >> N; for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--, b--; to[a].push_back(b); to[b].push_back(a); } long long ret = 1LL * N * N; // オーバーフローで WA になるべき auto f = [&N](long long x) -> int { return x * (N - x); }; auto dfs = [&](auto self, int now, int prv) -> int { int subtree_size = 1; for (auto nxt : to[now]) { if (nxt != prv) { int tmp = self(self, nxt, now); ret += f(tmp); subtree_size += tmp; } } ret += f(subtree_size); return subtree_size; }; dfs(dfs, 0, -1); cout << ret << '\n'; }