結果
問題 | No.1364 [Renaming] Road to Cherry from Zelkova |
ユーザー | Mayimg |
提出日時 | 2021-01-23 01:29:47 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 269 ms / 2,500 ms |
コード長 | 12,216 bytes |
コンパイル時間 | 3,109 ms |
コンパイル使用メモリ | 236,840 KB |
実行使用メモリ | 45,076 KB |
最終ジャッジ日時 | 2024-06-09 07:38:47 |
合計ジャッジ時間 | 10,386 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 3 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 10 ms
5,888 KB |
testcase_09 | AC | 6 ms
5,376 KB |
testcase_10 | AC | 11 ms
5,376 KB |
testcase_11 | AC | 9 ms
5,376 KB |
testcase_12 | AC | 10 ms
5,376 KB |
testcase_13 | AC | 145 ms
27,928 KB |
testcase_14 | AC | 227 ms
26,368 KB |
testcase_15 | AC | 224 ms
29,624 KB |
testcase_16 | AC | 152 ms
20,736 KB |
testcase_17 | AC | 57 ms
21,120 KB |
testcase_18 | AC | 269 ms
37,680 KB |
testcase_19 | AC | 255 ms
37,796 KB |
testcase_20 | AC | 231 ms
37,924 KB |
testcase_21 | AC | 259 ms
37,748 KB |
testcase_22 | AC | 241 ms
37,748 KB |
testcase_23 | AC | 41 ms
13,056 KB |
testcase_24 | AC | 30 ms
5,376 KB |
testcase_25 | AC | 125 ms
19,968 KB |
testcase_26 | AC | 220 ms
26,240 KB |
testcase_27 | AC | 171 ms
18,048 KB |
testcase_28 | AC | 94 ms
15,744 KB |
testcase_29 | AC | 173 ms
16,732 KB |
testcase_30 | AC | 103 ms
15,872 KB |
testcase_31 | AC | 56 ms
14,464 KB |
testcase_32 | AC | 107 ms
14,336 KB |
testcase_33 | AC | 230 ms
23,424 KB |
testcase_34 | AC | 204 ms
26,496 KB |
testcase_35 | AC | 222 ms
33,476 KB |
testcase_36 | AC | 208 ms
31,544 KB |
testcase_37 | AC | 99 ms
9,344 KB |
testcase_38 | AC | 100 ms
6,656 KB |
testcase_39 | AC | 96 ms
6,656 KB |
testcase_40 | AC | 97 ms
6,528 KB |
testcase_41 | AC | 97 ms
6,656 KB |
testcase_42 | AC | 95 ms
6,656 KB |
testcase_43 | AC | 109 ms
45,076 KB |
testcase_44 | AC | 83 ms
27,876 KB |
testcase_45 | AC | 79 ms
38,168 KB |
testcase_46 | AC | 16 ms
18,620 KB |
testcase_47 | AC | 2 ms
5,376 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; template<typename T> class Modular { private: long long value; constexpr static int MOD() { return static_cast<int>(T::value); } public: constexpr Modular() : value() {}; constexpr Modular(const Modular& other) : value(other.value) {} template <typename U> constexpr Modular(const U& x) { value = normalize(x); } template <typename U> static long long normalize(const U& x) { long long v; if (-MOD() <= x && x < MOD()) v = static_cast<long long>(x); else v = static_cast<long long>(x % MOD()); if (v < 0) v += MOD(); return v; } constexpr static long long inverse(long long x) { x = (x % MOD() + MOD()) % MOD(); long long y = MOD(), u = 1, v = 0; while(y) { long long t = x / y; x -= t * y; swap(x, y); u -= t * v; swap(u, v); } return (u % MOD() + MOD()) % MOD(); } static long long mul(const long long& a, const long long& b) { long long res; #ifdef _WIN32 unsigned long long x = a * b; unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m; asm( "divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (MOD()) ); res = m; #else res = a * b % MOD(); #endif return res; } explicit operator long long() const noexcept { return value;} template <typename U> explicit operator U() const noexcept { return static_cast<U>(value); } constexpr Modular& operator=(const Modular& other) & noexcept { value = other.value; return *this; } template <typename U> constexpr Modular& operator=(const U& other) & noexcept { return *this = Modular(other); } constexpr Modular& operator+=(const Modular& other) noexcept { if ((value += other.value) >= MOD()) value -= MOD(); return *this; } template <typename U> constexpr Modular& operator+=(const U& other) noexcept { return *this += Modular(other); } constexpr Modular& operator-=(const Modular& other) noexcept { if ((value -= other.value) < 0) value += MOD(); return *this; } template <typename U> constexpr Modular& operator-=(const U& other) noexcept { return *this -= Modular(other); } constexpr Modular& operator*=(const Modular& other) noexcept { this->value = mul(this->value, other.value); return *this; } template <typename U> constexpr Modular& operator*=(const U& other) noexcept {return *this *= Modular(other); } constexpr Modular& operator/=(const Modular& other) noexcept { return *this *= Modular(inverse(other.value)); } template <typename U> constexpr Modular& operator/=(const U& other) noexcept { return *this *= Modular(inverse(normalize(other))); } constexpr Modular& operator++() noexcept {return *this += 1; } constexpr Modular operator++(int) noexcept { Modular ret(*this); *this += 1; return ret; } constexpr Modular& operator--() noexcept {return *this -= 1; } constexpr Modular operator--(int) noexcept { Modular ret(*this); *this -= 1; return ret; } constexpr Modular operator-() const { return Modular(-value); } friend constexpr bool operator==(const Modular& lhs, const Modular<T>& rhs) noexcept { return lhs.value == rhs.value; } template <typename U> friend constexpr bool operator==(const Modular<T>& lhs, U rhs) noexcept { return lhs == Modular<T>(rhs); } template <typename U> friend constexpr bool operator==(U lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) == rhs; } friend constexpr bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return !(lhs == rhs); } template <typename U> friend constexpr bool operator!=(const Modular<T>& lhs, U rhs) noexcept { return !(lhs == rhs); } template <typename U> friend constexpr bool operator!=(U lhs, const Modular<T> rhs) noexcept { return !(lhs == rhs); } friend constexpr bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return lhs.value < rhs.value; } template <typename U> friend constexpr bool operator<(const Modular<T> &lhs, U rhs) noexcept { return lhs.value < rhs; } template <typename U> friend constexpr bool operator<(U lhs, const Modular<T> &rhs) noexcept { return lhs < rhs.value; } friend constexpr bool operator>(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return rhs.value < lhs.value; } template <typename U> friend constexpr bool operator>(const Modular<T> &lhs, U rhs) noexcept { return rhs.value < lhs; } template <typename U> friend constexpr bool operator>(U lhs, const Modular<T> &rhs) noexcept { return rhs < lhs.value; } friend constexpr bool operator<=(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return !(lhs.value > rhs.value); } template <typename U> friend constexpr bool operator<=(const Modular<T> &lhs, U rhs) noexcept { return !(lhs.value > rhs); } template <typename U> friend constexpr bool operator<=(U lhs, const Modular<T> &rhs) noexcept { return !(lhs < rhs.value); } friend constexpr bool operator>=(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return !(lhs.value < rhs.value); } template <typename U> friend constexpr bool operator>=(const Modular<T> &lhs, U rhs) noexcept { return !(lhs.value < rhs); } template <typename U> friend constexpr bool operator>=(U lhs, const Modular<T> &rhs) noexcept { return !(lhs < rhs.value); } friend constexpr Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) += rhs; } template <typename U> friend constexpr Modular<T> operator+(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) += rhs; } template <typename U> friend constexpr Modular<T> operator+(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) += rhs; } friend constexpr Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) -= rhs; } template <typename U> friend constexpr Modular<T> operator-(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) -= rhs; } template <typename U> friend constexpr Modular<T> operator-(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) -= rhs; } friend constexpr Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) *= rhs; } template <typename U> friend constexpr Modular<T> operator*(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) *= rhs; } template <typename U> friend constexpr Modular<T> operator*(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) *= rhs; } friend constexpr Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) /= rhs; } template <typename U> friend constexpr Modular<T> operator/(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) /= rhs; } template <typename U> friend constexpr Modular<T> operator/(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) /= rhs; } friend std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) noexcept { return stream << number.value; } friend std::istream& operator>>(std::istream& stream, Modular<T>& number) { long long in; stream >> in; number.value = Modular<T>::normalize(in); return stream; } constexpr int getmod() const { return MOD(); } }; template<typename T, typename U> Modular<T> power(const Modular<T>& x, const U& y) { assert(y >= 0); Modular<T> k = x, result = 1; U p = y; while (p > 0) { if (p & 1) result *= k; k *= k; p >>= 1; } return result; } template<typename T> class BinaryCoefficients { private: vector<Modular<T>> fact_, inv_, finv_; long long MOD = static_cast<long long>(T::value); public: constexpr BinaryCoefficients(int n = 2020200) : fact_(n, 1), inv_(n, 1), finv_(n, 1) { for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr Modular<T> comb(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr Modular<T> fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr Modular<T> inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr Modular<T> finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; constexpr int mod = 1e9 + 7; //constexpr int mod = 998244353; using mint = Modular<std::integral_constant<decay<decltype(mod)>::type, mod>>; using bicoef = BinaryCoefficients<std::integral_constant<decay<decltype(mod)>::type, mod>>; // struct modValue { static int value; }; // int modValue::value; // int& mod = modValue::value; // using mint = Modular<modValue>; // using bicoef = BinaryCoefficients<modValue>; class StronglyConnectedComponents { private: int siz; int itr = 0; vector<bool> done; vector<int> order, comp; void dfs (int cur, const vector<vector<int>>& g) { done[cur] = true; for (int nxt : g[cur]) { if (!done[nxt]) dfs(nxt, g); } order.push_back(cur); } void rdfs (int cur, const vector<vector<int>>& rg) { done[cur] = true; comp[cur] = itr; for (int nxt : rg[cur]) { if (!done[nxt]) rdfs(nxt, rg); } } void build (const vector<vector<int>>& g, const vector<vector<int>>& rg) { for (int i = 0; i < siz; ++i) if (!done[i]) dfs (i, g); reverse(order.begin(), order.end()); fill(done.begin(), done.end(), false); for (int i = 0; i < siz; ++i) if (!done[order[i]]) rdfs (order[i], rg), ++itr; } public: StronglyConnectedComponents(const vector<vector<int>>& g, const vector<vector<int>>& rg, const int& n) : siz(n), done(siz, false), comp(siz, -1) { build(g, rg); } bool has_cycle() { return *max_element(comp.begin(), comp.end()) < siz - 1; } vector<int> get_comp() { return comp;} }; using SCC = StronglyConnectedComponents; struct TopologicalSort { std::vector<int> orderToid; std::vector<int> idToOrder; inline void build (const vector<vector<int>>& g) { orderToid.clear(); orderToid.reserve(g.size()); std::vector<bool> vis(g.size()); auto dfs = [&] (auto&& self, int cur) { if (vis[cur]) return; vis[cur] = true; for (int nbr : g[cur]) { self(self, nbr); } orderToid.emplace_back(cur); }; for (int i = 0; i < (int) g.size(); i++) dfs(dfs, i); std::reverse(orderToid.begin(), orderToid.end()); assert(orderToid.size() == g.size()); idToOrder.resize(g.size()); for (int i = 0; i < (int) orderToid.size(); i++) { idToOrder[orderToid[i]] = i; } } TopologicalSort () {} TopologicalSort (const vector<vector<int>>& g) {build(g);}; }; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<set<int>> inG(n + 1); vector<vector<tuple<int, int, int>>> inG2(n + 1); for (int i = 0; i < m; i++) { int u, v, l, a; cin >> u >> v >> l >> a; inG[u].emplace(v); inG2[u].emplace_back(v, l, a); } vector<vector<int>> g(n + 1); vector<vector<int>> rg(n + 1); for (int i = 0; i <= n; i++) { for (auto nbr : inG[i]) { g[i].emplace_back(nbr); rg[nbr].emplace_back(i); } } SCC scc(g, rg, n + 1); auto comp = scc.get_comp(); vector<bool> reachable(n + 1); vector<bool> vis(n + 1); auto dfs = [&] (auto&& self, int cur) -> bool { if (vis[cur]) return reachable[cur]; if (cur == n) return (reachable[cur] = true); vis[cur] = true; bool res = false; for (auto nbr : g[cur]) { res |= self(self, nbr); } return (reachable[cur] = res); }; dfs(dfs, 0); map<int, int> cnt; for (int i = 0; i <= n; i++) if (reachable[i]) cnt[comp[i]]++; for (auto p : cnt) { if (p.second > 1) { cout << "INF" << endl; return 0; } } TopologicalSort ts(g); vector<mint> ways(n + 1), tot(n + 1); ways[0] = 1; for (int i = 0; i <= n; i++) { int cur = ts.orderToid[i]; if (!reachable[cur]) continue; for (auto [nbr, l, a] : inG2[cur]) { if (!reachable[nbr]) continue; tot[nbr] += tot[cur] * a; tot[nbr] += ways[cur] * a * l; ways[nbr] += ways[cur] * a; } } cout << tot[n] << endl; return 0; }