結果
問題 | No.1103 Directed Length Sum |
ユーザー | keijak |
提出日時 | 2020-07-03 22:23:50 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 710 ms / 3,000 ms |
コード長 | 2,802 bytes |
コンパイル時間 | 1,659 ms |
コンパイル使用メモリ | 176,536 KB |
実行使用メモリ | 107,472 KB |
最終ジャッジ日時 | 2023-10-17 04:32:23 |
合計ジャッジ時間 | 8,667 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 319 ms
107,472 KB |
testcase_03 | AC | 179 ms
38,588 KB |
testcase_04 | AC | 362 ms
29,804 KB |
testcase_05 | AC | 710 ms
49,392 KB |
testcase_06 | AC | 203 ms
20,700 KB |
testcase_07 | AC | 32 ms
7,440 KB |
testcase_08 | AC | 54 ms
9,552 KB |
testcase_09 | AC | 21 ms
5,856 KB |
testcase_10 | AC | 77 ms
11,860 KB |
testcase_11 | AC | 403 ms
32,376 KB |
testcase_12 | AC | 225 ms
20,700 KB |
testcase_13 | AC | 82 ms
12,124 KB |
testcase_14 | AC | 16 ms
5,328 KB |
testcase_15 | AC | 140 ms
16,808 KB |
testcase_16 | AC | 461 ms
36,004 KB |
testcase_17 | AC | 487 ms
37,260 KB |
testcase_18 | AC | 76 ms
11,860 KB |
testcase_19 | AC | 414 ms
33,168 KB |
testcase_20 | AC | 24 ms
6,384 KB |
testcase_21 | AC | 47 ms
9,024 KB |
testcase_22 | AC | 310 ms
27,692 KB |
testcase_23 | AC | 148 ms
17,864 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using i64 = long long; using u64 = unsigned long long; #define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i) #ifdef ENABLE_DEBUG template <typename T> void debug(T value) { cerr << value; } template <typename T, typename... Ts> void debug(T value, Ts... args) { cerr << value << ", "; debug(args...); } #define DEBUG(...) \ do { \ cerr << " \033[33m (L" << __LINE__ << ") "; \ cerr << #__VA_ARGS__ << ":\033[0m "; \ debug(__VA_ARGS__); \ cerr << endl; \ } while (0) #else #define debug(...) #define DEBUG(...) #endif const i64 MOD = 1'000'000'007; struct mint { long long x; mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint& operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream& operator>>(istream& is, mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> child(N); vector<int> parent(N, -1); REP(i, N - 1) { int a, b; cin >> a >> b; a--; b--; child[a].push_back(b); assert(parent[b] == -1); parent[b] = a; } int root = -1; REP(i, N) { if (parent[i] == -1) { assert(root == -1); root = i; } } vector<int> subsize(N); auto calc_subsize = [&](auto rec, int v) -> int { int sz = 1; for (int u : child[v]) { sz += rec(rec, u); } subsize[v] = sz; return sz; }; calc_subsize(calc_subsize, root); REP(i, N) { DEBUG(i, subsize[i]); } mint ans = 0; auto calc_psum = [&](auto rec, int v, mint plen) -> void { for (int u : child[v]) { mint x = plen * subsize[u]; ans += x; DEBUG(v, u, plen, subsize[u], x, ans); rec(rec, u, plen + 1); } }; calc_psum(calc_psum, root, 1); cout << ans << '\n'; }