結果
問題 | No.1507 Road Blocked |
ユーザー | そすうぽよ |
提出日時 | 2021-05-14 22:25:16 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 104 ms / 2,000 ms |
コード長 | 5,885 bytes |
コンパイル時間 | 2,241 ms |
コンパイル使用メモリ | 206,032 KB |
実行使用メモリ | 16,512 KB |
最終ジャッジ日時 | 2024-10-02 02:02:32 |
合計ジャッジ時間 | 6,446 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 75 ms
16,512 KB |
testcase_04 | AC | 102 ms
8,832 KB |
testcase_05 | AC | 98 ms
8,576 KB |
testcase_06 | AC | 96 ms
8,704 KB |
testcase_07 | AC | 104 ms
8,576 KB |
testcase_08 | AC | 98 ms
8,732 KB |
testcase_09 | AC | 104 ms
8,704 KB |
testcase_10 | AC | 99 ms
8,704 KB |
testcase_11 | AC | 101 ms
8,800 KB |
testcase_12 | AC | 97 ms
8,704 KB |
testcase_13 | AC | 94 ms
8,704 KB |
testcase_14 | AC | 92 ms
8,704 KB |
testcase_15 | AC | 98 ms
8,704 KB |
testcase_16 | AC | 95 ms
8,704 KB |
testcase_17 | AC | 100 ms
8,704 KB |
testcase_18 | AC | 99 ms
8,704 KB |
testcase_19 | AC | 95 ms
8,576 KB |
testcase_20 | AC | 99 ms
8,832 KB |
testcase_21 | AC | 92 ms
8,704 KB |
testcase_22 | AC | 91 ms
8,632 KB |
testcase_23 | AC | 93 ms
8,832 KB |
testcase_24 | AC | 97 ms
8,576 KB |
testcase_25 | AC | 93 ms
8,704 KB |
testcase_26 | AC | 100 ms
8,576 KB |
testcase_27 | AC | 90 ms
8,616 KB |
testcase_28 | AC | 98 ms
8,704 KB |
testcase_29 | AC | 98 ms
8,704 KB |
testcase_30 | AC | 98 ms
8,576 KB |
testcase_31 | AC | 96 ms
8,704 KB |
testcase_32 | AC | 101 ms
8,704 KB |
ソースコード
#include <bits/stdc++.h> #include <variant> #define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++) #define rep(i,n) rep2(i,0,n) #define all(x) begin(x),end(x) #ifdef ENV_LOCAL #define dump if (1) cerr #else #define dump if (0) cerr #endif using namespace std; using namespace std::string_literals; using i32 = int32_t; using i64 = int64_t; using f64 = double; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; /* * harudake::modint * * Copyright (c) 2021 prime number * * This software is released under the MIT license. * see https://opensource.org/licenses/MIT * */ namespace harudake { template <typename base_t, base_t MOD> class static_modint; using modint_1000000007 = static_modint<uint64_t, 1'000'000'007>; using modint_998244353 = static_modint<uint64_t, 998'244'353>; template <typename base_t, base_t MOD> static_modint<base_t, MOD> add(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { base_t tmp = lhs.val + rhs.val; if (tmp >= MOD) tmp -= MOD; return static_modint<base_t, MOD>::make_raw(tmp); } template <typename base_t, base_t MOD> static_modint<base_t, MOD> sub(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { base_t tmp = lhs.val + MOD - rhs.val; if (tmp >= MOD) tmp -= MOD; return static_modint<base_t, MOD>::make_raw(tmp); } template <typename base_t, base_t MOD> static_modint<base_t, MOD> mul(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { base_t tmp = lhs.val * rhs.val; tmp %= MOD; return static_modint<base_t, MOD>::make_raw(tmp); } // data types must be signed integer inline int64_t inv (const int64_t a, const int64_t p) { return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p); }; template <typename base_t, base_t MOD> static_modint<base_t, MOD> div(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { base_t tmp = lhs.val * inv(rhs.val, MOD); tmp %= MOD; return static_modint<base_t, MOD>::make_raw(tmp); } template <typename base_t, base_t MOD> static_modint<base_t, MOD> pow(const static_modint<base_t, MOD> base, const uint64_t index) { if (index == 0) return static_modint<base_t, MOD> { 1 }; if (index % 2 == 1) { return pow(base, index - 1) * base; } else { auto half = pow(base, index / 2); return half * half; } } template <typename base_t, base_t MOD> class static_modint { using modint = static_modint; public: static constexpr base_t mod = MOD; static_modint() : val(0) {} static_modint(const base_t x) : val(x % MOD) {} static_modint(const modint&) = default; modint& operator=(const modint&) = default; modint& operator=(const base_t x) { val = x % MOD; return *this; } modint operator-() const { return val == 0 ? 0 : MOD - val; } explicit operator base_t() const { return val; } base_t get() const { return val; } constexpr base_t get_mod() const { return mod; } static modint make_raw(const base_t raw) { modint res; res.val = raw; return res; } private: friend modint add<base_t, MOD>(const modint, const modint); friend modint sub<base_t, MOD>(const modint, const modint); friend modint mul<base_t, MOD>(const modint, const modint); friend modint div<base_t, MOD>(const modint, const modint); base_t val; }; template <typename base_t, base_t MOD> static_modint<base_t, MOD> operator+(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { return add(lhs, rhs); } template <typename base_t, base_t MOD> static_modint<base_t, MOD> operator-(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { return sub(lhs, rhs); } template <typename base_t, base_t MOD> static_modint<base_t, MOD> operator*(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { return mul(lhs, rhs); } template <typename base_t, base_t MOD> static_modint<base_t, MOD> operator/(const static_modint<base_t, MOD> lhs, const static_modint<base_t, MOD> rhs) { return div(lhs, rhs); } template <typename base_t, base_t MOD> static_modint<base_t, MOD> operator^(const static_modint<base_t, MOD> lhs, const uint64_t rhs) { return pow(lhs, rhs); } template <typename base_t, base_t MOD> static_modint<base_t, MOD>& operator+=(static_modint<base_t, MOD>& lhs, const static_modint<base_t, MOD> rhs) { return lhs = lhs + rhs; } template <typename base_t, base_t MOD> static_modint<base_t, MOD>& operator-=(static_modint<base_t, MOD>& lhs, const static_modint<base_t, MOD> rhs) { return lhs = lhs - rhs; } template <typename base_t, base_t MOD> static_modint<base_t, MOD>& operator*=(static_modint<base_t, MOD>& lhs, const static_modint<base_t, MOD> rhs) { return lhs = lhs * rhs; } template <typename base_t, base_t MOD> static_modint<base_t, MOD>& operator/=(static_modint<base_t, MOD>& lhs, const static_modint<base_t, MOD> rhs) { return lhs = lhs / rhs; } template <typename base_t, base_t MOD> static_modint<base_t, MOD>& operator^=(static_modint<base_t, MOD>& lhs, const uint64_t rhs) { return lhs = lhs ^ rhs; } } // namespace harudake using namespace harudake; using modint = modint_998244353; pair<i32, modint> dfs(const vector<vi32>& g, i32 n, i32 i, i32 p = -1) { i32 sz = 1; modint ans(0); for (auto&& to : g[i]) { if (to == p) continue; auto [csz, cval] = dfs(g, n, to, i); ans += cval; sz += csz; } if (sz < n) { modint mn(n); modint mk(sz); ans += (mk*(mk-modint(1)) + (mn-mk)*(mn-mk-modint(1))) / (mn * (mn - modint(1))); } return make_pair(sz, ans); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i64 n; cin>>n; vector<vi32> g(n); rep(i,n-1) { i32 u,v; cin>>u>>v; --u;--v; g[u].push_back(v); g[v].push_back(u); } auto [sz, ans] = dfs(g, n, 0); ans /= modint(n-1); cout<<ans.get()<<endl; return 0; }