結果
問題 |
No.1418 Sum of Sum of Subtree Size
|
ユーザー |
![]() |
提出日時 | 2021-02-06 12:49:18 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
CE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,747 bytes |
コンパイル時間 | 49 ms |
コンパイル使用メモリ | 24,960 KB |
最終ジャッジ日時 | 2025-01-18 13:29:47 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
main.cpp:2:10: fatal error: testlib.h: No such file or directory 2 | #include "testlib.h" | ^~~~~~~~~~~ compilation terminated.
ソースコード
// VALIDATOR #include "testlib.h" #include <cassert> #include <numeric> #include <vector> using namespace std; // UnionFind Tree (0-indexed), based on size of each disjoint set struct UnionFind { std::vector<int> par, cou; UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); } int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return false; if (cou[x] < cou[y]) std::swap(x, y); par[y] = x, cou[x] += cou[y]; return true; } int count(int x) { return cou[find(x)]; } bool same(int x, int y) { return find(x) == find(y); } }; vector<int> to[100000]; int main(int argc, char **argv) { registerValidation(argc, argv); int N = inf.readInt(2, 100000); inf.readEoln(); UnionFind uf(N); for (int i = 0; i < N - 1; i++) { int a = inf.readInt(1, N); inf.readSpace(); int b = inf.readInt(1, N); inf.readEoln(); a--, b--; uf.unite(a, b); to[a].push_back(b); to[b].push_back(a); } inf.readEof(); if (uf.count(0) != N) { // NOT tree exit(8); } long long ret = 1LL * N * N; auto f = [&N](long long x) -> long long { 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'; }