結果
問題 | No.1103 Directed Length Sum |
ユーザー | m_tsubasa |
提出日時 | 2020-07-22 14:38:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 906 ms / 3,000 ms |
コード長 | 2,639 bytes |
コンパイル時間 | 1,928 ms |
コンパイル使用メモリ | 207,380 KB |
実行使用メモリ | 159,616 KB |
最終ジャッジ日時 | 2024-06-11 19:42:27 |
合計ジャッジ時間 | 13,083 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 697 ms
159,616 KB |
testcase_03 | AC | 430 ms
38,756 KB |
testcase_04 | AC | 453 ms
29,912 KB |
testcase_05 | AC | 906 ms
49,420 KB |
testcase_06 | AC | 285 ms
20,588 KB |
testcase_07 | AC | 58 ms
7,424 KB |
testcase_08 | AC | 88 ms
9,472 KB |
testcase_09 | AC | 37 ms
5,888 KB |
testcase_10 | AC | 131 ms
11,912 KB |
testcase_11 | AC | 542 ms
32,356 KB |
testcase_12 | AC | 302 ms
20,736 KB |
testcase_13 | AC | 138 ms
12,160 KB |
testcase_14 | AC | 27 ms
5,376 KB |
testcase_15 | AC | 218 ms
16,952 KB |
testcase_16 | AC | 640 ms
35,932 KB |
testcase_17 | AC | 669 ms
37,376 KB |
testcase_18 | AC | 131 ms
11,900 KB |
testcase_19 | AC | 567 ms
33,028 KB |
testcase_20 | AC | 43 ms
6,400 KB |
testcase_21 | AC | 79 ms
8,960 KB |
testcase_22 | AC | 411 ms
27,648 KB |
testcase_23 | AC | 227 ms
17,792 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; template <int mod = (int)(1e9 + 7)> struct ModInt { int x; constexpr ModInt() : x(0) {} constexpr ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} constexpr ModInt &operator+=(const ModInt &p) noexcept { if ((x += p.x) >= mod) x -= mod; return *this; } constexpr ModInt &operator-=(const ModInt &p) noexcept { if ((x += mod - p.x) >= mod) x -= mod; return *this; } constexpr ModInt &operator*=(const ModInt &p) noexcept { x = (int)(1LL * x * p.x % mod); return *this; } constexpr ModInt &operator/=(const ModInt &p) noexcept { *this *= p.inverse(); return *this; } constexpr ModInt operator-() const { return ModInt(-x); } constexpr ModInt operator+(const ModInt &p) const noexcept { return ModInt(*this) += p; } constexpr ModInt operator-(const ModInt &p) const noexcept { return ModInt(*this) -= p; } constexpr ModInt operator*(const ModInt &p) const noexcept { return ModInt(*this) *= p; } constexpr ModInt operator/(const ModInt &p) const noexcept { return ModInt(*this) /= p; } constexpr bool operator==(const ModInt &p) const noexcept { return x == p.x; } constexpr bool operator!=(const ModInt &p) const noexcept { return x != p.x; } constexpr ModInt inverse() const noexcept { int a = x, b = mod, u = 1, v = 0, t = 0; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } constexpr ModInt pow(int64_t n) const { ModInt res(1), mul(x); while (n) { if (n & 1) res *= mul; mul *= mul; n >>= 1; } return res; } friend constexpr ostream &operator<<(ostream &os, const ModInt &p) noexcept { return os << p.x; } friend constexpr istream &operator>>(istream &is, ModInt &a) noexcept { int64_t t = 0; is >> t; a = ModInt<mod>(t); return (is); } constexpr int get_mod() { return mod; } }; int n, root = -1; vector<int> cnt, dp; vector<vector<int>> g; ModInt<> solve(int now, int par, int dist); int main() { cin >> n; g.resize(n); dp.assign(n, 1); cnt.assign(n, 0); for (int i = 1; i < n; ++i) { int a, b; cin >> a >> b; g[--a].push_back(--b); ++cnt[b]; } for (int i = 0; i < n; ++i) if (cnt[i] == 0) root = i; cout << solve(root, -1, 0) << endl; return 0; } ModInt<> solve(int now, int par, int dist) { ModInt<> res = 0; for (auto to : g[now]) if (to != par) { res += solve(to, now, dist + 1); dp[now] += dp[to]; } res += ModInt<>(dp[now]) * dist; return res; }