// #define MULTI_TESTCASE // #define AOJ_TESTCASE // #define FAST_CIO // #define INTERACTIVE #define INF 4'000'000'000'000'000'037LL // https://github.com/miscalculation53/library/tree/wip/template/template_all.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_all_but_modint.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_types.hpp #include namespace { using namespace std; using ll = long long; using uint = unsigned int; using ull = unsigned long long; using pll = pair; #define vc vector template using vvc = vc>; using vpll = vc; using i128 = __int128_t; using u128 = __uint128_t; #define cauto const auto // https://github.com/miscalculation53/library/tree/wip/template/template_rep.hpp #define overload4(_1,_2,_3,_4,name,...) name #define rep1(i,n) for (ll i = 0, nnnnn = ll(n); i < nnnnn; i++) #define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define repi1(i,n) for (int i = 0, nnnnn = int(n); i < nnnnn; i++) #define repi(...) overload4(__VA_ARGS__, repi3, repi2, repi1)(__VA_ARGS__) #define fe(...) for (auto __VA_ARGS__) #define fec(...) for (cauto &__VA_ARGS__) // https://github.com/miscalculation53/library/tree/wip/template/template_math.hpp // https://github.com/miscalculation53/library/tree/wip/utils/is_integral_ext.hpp template constexpr bool is_integral_ext = is_integral_v || is_same_v || is_same_v; template constexpr bool is_signed_ext = is_signed_v || is_same_v; template constexpr bool is_unsigned_ext = is_unsigned_v || is_same_v; // https://github.com/miscalculation53/library/tree/wip/utils/default_infty.hpp namespace default_infty_detail { template struct has_infty : false_type {}; template struct has_infty> : true_type {}; template const T &custom_value() { static const T value = T::infty(); return value; } template inline constexpr bool unsupported = false; } template constexpr decltype(auto) default_infty() { if constexpr (default_infty_detail::has_infty::value) return default_infty_detail::custom_value(); else if constexpr (is_same_v || is_same_v) return T(INF) * T(INF); else if constexpr (is_integral_ext && !is_same_v) { if constexpr (sizeof(T) >= sizeof(ll)) return T(INF); else if constexpr (numeric_limits::digits >= 31) return (T(1) << 30) - 1; else return T(numeric_limits::max() / 2); } else if constexpr (numeric_limits::has_infinity) return numeric_limits::infinity(); else static_assert(default_infty_detail::unsupported, "No default infinity for this type; specify infty explicitly or define T::infty()."); } template && is_integral_ext>> inline constexpr T divfloor(U a, V b) { return T(a) / T(b) - (T(a) % T(b) && (T(a) ^ T(b)) < 0); } template && is_integral_ext>> inline constexpr T safemod(U a, V b) { return T(a) - T(b) * divfloor(a, b); } // https://github.com/miscalculation53/library/tree/wip/template/template_vector.hpp #define ALL(a) (a).begin(), (a).end() template inline T SZ(const V &x) { return x.size(); } #define eb emplace_back #define LMD(x,fx) ([&](const auto &x) { return fx; }) // https://github.com/miscalculation53/library/tree/wip/template/template_algo.hpp // https://github.com/miscalculation53/library/tree/wip/utils/resolved_infty.hpp // https://github.com/miscalculation53/library/tree/wip/utils/resolved_value.hpp template , int> = 0> constexpr T resolved_value() { return T(x); } template , long> = 0> const T &resolved_value() { static const T value = T(x()); return value; } template constexpr decltype(auto) resolved_infty() { if constexpr (is_same_v) return default_infty(); else return resolved_value(); } template auto MAX(const V &v) { return *max_element(ALL(v)); } template struct has_e0 : false_type {}; template struct has_e0> : true_type {}; template inline constexpr bool has_e0_v = has_e0::value; template struct MonoidAdd { using S = T; static constexpr S op(S a, S b) { return a + b; } static constexpr S e() { if constexpr (has_e0_v) return S::e0(); else return {}; } }; namespace internal { } template vc cuml(const vc &v, int left_index = 0) { const int n = v.size(); vc res(n + 1); res[0] = M::e(); repi(i, n) res[i + 1] = M::op(res[i], v[i]); res.erase(res.begin(), res.begin() + left_index); return res; } template vc cumlsum(const vc &v, int left_index = 0) { return cuml>(v, left_index); } constexpr array DRULgrid = {{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}}; constexpr array DRULplane = {{{0, -1}, {1, 0}, {0, 1}, {-1, 0}}}; // https://github.com/miscalculation53/library/tree/wip/template/template_binsearch.hpp template struct is_random_access_iterator { static constexpr bool value = is_same_v< typename iterator_traits::iterator_category, random_access_iterator_tag >; }; template constexpr bool is_random_access_iterator_v = is_random_access_iterator::value; namespace internal { }; // https://github.com/miscalculation53/library/tree/wip/template/template_bit.hpp template inline constexpr ull MASK(T k) { return (1ULL << k) - 1ULL; } inline constexpr ll countr_zero(ll x) { assert(x != 0); return std::countr_zero((ull)x); } // https://github.com/miscalculation53/library/tree/wip/template/template_inout.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_dump.hpp // https://github.com/miscalculation53/library/tree/wip/template/template_dump_map.hpp #define CPP_DUMP_DEFINE_DATA(...) #define dump(...) #define oj(...) __VA_ARGS__ namespace fastio { template struct unsigned_integer { using type = make_unsigned_t; }; template <> struct unsigned_integer { using type = u128; }; template <> struct unsigned_integer { using type = u128; }; template using unsigned_integer_t = typename unsigned_integer::type; static constexpr uint32_t SIZ = 1 << 17; char ibuf[SIZ]; char obuf[SIZ]; char out[100]; uint32_t pil = 0, pir = 0, por = 0; struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { memcpy(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SIZ - pir + pil, stdin); pil = 0; if (pir < SIZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } template void rd1_integer(T &x) { using U = unsigned_integer_t; bool minus = false; U val = 0; if constexpr (check_buffer) if (pil + 100 > pir) load(); uint32_t p = pil; while (ibuf[p] < '-') ++p; if constexpr (is_signed::value || is_same_v) { if (ibuf[p] == '-') minus = true, ++p; } while ('0' <= ibuf[p]) val = val * 10 + (ibuf[p++] & 15); pil = p; if constexpr (is_signed::value || is_same_v) { if (minus) { const U min_abs = U(numeric_limits::max()) + 1; x = val == min_abs ? numeric_limits::lowest() : -T(val); } else x = T(val); } else x = T(val); } void rd1(ll &x) { rd1_integer(x); } template void rd1(pair &p) { return rd1(p.first), rd1(p.second); } template void rd1(vc &x) { for (auto &d: x) rd1(d); } template void read(T &...x) { if constexpr (sizeof...(T) <= SIZ / 100 && ((!is_same_v && (is_integral_v || is_same_v || is_same_v)) && ...)) { if (pil + 100 * sizeof...(T) > pir) load(); (rd1_integer(x), ...); } else (rd1(x), ...); } void wt1(const char c) { if (por == SIZ) flush(); obuf[por++] = c; } template void wt1_integer(T x) { if (por > SIZ - 100) flush(); using U = unsigned_integer_t; U ux; if constexpr (is_signed::value || is_same_v) { if (x < 0) obuf[por++] = '-', ux = U(0) - U(x); else ux = U(x); } else ux = x; int outi; for (outi = 96; ux >= 10000; outi -= 4) { memcpy(out + outi, pre.num[ux % 10000], 4); ux /= 10000; } if (ux >= 1000) { memcpy(obuf + por, pre.num[ux], 4); por += 4; } else if (ux >= 100) { memcpy(obuf + por, pre.num[ux] + 1, 3); por += 3; } else if (ux >= 10) { int q = (ux * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (ux - q * 10) | '0'; por += 2; } else obuf[por++] = ux | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template , int> = 0> void wt1(T x) { wt1_integer(x); } template void wt1(const pair &val); template void wt1(const tuple &tpl); template void wt1(const array &val); template void wt1(const vector &val); template void write(T &&...x) { (wt1(std::forward(x)), ...); } template void print(T &&...x) { if constexpr (sizeof...(T)) { int i = 0; ((i++ ? wt1(' ') : void(), wt1(std::forward(x))), ...); } wt1('\n'); } } struct Dummy { Dummy() { atexit(fastio::flush); } } dummy; namespace internal { template void READnodump(Ts &...a) { fastio::read(a...); } template void READVECnodump(int n, vc &...v) { (v.resize(n), ...); READnodump(v...); } }; #define READ(...) internal::READnodump(__VA_ARGS__); dump(__VA_ARGS__) #define IN(T,...) T __VA_ARGS__; READ(__VA_ARGS__) #define LL(...) IN(ll, __VA_ARGS__) #define READVEC(...) internal::READVECnodump(__VA_ARGS__); dump(__VA_ARGS__) #define VEC(T,n,...) vc __VA_ARGS__; READVEC(n, __VA_ARGS__) #define PRINT fastio::print template pair &operator+=(pair &a, const P &b) { a.first += b.first; a.second += b.second; return a; } namespace internal { }; template void offset(vc &v, const Add &add) { for (auto &vi : v) vi += add; } template void offset(vvc &v, const Add &add) { for (auto &vi : v) for (auto &vij : vi) vij += add; } namespace internal { }; // https://github.com/miscalculation53/library/tree/wip/template/template_random.hpp mt19937_64 mt; namespace internal { }; // https://github.com/miscalculation53/library/tree/wip/math/modint/template_modint.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/modint.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_static.hpp // https://github.com/miscalculation53/library/tree/wip/utils/larger_int.hpp namespace larger_int_detail { } template struct larger_int { private: static constexpr bool check(); static_assert(check()); public: using type = T; }; #define LARGER_INT(T,U) template <> struct larger_int { using type = U; }; LARGER_INT(signed char, short) LARGER_INT(short, int) LARGER_INT(int, long long) LARGER_INT(long, __int128_t) LARGER_INT(long long, __int128_t) LARGER_INT(unsigned char, unsigned short) LARGER_INT(unsigned short, unsigned int) LARGER_INT(unsigned int, unsigned long long) LARGER_INT(unsigned long, __uint128_t) LARGER_INT(unsigned long long, __uint128_t) #undef LARGER_INT template struct Rational; template struct larger_int> { using type = Rational::type>; }; template using larger_int_t = typename larger_int::type; // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_isprime.hpp namespace internal { template constexpr ll powmod_constexpr(ll x, ll n, T m) { if (m == 1) return 0; using U = make_unsigned_t; using L = larger_int_t; U r = 1, y = safemod(x, m); while (n) { if (n & 1) r = L(r) * y % m; y = L(y) * y % m; n >>= 1; } return r; } template constexpr bool isprime = isprime_constexpr(n); }; namespace internal { template struct policy_static { using mod_type = decltype(M); using value_type = make_unsigned_t; using calc_type = larger_int_t; static constexpr bool is_prime = isprime_constexpr(M); static constexpr value_type init(value_type v) { return v; } static constexpr mod_type val(value_type v); }; }; // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_barrett32.hpp namespace internal { struct barrett32 { uint m; ull im; explicit barrett32(uint m) : m(m), im((ull)(-1) / m + 1) {} }; template struct policy_barrett32 { using value_type = uint; using calc_type = ull; using mod_type = int; static constexpr bool is_prime = false; static inline barrett32 reducer{998244353}; static value_type init(value_type v) { return v; } static mod_type val(value_type v); }; }; // https://github.com/miscalculation53/library/tree/wip/math/modint/modint_internal_montgomery64.hpp namespace internal { inline constexpr ull inv64(ull a) { ull x = a; while (a * x != 1) x *= 2 - a * x; return x; } struct montgomery64odd { ull m, im, sq; explicit montgomery64odd(ull m) : m(m), im(inv64(m)), sq(-u128(m) % m) {} ull reduce(u128 x) const { auto t = (x + u128(m) * (-im * ull(x))) >> 64; if (t >= m) t -= m; return (ull)t; } ull inv_reduce(i128 v) const { return reduce(u128(v % m + m) * sq); } }; struct montgomery64 { ull m, mx, imx, d, q; uint b; explicit montgomery64(ull m) : m(m) { b = countr_zero(m), mx = m >> b; imx = inv64(mx); d = powmod_constexpr((mx + 1) / 2, b, mx); u128 sq = -u128(mx) % mx; q = (1 + (((sq - 1) * d) << b)) % m; } ull reduce(u128 x) const { if (b == 0) { auto t = (x + u128(mx) * (-imx * ull(x))) >> 64; if (t >= m) t -= m; return (ull)t; } ull p = x & MASK(b); x = (x >> b) + p * d; ull y = p << (64 - b); auto t = (x + u128(mx) * (imx * (y - ull(x)))) >> (64 - b); if (t >= m) { t -= m; if (t >= m) t -= m; } return (ull)t; } ull inv_reduce(i128 v) const { return reduce(u128(v % m + m) * q); } }; template struct policy_montgomery64_odd { using value_type = ull; using calc_type = u128; using mod_type = ll; static constexpr bool is_prime = false; static inline montgomery64odd reducer{(1LL << 61) - 1}; static value_type init(value_type v) { return reducer.inv_reduce(v); } static mod_type val(value_type v); }; template struct policy_montgomery64 { using value_type = ull; using calc_type = u128; using mod_type = ll; static constexpr bool is_prime = false; static inline montgomery64 reducer{(1LL << 61) - 1}; static value_type init(value_type v) { return reducer.inv_reduce(v); } static mod_type val(value_type v); }; }; // https://github.com/miscalculation53/library/tree/wip/math/extgcd.hpp namespace internal { template struct modint_impl { using V = typename Policy::value_type; using M = typename Policy::mod_type; using mint = modint_impl; private: V _v; public: modint_impl(); template >> modint_impl(T v); M val() const; friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint &lhs, const mint &rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs._v != rhs._v; } friend void rd1(mint &x) { long long a; fastio::rd1(a); x = a; } friend void wt1(const mint &x) { fastio::wt1(x.val()); } }; }; template using static_modint32 = internal::modint_impl>; template using dynamic_modint32 = internal::modint_impl>; template using static_modint64 = internal::modint_impl>; template using dynamic_modint64_odd = internal::modint_impl>; template using dynamic_modint64 = internal::modint_impl>; using modint998244353 = static_modint32<998244353>; template struct is_modint : std::false_type { }; template struct is_modint> : std::true_type { }; template inline constexpr bool is_modint_v = is_modint::value; template struct is_static_modint : false_type {}; template struct is_static_modint> : true_type {}; template struct is_static_modint> : true_type {}; template inline constexpr bool is_static_modint_v = is_static_modint::value; template struct is_dynamic_modint : false_type {}; template struct is_dynamic_modint> : true_type {}; template struct is_dynamic_modint> : true_type {}; template struct is_dynamic_modint> : true_type {}; template inline constexpr bool is_dynamic_modint_v = is_dynamic_modint::value; // https://github.com/miscalculation53/library/tree/wip/math/modint/power_table.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/binomial.hpp template struct Binomial { private: inline static decltype(T::mod()) mod; public: inline static vc fac_, finv_, inv_; }; // https://github.com/miscalculation53/library/tree/wip/math/modint/stom.hpp // https://github.com/miscalculation53/library/tree/wip/math/modint/to_rational.hpp // https://github.com/miscalculation53/library/tree/wip/math/svp2d.hpp namespace cpp_dump { template struct rat_value { T p, q; friend ostream &operator<<(ostream &os, const rat_value &x) { os << x.p; if (x.q != 1) os << '/' << x.q; return os; } }; template inline constexpr bool rat_is_map = false; template inline constexpr bool rat_is_map> = true; template inline constexpr bool rat_is_map> = true; template inline constexpr bool rat_is_map> = true; template inline constexpr bool rat_is_map> = true; } using mint = modint998244353; // using mint = modint1000000007; // using mint = static_modint<1000000000>; // using mint = modint; void init() { oj(mt.seed(random_device()())); } // https://github.com/miscalculation53/library/tree/wip/graph/connected_components.hpp // https://github.com/miscalculation53/library/tree/wip/graph/graph.hpp // https://github.com/miscalculation53/library/tree/wip/ds/csr.hpp template struct CSR { protected: int n, m; vc start; vc elist; vc len; inline int get_last(int i) const { if constexpr (is_erasable) return start[i] + len[i]; else return start[i + 1]; } template struct RowBase { using iterator = Iter; using reference = typename iterator_traits::reference; private: iterator begi, endi; public: RowBase(const iterator &begi, const iterator &endi) : begi(begi), endi(endi) {} inline iterator begin() const { return begi; } inline iterator end() const { return endi; } template inline I size() const { return endi - begi; } inline bool empty() const { return size() == 0; } inline reference operator[](int i) const { return *(begi + i); } inline reference back() const { assert(!empty()); return *prev(endi); } }; using Row = RowBase::iterator>; using ConstRow = RowBase::const_iterator>; public: CSR() {} CSR(const vc &row_sizes) : n(row_sizes.size()) { fec(s : row_sizes) assert(s >= 0); start = cumlsum(row_sizes); m = start.back(); elist.resize(m); if constexpr (is_erasable) len = row_sizes; } template CSR(int n, const vc> &ies) : n(n), m(ies.size()), start(n, 0), elist(m) { if constexpr (is_erasable) len.resize(n); fec([ i, e ] : ies) { assert(0 <= i && i < n); start[i]++; } start = cumlsum(start); if constexpr (is_erasable) repi(i, n) len[i] = start[i + 1] - start[i]; auto cnt = start; repi(j, m) { cauto & [ i, e ] = ies[j]; int &k = cnt[i]; elist[k] = e; k++; } } CSR(const vvc &vv) : n(vv.size()), start(n + 1, 0) { m = 0; fec(row : vv) m += row.size(); elist.resize(m); if constexpr (is_erasable) len.resize(n); for (int i = 0, j = 0; i < n; i++) { start[i] = j; if constexpr (is_erasable) len[i] = vv[i].size(); fec(e : vv[i]) { elist[j] = e; j++; } } start.back() = m; } Row operator[](int i) { return Row(elist.begin() + start[i], elist.begin() + get_last(i)); } ConstRow operator[](int i) const { return ConstRow(elist.begin() + start[i], elist.begin() + get_last(i)); } ConstRow at(int i) const { if (!(0 <= i && i < n)) return ConstRow(elist.begin(), elist.begin()); return ConstRow(elist.begin() + start[i], elist.begin() + get_last(i)); } int offset(int i) const { assert(0 <= i && i <= n); return start[i]; } template I size() const { return n; } const vc &get_elist() const { return elist; } }; template struct Edge { int from, to; Cost cost; int index; CPP_DUMP_DEFINE_DATA(from, to, cost, index); }; template <> struct Edge { int from, to, index; static constexpr ll cost = 1; CPP_DUMP_DEFINE_DATA(from, to, cost, index); Edge() : from(-1), to(-1), index(-1) {} Edge(int s, int t, int i = -1) : from(s), to(t), index(i) {} operator int() const { return to; } bool operator<(const Edge &) const { return false; } }; template struct Graph { using E = Edge; protected: template > struct InternalEdge { int to, index; C cost; }; template struct InternalEdge { int to, index; static constexpr ll cost = 1; }; using GE = conditional_t>; using Weight = decay_t; int n = 0, m = 0, era = 0; CSR g; vc eid_to_elist_id; static E make_edge(int from, int to, const Weight &cost, int index) { if constexpr (is_void_v) return E(from, to, index); else return E(from, to, cost, index); } struct OutEdgeRow; struct OutEdgeIter { using iterator_category = input_iterator_tag; using value_type = E; using difference_type = ptrdiff_t; using pointer = const E *; using reference = const E &; private: friend struct OutEdgeRow; int from = -1; typename vc::const_iterator it; mutable E e; OutEdgeIter(int from, typename vc::const_iterator it) : from(from), it(it) {} public: OutEdgeIter() = default; reference operator*() const { if constexpr (is_erasable) return *it; else { e = make_edge(from, it->to, it->cost, it->index); return e; } } OutEdgeIter &operator++() { ++it; return *this; } bool operator==(const OutEdgeIter &rhs) const { return it == rhs.it; } bool operator!=(const OutEdgeIter &rhs) const { return !(*this == rhs); } }; struct OutEdgeRow { private: const Graph *g; int from, l, r; OutEdgeIter iter(int pos) const { return {from, g->g.get_elist().begin() + pos}; } public: OutEdgeRow(const Graph *g, int from) : g(g), from(from), l(g->g.offset(from)), r(l + g->g[from].size()) {} OutEdgeIter begin() const { return iter(l); } OutEdgeIter end() const { return iter(r); } template I size() const { return r - l; } bool empty() const { return l == r; } E back() const { assert(!empty()); return (*this)[size() - 1]; } }; template void build(F input_edge) { vc row_sizes(n); repi(i, m) { auto [u, v, w] = input_edge(i); assert(0 <= u && u < n && 0 <= v && v < n); row_sizes[u]++; if constexpr (!is_directed) if (u != v) row_sizes[v]++; } g = CSR(row_sizes); if constexpr (is_erasable) eid_to_elist_id.assign((is_directed ? 1 : 2) * m, -1); vc cnt(n); repi(i, m) { auto [u, v, w] = input_edge(i); int j = cnt[u]++; int k = g.offset(u) + j; if constexpr (is_erasable) g[u][j] = make_edge(u, v, w, i); else if constexpr (is_void_v) g[u][j] = GE{int(v), i}; else g[u][j] = GE{int(v), i, w}; if constexpr (is_erasable) { int id = is_directed ? i : 2 * i + (u <= v); eid_to_elist_id[id] = k; } if constexpr (!is_directed) { if (u != v) { j = cnt[v]++; k = g.offset(v) + j; if constexpr (is_erasable) g[v][j] = make_edge(v, u, w, i); else if constexpr (is_void_v) g[v][j] = GE{int(u), i}; else g[v][j] = GE{int(u), i, w}; if constexpr (is_erasable) eid_to_elist_id[2 * i + (v <= u)] = k; } } } } public: Graph() {} template Graph(int n, const vc> &es) : n(n), m(es.size()), era(0) { build(LMD(i, (tuple{es[i].first, es[i].second, Weight(1)}))); } template , int> = 0> Graph(int n, const vc> &es, const Weight &dflt_cost) : n(n), m(es.size()), era(0) { build(LMD(i, (tuple{es[i].first, es[i].second, dflt_cost}))); } template && !is_void_v, int> = 0> Graph(int n, const vc> &es) : n(n), m(es.size()), era(0) { build(LMD(i, es[i])); } Graph(int n, const vc &es) : n(n), m(es.size()), era(0) { build(LMD(i, (tuple{es[i].from, es[i].to, es[i].cost}))); } template I size() const { return n; } auto out_edges(int v) const { return OutEdgeRow(this, v); } template vc indegs() const { vc res(n); if constexpr (is_directed) { repi(v, n) fec(e : g[v]) res[e.to]++; } else repi(v, n) res[v] = g[v].size(); return res; } template vc outdegs() const { vc res(n); repi(v, n) res[v] = g[v].size(); return res; } private: }; template Graph(int, const vc> &) -> Graph; template Graph(int, const vc> &) -> Graph; template Graph(int, const vc> &, const Cost &) -> Graph; template Graph(int, const vc> &) -> Graph; template using GraphDirected = Graph; template using GraphUndirected = Graph; // https://github.com/miscalculation53/library/tree/wip/ds/my_queue.hpp template struct MyQueue { private: vc d; int pos = 0; public: template I size() const { return SZ(d) - pos; } bool empty() const { return pos == SZ(d); } void push(const T &t) { d.eb(t); } T &front() { return d[pos]; } void clear() { d.clear(); pos = 0; } void pop() { pos++; } }; template vc connected_component_ids(const GraphUndirected &g) { const int n = g.size(); vc res(n, -1); int id = 0; MyQueue que; repi(sv, n) { if (res[sv] != -1) continue; res[sv] = id; que.clear(); que.push(sv); while (!que.empty()) { int v = que.front(); que.pop(); fe(nv : g.out_edges(v)) { if (res[nv] != -1) continue; res[nv] = id; que.push(nv); } } id++; } return res; } // https://github.com/miscalculation53/library/tree/wip/ds/group_index.hpp template struct GroupIndex { private: int n, m; CSR csr; public: GroupIndex() {} template GroupIndex(const vc &a) : n(a.size()), m(a.empty() ? 0 : MAX(a) + 1) { vc> ies(n); repi(i, n) { assert(0 <= a[i]); ies[i] = {a[i], i}; } csr = CSR(m, ies); } auto idxs(int val) const { return csr.at(val); } I value_bound() const { return m; } }; // 0 -> 2 // 0 -> 3 // 1 -> 1 // 3 -> 3 // -2, 0, 1, 1 ll solve(ll N, vpll UV) { GraphUndirected Gund(N, UV); GraphDirected Gd(N, UV); auto in = Gd.indegs(), out = Gd.outdegs(); auto ccid = connected_component_ids(Gund); GroupIndex ccgrp(ccid); ll ccnum = MAX(ccid) + 1; ll isol = 0; rep(i, N) if (in[i] == 0 && out[i] == 0) isol++; dump(ccnum, isol); ll ans = 0; rep(k, ccgrp.value_bound()) { auto is = ccgrp.idxs(k); ll tmp = 0; fe(i : is) tmp += max(0LL, in[i] - out[i]); dump(is, tmp); ans += tmp; } ans += ccnum - isol; return ans; } void main2() { LL(N, M); VEC(pll, M, UV); offset(UV, pll{-1, -1}); PRINT(solve(N, UV)); } void test() { //* //*/ } // https://github.com/miscalculation53/library/tree/wip/template/template_main.hpp template struct Main { Main() { cauto CERR = [](string val, string color) { string s = "\033[" + color + "m" + val + "\033[m"; }; CERR("\n[FAST_IO]\n\n", "32"); cout << fixed << setprecision(20); init(); CERR("\n[SINGLE_TESTCASE]\n\n", "36"); main2(); } }; Main main_dummy; } int main() {}