結果
問題 |
No.3291 K-step Navigation
|
ユーザー |
![]() |
提出日時 | 2025-10-03 21:43:17 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 34,964 bytes |
コンパイル時間 | 6,168 ms |
コンパイル使用メモリ | 342,248 KB |
実行使用メモリ | 7,720 KB |
最終ジャッジ日時 | 2025-10-03 21:43:35 |
合計ジャッジ時間 | 17,308 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 WA * 12 |
ソースコード
// BEGIN: main.cpp #line 1 "main.cpp" // BEGIN: my_template.hpp #line 1 "my_template.hpp" #if defined(LOCAL) #include <my_template_compiled.hpp> #else #if defined(__GNUC__) #include <bits/allocator.h> #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,popcnt") #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using i128 = __int128; using u128 = unsigned __int128; using f128 = __float128; template <class T> constexpr T infty = 0; template <> constexpr int infty<int> = 1'010'000'000; template <> constexpr ll infty<ll> = 2'020'000'000'000'000'000; template <> constexpr u32 infty<u32> = infty<int>; template <> constexpr u64 infty<u64> = infty<ll>; template <> constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000; template <> constexpr double infty<double> = numeric_limits<double>::infinity(); template <> constexpr long double infty<long double> = numeric_limits<long double>::infinity(); using pi = pair<ll, ll>; using vi = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vc<T>>; template <class T> using vvvc = vector<vvc<T>>; template <class T> using vvvvc = vector<vvvc<T>>; template <class T> using pq_max = priority_queue<T>; template <class T> using pq_min = priority_queue<T, vector<T>, greater<T>>; #define vv(type, name, h, ...) \ vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector<vector<vector<type>>> name( \ h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector<vector<vector<vector<type>>>> name( \ a, vector<vector<vector<type>>>( \ b, vector<vector<type>>(c, vector<type>(__VA_ARGS__)))) // https://trap.jp/post/1224/ #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c)) #define FOR1_R(a) for (ll i = (a) - 1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a) - 1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (b) - 1; i >= ll(a); --i) #define overload4(a, b, c, d, e, ...) e #define overload3(a, b, c, d, ...) d #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define stoi stoll int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); } int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); } int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); } int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template <typename T> T kth_bit(int k) { return T(1) << k; } template <typename T> bool has_kth_bit(T x, int k) { return x >> k & 1; } template <typename UINT> struct all_bit { struct iter { UINT s; iter(UINT s) : s(s) {} int operator*() const { return lowbit(s); } iter &operator++() { s &= s - 1; return *this; } bool operator!=(const iter) const { return s != 0; } }; UINT s; all_bit(UINT s) : s(s) {} iter begin() const { return iter(s); } iter end() const { return iter(0); } }; template <typename UINT> struct all_subset { static_assert(is_unsigned<UINT>::value); struct iter { UINT s, t; bool ed; iter(UINT s) : s(s), t(s), ed(0) {} UINT operator*() const { return s ^ t; } iter &operator++() { (t == 0 ? ed = 1 : t = (t - 1) & s); return *this; } bool operator!=(const iter) const { return !ed; } }; UINT s; all_subset(UINT s) : s(s) {} iter begin() const { return iter(s); } iter end() const { return iter(0); } }; template <typename T> T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); } template <typename T> T ceil(T x, T y) { return floor(x + y - 1, y); } template <typename T> T bmod(T x, T y) { return x - y * floor(x, y); } template <typename T> pair<T, T> divmod(T x, T y) { T q = floor(x, y); return {q, x - q * y}; } constexpr ll TEN[] = { 1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL, 1000000000000000000LL, }; template <typename T, typename U> T SUM(const U &A) { return std::accumulate(A.begin(), A.end(), T{}); } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) template <class C, class T> inline long long LB(const C &c, const T &x) { return lower_bound(c.begin(), c.end(), x) - c.begin(); } template <class C, class T> inline long long UB(const C &c, const T &x) { return upper_bound(c.begin(), c.end(), x) - c.begin(); } #define UNIQUE(x) \ sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit() template <typename T> T POP(deque<T> &que) { T a = que.front(); que.pop_front(); return a; } template <class T, class Container, class Compare> T POP(priority_queue<T, Container, Compare> &que) { T a = que.top(); que.pop(); return a; } template <typename T> T POP(vc<T> &que) { T a = que.back(); que.pop_back(); return a; } template <typename F> ll binary_search(F check, ll ok, ll ng, bool check_ok = true) { if (check_ok) assert(check(ok)); while (llabs(ok - ng) > 1) { auto x = (ng + ok) / 2; (check(x) ? ok : ng) = x; } return ok; } template <typename F> double binary_search_real(F check, double ok, double ng, int iter = 100) { FOR(iter) { double x = (ok + ng) / 2; (check(x) ? ok : ng) = x; } return (ok + ng) / 2; } template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } // ? は -1 vc<int> s_to_vi(const string &S, char first_char) { vc<int> A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } template <typename T, typename U> vc<T> cumsum(const vc<U> &A, int off = 1) { int N = A.size(); vc<T> B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } // stable sort template <typename T> vc<int> argsort(const vc<T> &A) { vc<int> ids(len(A)); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); }); return ids; } // A[I[0]], A[I[1]], ... template <typename T> vc<T> rearrange(const vc<T> &A, const vc<int> &I) { vc<T> B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template <typename T, typename... Vectors> void concat(vc<T> &first, const Vectors &...others) { vc<T> &res = first; (res.insert(res.end(), others.begin(), others.end()), ...); } #endif // END: my_template.hpp #line 2 "main.cpp" // BEGIN: other/io.hpp #line 1 "other/io.hpp" #define FASTIO // https://judge.yosupo.jp/submission/21623 namespace fastio { static constexpr uint32_t SZ = 1 << 17; char ibuf[SZ]; char obuf[SZ]; char out[100]; // pointer of ibuf, obuf 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() { memmove(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin); pil = 0; if (pir < SZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd(char &c) { do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); } void rd(string &x) { x.clear(); char c; do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); do { x += c; if (pil == pir) load(); c = ibuf[pil++]; } while (!isspace(c)); } template <typename T> void rd_real(T &x) { string s; rd(s); x = stod(s); } template <typename T> void rd_integer(T &x) { if (pil + 100 > pir) load(); char c; do c = ibuf[pil++]; while (c < '-'); bool minus = 0; if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (minus) x = -x; } } template <class T> enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> rd( T &x) { rd_integer(x); } template <class T> enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> rd(T &x) { rd_real(x); } template <class T, class U> void rd(pair<T, U> &p) { rd(p.first), rd(p.second); } template <size_t N = 0, typename T> void rd_tuple(T &t) { if constexpr (N < tuple_size<T>::value) { auto &x = get<N>(t); rd(x); rd_tuple<N + 1>(t); } } template <class... T> void rd(tuple<T...> &tpl) { rd_tuple(tpl); } template <size_t N = 0, typename T> void rd(array<T, N> &x) { for (auto &d : x) rd(d); } template <class T> void rd(vc<T> &x) { for (auto &d : x) rd(d); } void read() {} template <class H, class... T> void read(H &h, T &...t) { rd(h), read(t...); } // 先に用意(既出なら不要) inline void wt_range(const char *s, size_t n) { size_t i = 0; while (i < n) { if (por == SZ) flush(); size_t chunk = min(n - i, (size_t)(SZ - por)); memcpy(obuf + por, s + i, chunk); por += chunk; i += chunk; } } void wt(const char c) { if (por == SZ) flush(); obuf[por++] = c; } void wt(const char *s) { wt_range(s, strlen(s)); } void wt(const string &s) { wt_range(s.data(), s.size()); } template <typename T> void wt_integer(T x) { if (por > SZ - 100) flush(); if (x < 0) { obuf[por++] = '-', x = -x; } int outi; for (outi = 96; x >= 10000; outi -= 4) { memcpy(out + outi, pre.num[x % 10000], 4); x /= 10000; } if (x >= 1000) { memcpy(obuf + por, pre.num[x], 4); por += 4; } else if (x >= 100) { memcpy(obuf + por, pre.num[x] + 1, 3); por += 3; } else if (x >= 10) { int q = (x * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (x - q * 10) | '0'; por += 2; } else obuf[por++] = x | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template <typename T> inline void wt_real(T x) { char buf[64]; // 有効数字 15 桁、通常/指数を自動選択(printf("%.15g") 相当) int n = std::snprintf(buf, sizeof(buf), "%.15g", (double)x); // (任意)-0 を 0 に正規化 if (n == 2 && buf[0] == '-' && buf[1] == '0') { buf[0] = '0'; n = 1; } wt_range(buf, (size_t)n); } template <class T> enable_if_t<is_integral_v<T> || is_same_v<T, i128> || is_same_v<T, u128>> wt( T x) { wt_integer(x); } template <class T> enable_if_t<is_floating_point_v<T> || is_same_v<T, f128>> wt(T x) { wt_real(x); } inline void wt(bool b) { wt(static_cast<char>('0' + (b ? 1 : 0))); } template <class T, class U> void wt(const pair<T, U> &val) { wt(val.first); wt(' '); wt(val.second); } template <size_t N = 0, typename T> void wt_tuple(const T &t) { if constexpr (N < tuple_size<T>::value) { if constexpr (N > 0) wt(' '); wt(get<N>(t)); wt_tuple<N + 1>(t); } } template <class... T> void wt(const tuple<T...> &tpl) { wt_tuple(tpl); } template <class T, size_t S> void wt(const array<T, S> &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } template <class T> void wt(const vector<T> &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } void print() { wt('\n'); } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { wt(head); if (sizeof...(Tail)) wt(' '); print(forward<Tail>(tail)...); } // gcc expansion. called automaticall after main. void __attribute__((destructor)) _d() { flush(); } } // namespace fastio using fastio::flush; using fastio::print; using fastio::read; #if defined(LOCAL) #define HDR "[DEBUG:", __func__, __LINE__, "]" #define SHOW(...) \ SHOW_IMPL(__VA_ARGS__, SHOW8, SHOW7, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, \ SHOW1) \ (__VA_ARGS__) #define SHOW_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME #define SHOW1(x) print(HDR, #x, "=", (x)), flush() #define SHOW2(x, y) print(HDR, #x, "=", (x), #y, "=", (y)), flush() #define SHOW3(x, y, z) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z)), flush() #define SHOW4(x, y, z, w) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush() #define SHOW5(x, y, z, w, v) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v)), \ flush() #define SHOW6(x, y, z, w, v, u) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v), #u, "=", (u)), \ flush() #define SHOW7(x, y, z, w, v, u, t) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v), #u, "=", (u), #t, "=", (t)), \ flush() #define SHOW8(x, y, z, w, v, u, t, s) \ print(HDR, #x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", \ (v), #u, "=", (u), #t, "=", (t), #s, "=", (s)), \ flush() #else #define SHOW(...) #endif #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define U32(...) \ u32 __VA_ARGS__; \ read(__VA_ARGS__) #define U64(...) \ u64 __VA_ARGS__; \ read(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ read(name) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ read(name) void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } void YA(bool t = 1) { print(t ? "YA" : "TIDAK"); } void TIDAK(bool t = 1) { YA(!t); } // END: other/io.hpp #line 3 "main.cpp" // BEGIN: graph/base.hpp #line 1 "graph/base.hpp" // BEGIN: ds/hashmap.hpp #line 1 "ds/hashmap.hpp" // u64 -> Val template <typename Val> struct HashMap { // n は入れたいものの個数で ok HashMap(u32 n = 0) { build(n); } void build(u32 n) { u32 k = 8; while (k < n * 2) k *= 2; cap = k / 2, mask = k - 1; key.resize(k), val.resize(k), used.assign(k, 0); } // size を保ったまま. size=0 にするときは build すること. void clear() { used.assign(len(used), 0); cap = (mask + 1) / 2; } int size() { return len(used) / 2 - cap; } int index(const u64& k) { int i = 0; for (i = hash(k); used[i] && key[i] != k; i = (i + 1) & mask) {} return i; } Val& operator[](const u64& k) { if (cap == 0) extend(); int i = index(k); if (!used[i]) { used[i] = 1, key[i] = k, val[i] = Val{}, --cap; } return val[i]; } Val get(const u64& k, Val default_value) { int i = index(k); return (used[i] ? val[i] : default_value); } bool count(const u64& k) { int i = index(k); return used[i] && key[i] == k; } // f(key, val) template <typename F> void enumerate_all(F f) { FOR(i, len(used)) if (used[i]) f(key[i], val[i]); } private: u32 cap, mask; vc<u64> key; vc<Val> val; vc<bool> used; u64 hash(u64 x) { static const u64 FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); x += FIXED_RANDOM; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return (x ^ (x >> 31)) & mask; } void extend() { vc<pair<u64, Val>> dat; dat.reserve(len(used) / 2 - cap); FOR(i, len(used)) { if (used[i]) dat.eb(key[i], val[i]); } build(2 * len(dat)); for (auto& [a, b]: dat) (*this)[a] = b; } };// END: ds/hashmap.hpp #line 3 "graph/base.hpp" template <typename T> struct Edge { int frm, to; T cost; int id; }; template <typename T = int, bool directed = false> struct Graph { static constexpr bool is_directed = directed; int N, M; using cost_type = T; using edge_type = Edge<T>; vector<edge_type> edges; vector<int> indptr; vector<edge_type> csr_edges; vc<int> vc_deg, vc_indeg, vc_outdeg; bool prepared; class OutgoingEdges { public: OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {} const edge_type* begin() const { if (l == r) { return 0; } return &G->csr_edges[l]; } const edge_type* end() const { if (l == r) { return 0; } return &G->csr_edges[r]; } private: const Graph* G; int l, r; }; bool is_prepared() { return prepared; } Graph() : N(0), M(0), prepared(0) {} Graph(int N) : N(N), M(0), prepared(0) {} void build(int n) { N = n, M = 0; prepared = 0; edges.clear(); indptr.clear(); csr_edges.clear(); vc_deg.clear(); vc_indeg.clear(); vc_outdeg.clear(); } void add(int frm, int to, T cost = 1, int i = -1) { assert(!prepared); assert(0 <= frm && 0 <= to && to < N); if (i == -1) i = M; auto e = edge_type({frm, to, cost, i}); edges.eb(e); ++M; } #ifdef FASTIO // wt, off void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); } void read_graph(int M, bool wt = false, int off = 1) { for (int m = 0; m < M; ++m) { INT(a, b); a -= off, b -= off; if (!wt) { add(a, b); } else { T c; read(c); add(a, b, c); } } build(); } #endif void build() { assert(!prepared); prepared = true; indptr.assign(N + 1, 0); for (auto&& e: edges) { indptr[e.frm + 1]++; if (!directed) indptr[e.to + 1]++; } for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; } auto counter = indptr; csr_edges.resize(indptr.back() + 1); for (auto&& e: edges) { csr_edges[counter[e.frm]++] = e; if (!directed) csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id}); } } OutgoingEdges operator[](int v) const { assert(prepared); return {this, indptr[v], indptr[v + 1]}; } vc<int> deg_array() { if (vc_deg.empty()) calc_deg(); return vc_deg; } pair<vc<int>, vc<int>> deg_array_inout() { if (vc_indeg.empty()) calc_deg_inout(); return {vc_indeg, vc_outdeg}; } int deg(int v) { if (vc_deg.empty()) calc_deg(); return vc_deg[v]; } int in_deg(int v) { if (vc_indeg.empty()) calc_deg_inout(); return vc_indeg[v]; } int out_deg(int v) { if (vc_outdeg.empty()) calc_deg_inout(); return vc_outdeg[v]; } #ifdef FASTIO void debug() { #ifdef LOCAL print("Graph"); if (!prepared) { print("frm to cost id"); for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id); } else { print("indptr", indptr); print("frm to cost id"); FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id); } #endif } #endif vc<int> new_idx; vc<bool> used_e; // G における頂点 V[i] が、新しいグラフで i になるようにする // {G, es} // sum(deg(v)) の計算量になっていて、 // 新しいグラフの n+m より大きい可能性があるので注意 Graph<T, directed> rearrange(vc<int> V, bool keep_eid = 0) { if (len(new_idx) != N) new_idx.assign(N, -1); int n = len(V); FOR(i, n) new_idx[V[i]] = i; Graph<T, directed> G(n); vc<int> history; FOR(i, n) { for (auto&& e: (*this)[V[i]]) { if (len(used_e) <= e.id) used_e.resize(e.id + 1); if (used_e[e.id]) continue; int a = e.frm, b = e.to; if (new_idx[a] != -1 && new_idx[b] != -1) { history.eb(e.id); used_e[e.id] = 1; int eid = (keep_eid ? e.id : -1); G.add(new_idx[a], new_idx[b], e.cost, eid); } } } FOR(i, n) new_idx[V[i]] = -1; for (auto&& eid: history) used_e[eid] = 0; G.build(); return G; } Graph<T, true> to_directed_tree(int root = -1) { if (root == -1) root = 0; assert(!is_directed && prepared && M == N - 1); Graph<T, true> G1(N); vc<int> par(N, -1); auto dfs = [&](auto& dfs, int v) -> void { for (auto& e: (*this)[v]) { if (e.to == par[v]) continue; par[e.to] = v, dfs(dfs, e.to); } }; dfs(dfs, root); for (auto& e: edges) { int a = e.frm, b = e.to; if (par[a] == b) swap(a, b); assert(par[b] == a); G1.add(a, b, e.cost); } G1.build(); return G1; } HashMap<int> MP_FOR_EID; int get_eid(u64 a, u64 b) { if (len(MP_FOR_EID) == 0) { MP_FOR_EID.build(N - 1); for (auto& e: edges) { u64 a = e.frm, b = e.to; u64 k = to_eid_key(a, b); MP_FOR_EID[k] = e.id; } } return MP_FOR_EID.get(to_eid_key(a, b), -1); } u64 to_eid_key(u64 a, u64 b) { if (!directed && a > b) swap(a, b); return N * a + b; } private: void calc_deg() { assert(vc_deg.empty()); vc_deg.resize(N); for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++; } void calc_deg_inout() { assert(vc_indeg.empty()); vc_indeg.resize(N); vc_outdeg.resize(N); for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; } } }; // END: graph/base.hpp #line 5 "main.cpp" // BEGIN: ds/my_bitset.hpp #line 1 "ds/my_bitset.hpp" // https://codeforces.com/contest/914/problem/F // https://yukicoder.me/problems/no/142 // わずかに普通の bitset より遅いときもあるようだが, // 固定長にしたくないときや slice 操作が必要なときに使う struct My_Bitset { using T = My_Bitset; int N; vc<u64> dat; // x で埋める My_Bitset(int N = 0, int x = 0) : N(N) { assert(x == 0 || x == 1); u64 v = (x == 0 ? 0 : -1); dat.assign((N + 63) >> 6, v); if (N) dat.back() >>= (64 * len(dat) - N); } int size() { return N; } void resize(int size) { dat.resize((size + 63) >> 6); int remainingBits = size & 63; if (remainingBits != 0) { u64 mask = (u64(1) << remainingBits) - 1; dat.back() &= mask; } N = size; } void append(int idx, bool b) { assert(N == idx); resize(idx + 1), (*this)[idx] = b; } static T from_string(string &S) { int N = len(S); T ANS(N); FOR(i, N) ANS[i] = (S[i] == '1'); return ANS; } class Proxy { public: Proxy(vc<u64> &d, int i) : dat(d), index(i) {} operator bool() const { return (dat[index >> 6] >> (index & 63)) & 1; } Proxy &operator=(u64 value) { dat[index >> 6] &= ~(u64(1) << (index & 63)); dat[index >> 6] |= (value & 1) << (index & 63); return *this; } void flip() { dat[index >> 6] ^= (u64(1) << (index & 63)); // XOR to flip the bit } private: vc<u64> &dat; int index; }; Proxy operator[](int i) { return Proxy(dat, i); } bool operator==(const T &p) { assert(N == p.N); FOR(i, len(dat)) if (dat[i] != p.dat[i]) return false; return true; } T &operator&=(const T &p) { assert(N == p.N); FOR(i, len(dat)) dat[i] &= p.dat[i]; return *this; } T &operator|=(const T &p) { assert(N == p.N); FOR(i, len(dat)) dat[i] |= p.dat[i]; return *this; } T &operator^=(const T &p) { assert(N == p.N); FOR(i, len(dat)) dat[i] ^= p.dat[i]; return *this; } T operator&(const T &p) const { return T(*this) &= p; } T operator|(const T &p) const { return T(*this) |= p; } T operator^(const T &p) const { return T(*this) ^= p; } T operator~() const { T p = (*this); p.flip_range(0, N); return p; } void set_minus_inplace(T &other) { assert(N == other.N); FOR(i, len(dat)) dat[i] = dat[i] & (~other.dat[i]); } T set_minus(T other) { assert(N == other.N); FOR(i, len(dat)) other.dat[i] = dat[i] & (~other.dat[i]); return other; } int count() { int ans = 0; for (u64 val : dat) ans += popcnt(val); return ans; } int dot(T &p) { assert(N == p.N); int ans = 0; FOR(i, len(dat)) ans += popcnt(dat[i] & p.dat[i]); return ans; } int next(int i) { chmax(i, 0); if (i >= N) return N; int k = i >> 6; { u64 x = dat[k]; int s = i & 63; x = (x >> s) << s; if (x) return (k << 6) | lowbit(x); } FOR(idx, k + 1, len(dat)) { if (dat[idx] == 0) continue; return (idx << 6) | lowbit(dat[idx]); } return N; } int prev(int i) { chmin(i, N - 1); if (i <= -1) return -1; int k = i >> 6; if ((i & 63) < 63) { u64 x = dat[k]; x &= (u64(1) << ((i & 63) + 1)) - 1; if (x) return (k << 6) | topbit(x); --k; } FOR_R(idx, k + 1) { if (dat[idx] == 0) continue; return (idx << 6) | topbit(dat[idx]); } return -1; } My_Bitset range(int L, int R) { assert(L <= R); My_Bitset p(R - L); int rm = (R - L) & 63; FOR(rm) { p[R - L - 1] = bool((*this)[R - 1]); --R; } int n = (R - L) >> 6; int hi = L & 63; int lo = 64 - hi; int s = L >> 6; if (hi == 0) { FOR(i, n) { p.dat[i] = dat[s + i]; } } else { FOR(i, n) { p.dat[i] ^= (dat[s + i] >> hi) ^ (dat[s + i + 1] << lo); } } return p; } My_Bitset slice(int L, int R) { return range(L, R); } int count_range(int L, int R) { assert(L <= R); int cnt = 0; while ((L < R) && (L & 63)) cnt += (*this)[L++]; while ((L < R) && (R & 63)) cnt += (*this)[--R]; int l = L >> 6, r = R >> 6; FOR(i, l, r) cnt += popcnt(dat[i]); return cnt; } // [L,R) に p を代入 void assign_to_range(int L, int R, My_Bitset &p) { assert(p.N == R - L); int a = 0, b = p.N; while (L < R && (L & 63)) { (*this)[L++] = bool(p[a++]); } while (L < R && (R & 63)) { (*this)[--R] = bool(p[--b]); } // p[a:b] を [L:R] に int l = L >> 6, r = R >> 6; int s = a >> 6; int n = r - l; if (!(a & 63)) { FOR(i, n) dat[l + i] = p.dat[s + i]; } else { int hi = a & 63; int lo = 64 - hi; FOR(i, n) dat[l + i] = (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo); } } // [L,R) に p を xor void xor_to_range(int L, int R, My_Bitset &p) { assert(p.N == R - L); int a = 0, b = p.N; while (L < R && (L & 63)) { dat[L >> 6] ^= u64(p[a]) << (L & 63); ++a, ++L; } while (L < R && (R & 63)) { --b, --R; dat[R >> 6] ^= u64(p[b]) << (R & 63); } // p[a:b] を [L:R] に int l = L >> 6, r = R >> 6; int s = a >> 6; int n = r - l; if (!(a & 63)) { FOR(i, n) dat[l + i] ^= p.dat[s + i]; } else { int hi = a & 63; int lo = 64 - hi; FOR(i, n) dat[l + i] ^= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo); } } // 行列基本変形で使うやつ // p は [i:N) にしかないとして p を xor する void xor_suffix(int i, My_Bitset &p) { assert(N == p.N && 0 <= i && i < N); FOR(k, i / 64, len(dat)) { dat[k] ^= p.dat[k]; } } // [L,R) に p を and void and_to_range(int L, int R, My_Bitset &p) { assert(p.N == R - L); int a = 0, b = p.N; while (L < R && (L & 63)) { if (!p[a]) (*this)[L] = 0; a++, L++; } while (L < R && (R & 63)) { --b, --R; if (!p[b]) (*this)[R] = 0; } // p[a:b] を [L:R] に int l = L >> 6, r = R >> 6; int s = a >> 6; int n = r - l; if (!(a & 63)) { FOR(i, n) dat[l + i] &= p.dat[s + i]; } else { int hi = a & 63; int lo = 64 - hi; FOR(i, n) dat[l + i] &= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo); } } // [L,R) に p を or void or_to_range(int L, int R, My_Bitset &p) { assert(p.N == R - L); int a = 0, b = p.N; while (L < R && (L & 63)) { dat[L >> 6] |= u64(p[a]) << (L & 63); ++a, ++L; } while (L < R && (R & 63)) { --b, --R; dat[R >> 6] |= u64(p[b]) << (R & 63); } // p[a:b] を [L:R] に int l = L >> 6, r = R >> 6; int s = a >> 6; int n = r - l; if (!(a & 63)) { FOR(i, n) dat[l + i] |= p.dat[s + i]; } else { int hi = a & 63; int lo = 64 - hi; FOR(i, n) dat[l + i] |= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo); } } // [L,R) or= p[Lp:Rp) void or_to_range(int L, int R, My_Bitset &p, int Lp, int Rp) { assert(R - L == Rp - Lp); while (L < R && (L & 63)) { dat[L >> 6] |= (u64(p[Lp]) << (L & 63)), ++L, ++Lp; } while (L < R && (R & 63)) { --R, --Rp, dat[R >> 6] |= (u64(p[Rp]) << (R & 63)); } int l = L >> 6, r = R >> 6; int a = Lp, b = Rp; int s = a >> 6; int n = r - l; if (!(a & 63)) { FOR(i, n) dat[l + i] |= p.dat[s + i]; } else { int hi = a & 63, lo = 64 - hi; int pw = (b + 63) >> 6; FOR(i, n) { u64 w0 = (s + i < pw ? (p.dat[s + i] >> hi) : 0); u64 w1 = (s + i + 1 < pw ? (p.dat[s + i + 1] << lo) : 0); dat[l + i] |= (w0 | w1); } } } // 行列基本変形で使うやつ // p は [i:N) にしかないとして p を or する void or_suffix(int i, My_Bitset &p) { assert(N == p.N && 0 <= i && i < N); FOR(k, i / 64, len(dat)) { dat[k] |= p.dat[k]; } } // [L,R) を 1 に変更 void set_range(int L, int R) { while (L < R && (L & 63)) { set(L++); } while (L < R && (R & 63)) { set(--R); } FOR(i, L >> 6, R >> 6) dat[i] = u64(-1); } // [L,R) を 1 に変更 void reset_range(int L, int R) { while (L < R && (L & 63)) { reset(L++); } while (L < R && (R & 63)) { reset(--R); } FOR(i, L >> 6, R >> 6) dat[i] = u64(0); } // [L,R) を flip void flip_range(int L, int R) { while (L < R && (L & 63)) { flip(L++); } while (L < R && (R & 63)) { flip(--R); } FOR(i, L >> 6, R >> 6) dat[i] ^= u64(-1); } // bitset に仕様を合わせる void set(int i) { (*this)[i] = 1; } void reset(int i) { (*this)[i] = 0; } void flip(int i) { (*this)[i].flip(); } void set() { fill(all(dat), u64(-1)); resize(N); } void reset() { fill(all(dat), 0); } void flip() { FOR(i, len(dat) - 1) { dat[i] = u64(-1) ^ dat[i]; } int i = len(dat) - 1; FOR(k, 64) { if (64 * i + k >= size()) break; flip(64 * i + k); } } bool any() { FOR(i, len(dat)) { if (dat[i]) return true; } return false; } bool ALL() { dat.resize((N + 63) >> 6); int r = N & 63; if (r != 0) { u64 mask = (u64(1) << r) - 1; if (dat.back() != mask) return 0; } for (int i = 0; i < N / 64; ++i) if (dat[i] != u64(-1)) return false; return true; } // bs[i]==true であるような i 全体 vc<int> collect_idx() { vc<int> I; FOR(i, N) if ((*this)[i]) I.eb(i); return I; } bool is_subset(T &other) { assert(len(other) == N); FOR(i, len(dat)) { u64 a = dat[i], b = other.dat[i]; if ((a & b) != a) return false; } return true; } int _Find_first() { return next(0); } int _Find_next(int p) { return next(p + 1); } template <typename F> void enumerate(int L, int R, F f) { if (L >= size()) return; int p = ((*this)[L] ? L : _Find_next(L)); while (p < R) { f(p); p = _Find_next(p); } } static string TO_STR[256]; string to_string() const { if (TO_STR[0].empty()) precompute(); string S; for (auto &x : dat) { FOR(i, 8) S += TO_STR[(x >> (8 * i) & 255)]; } S.resize(N); return S; } static void precompute() { FOR(s, 256) { string x; FOR(i, 8) x += '0' + (s >> i & 1); TO_STR[s] = x; } } }; string My_Bitset::TO_STR[256];// END: ds/my_bitset.hpp #line 6 "main.cpp" void solve() { LL(N, M, K, s, t); if (K > N + N) { ll t = (K - N - N) / 2; K -= t * 2; } Graph<int, 0> G(N); G.read_graph(M); using BS = My_Bitset; auto calc = [&](int v, bool rev) -> vc<BS> { vv(bool, dp, K + 1, N); dp[0][v] = 1; FOR(i, K) { FOR(v, N) { if (!dp[i][v]) continue; for (auto& e : G[v]) dp[i + 1][e.to] = 1; } } vc<BS> ANS(N, BS(K + 1)); if (!rev) { FOR(i, K + 1) FOR(v, N) ANS[v][i] = dp[i][v]; } else { FOR(i, K) FOR(v, N) ANS[v][i] = dp[K - 1 - i][v]; } return ANS; }; --s, --t; auto dp1 = calc(s, false); auto dp2 = calc(t, true); if (dp1[t][K]) return Yes(); vv(bool, adj, N, N); for (auto& e : G.edges) { adj[e.frm][e.to] = 1; adj[e.to][e.frm] = 1; } FOR(a, N) { FOR(b, N) { if (a == b) continue; if (adj[a][b]) continue; if ((dp1[a] & dp2[b]).any()) return Yes(); } } return No(); } signed main() { solve(); } // END: main.cpp