結果
問題 | No.1103 Directed Length Sum |
ユーザー | KoD |
提出日時 | 2020-07-03 21:25:19 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,080 ms / 3,000 ms |
コード長 | 5,209 bytes |
コンパイル時間 | 1,044 ms |
コンパイル使用メモリ | 84,764 KB |
実行使用メモリ | 135,392 KB |
最終ジャッジ日時 | 2024-09-16 21:36:06 |
合計ジャッジ時間 | 11,419 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 763 ms
135,392 KB |
testcase_03 | AC | 495 ms
42,376 KB |
testcase_04 | AC | 595 ms
32,112 KB |
testcase_05 | AC | 1,080 ms
53,308 KB |
testcase_06 | AC | 372 ms
22,072 KB |
testcase_07 | AC | 73 ms
7,808 KB |
testcase_08 | AC | 116 ms
10,108 KB |
testcase_09 | AC | 43 ms
6,940 KB |
testcase_10 | AC | 163 ms
12,628 KB |
testcase_11 | AC | 659 ms
34,752 KB |
testcase_12 | AC | 377 ms
22,184 KB |
testcase_13 | AC | 170 ms
12,976 KB |
testcase_14 | AC | 32 ms
6,944 KB |
testcase_15 | AC | 286 ms
18,132 KB |
testcase_16 | AC | 743 ms
38,708 KB |
testcase_17 | AC | 776 ms
40,184 KB |
testcase_18 | AC | 182 ms
12,636 KB |
testcase_19 | AC | 686 ms
35,556 KB |
testcase_20 | AC | 54 ms
6,940 KB |
testcase_21 | AC | 106 ms
9,600 KB |
testcase_22 | AC | 540 ms
29,696 KB |
testcase_23 | AC | 292 ms
18,984 KB |
ソースコード
#include <iostream> #include <algorithm> #include <utility> #include <numeric> #include <vector> #include <array> template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return true; } return false; } struct range { using itr = int64_t; struct iterator { itr i; constexpr iterator(itr i_) noexcept : i(i_) { } constexpr void operator ++ () noexcept { ++i; } constexpr itr operator * () const noexcept { return i; } constexpr bool operator != (iterator x) const noexcept { return i != x.i; } }; const iterator l, r; constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { } constexpr iterator begin() const noexcept { return l; } constexpr iterator end() const noexcept { return r; } }; struct revrange { using itr = int64_t; struct iterator { itr i; constexpr iterator(itr i_) noexcept : i(i_) { } constexpr void operator ++ () noexcept { --i; } constexpr itr operator * () const noexcept { return i; } constexpr bool operator != (iterator x) const noexcept { return i != x.i; } }; const iterator l, r; constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { } constexpr iterator begin() const noexcept { return r; } constexpr iterator end() const noexcept { return l; } }; template <uint32_t Modulus> class modular { public: using value_type = uint32_t; using max_type = uint64_t; static constexpr value_type mod = Modulus; static constexpr value_type get_mod() { return mod; } static_assert(mod >= 2, "invalid mod :: smaller than 2"); static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31"); template <class T> static constexpr value_type normalize(T value_) { if (value_ < 0) { value_ = -value_; value_ %= mod; if (value_ == 0) return 0; return mod - value_; } return value_ % mod; } private: value_type value; public: constexpr modular(): value(0) { } template <class T> explicit constexpr modular(T value_): value(normalize(value_)) { } template <class T> explicit constexpr operator T() { return static_cast<T>(value); } constexpr value_type get() const { return value; } constexpr modular operator - () const { return modular(mod - value); } constexpr modular operator ~ () const { return inverse(); } constexpr value_type &extract() { return value; } constexpr modular inverse() const { return power(mod - 2); } constexpr modular power(max_type exp) const { modular res(1), mult(*this); while (exp > 0) { if (exp & 1) res *= mult; mult *= mult; exp >>= 1; } return res; } constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; } constexpr modular& operator += (const modular &rhs) { if ((value += rhs.value) >= mod) value -= mod; return *this; } constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; } constexpr modular& operator -= (const modular &rhs) { if ((value += mod - rhs.value) >= mod) value -= mod; return *this; } constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; } constexpr modular& operator *= (const modular &rhs) { value = (max_type) value * rhs.value % mod; return *this; } constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; } constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); } constexpr bool zero() const { return value == 0; } constexpr bool operator == (const modular &rhs) const { return value == rhs.value; } constexpr bool operator != (const modular &rhs) const { return value != rhs.value; } friend std::ostream& operator << (std::ostream &stream, const modular &rhs) { return stream << rhs.value; } }; using m32 = modular<1000000007>; using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; constexpr i32 inf32 = (i32(1) << 30) - 1; constexpr i64 inf64 = (i64(1) << 62) - 1; template <class Func> struct fix_point: private Func { explicit constexpr fix_point(Func &&func): Func(std::forward<Func>(func)) { } template <class... Args> constexpr decltype(auto) operator () (Args &&... args) const { return Func::operator()(*this, std::forward<Args>(args)...); } }; int main() { size_t N; std::cin >> N; std::vector<std::vector<i32>> graph(N); std::vector<i32> degree(N); for (auto i: range(1, N)) { i32 x, y; std::cin >> x >> y; --x; --y; graph[x].push_back(y); ++degree[y]; } i32 root = -1; for (auto i: range(0, N)) { if (degree[i] == 0) { root = i; break; } } std::vector<i32> size(N); std::vector<m32> ans(N); fix_point([&](auto dfs, i32 u) -> void { size[u] = 1; for (auto v: graph[u]) { dfs(v); size[u] += size[v]; ans[u] += ans[v] + m32(size[v]); } })(root); std::cout << std::accumulate(ans.begin(), ans.end(), m32(0)) << '\n'; return 0; }