// BEGIN: main.cpp #line 1 "main.cpp" // BEGIN: my_template.hpp #line 1 "my_template.hpp" #if defined(LOCAL) #include #else #if defined(__GNUC__) #include #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,popcnt") #endif #include 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 constexpr T infty = 0; template <> constexpr int infty = 1'010'000'000; template <> constexpr ll infty = 2'020'000'000'000'000'000; template <> constexpr u32 infty = infty; template <> constexpr u64 infty = infty; template <> constexpr i128 infty = i128(infty) * 2'000'000'000'000'000'000; template <> constexpr double infty = numeric_limits::infinity(); template <> constexpr long double infty = numeric_limits::infinity(); using pi = pair; using vi = vector; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using pq_max = priority_queue; template using pq_min = priority_queue, greater>; #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__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 T kth_bit(int k) { return T(1) << k; } template bool has_kth_bit(T x, int k) { return x >> k & 1; } template 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 struct all_subset { static_assert(is_unsigned::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 T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); } template T ceil(T x, T y) { return floor(x + y - 1, y); } template T bmod(T x, T y) { return x - y * floor(x, y); } template pair 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 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 inline long long LB(const C &c, const T &x) { return lower_bound(c.begin(), c.end(), x) - c.begin(); } template 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 T POP(deque &que) { T a = que.front(); que.pop_front(); return a; } template T POP(priority_queue &que) { T a = que.top(); que.pop(); return a; } template T POP(vc &que) { T a = que.back(); que.pop_back(); return a; } template 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 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 inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } // ? は -1 vc s_to_vi(const string &S, char first_char) { vc A(S.size()); FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); } return A; } template vc cumsum(const vc &A, int off = 1) { int N = A.size(); vc 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 vc argsort(const vc &A) { vc 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 vc rearrange(const vc &A, const vc &I) { vc B(len(I)); FOR(i, len(I)) B[i] = A[I[i]]; return B; } template void concat(vc &first, const Vectors &...others) { vc &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 void rd_real(T &x) { string s; rd(s); x = stod(s); } template 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::value || is_same_v) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed::value || is_same_v) { if (minus) x = -x; } } template enable_if_t || is_same_v || is_same_v> rd( T &x) { rd_integer(x); } template enable_if_t || is_same_v> rd(T &x) { rd_real(x); } template void rd(pair &p) { rd(p.first), rd(p.second); } template void rd_tuple(T &t) { if constexpr (N < tuple_size::value) { auto &x = get(t); rd(x); rd_tuple(t); } } template void rd(tuple &tpl) { rd_tuple(tpl); } template void rd(array &x) { for (auto &d : x) rd(d); } template void rd(vc &x) { for (auto &d : x) rd(d); } void read() {} template 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 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 inline void wt_real(T x) { char buf[64]; int n = std::snprintf(buf, sizeof(buf), "%.15g", (double)x); if (n == 2 && buf[0] == '-' && buf[1] == '0') { buf[0] = '0'; n = 1; } wt_range(buf, (size_t)n); } template enable_if_t || is_same_v || is_same_v> wt( T x) { wt_integer(x); } template enable_if_t || is_same_v> wt(T x) { wt_real(x); } inline void wt(bool b) { wt(static_cast('0' + (b ? 1 : 0))); } template void wt(const pair &val) { wt(val.first); wt(' '); wt(val.second); } template void wt_tuple(const T &t) { if constexpr (N < tuple_size::value) { if constexpr (N > 0) wt(' '); wt(get(t)); wt_tuple(t); } } template void wt(const tuple &tpl) { wt_tuple(tpl); } template void wt(const array &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } template void wt(const vector &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } void print() { wt('\n'); } template void print(Head &&head, Tail &&...tail) { wt(head); if (sizeof...(Tail)) wt(' '); print(forward(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 name(size); \ read(name) #define VV(type, name, h, w) \ vector> name(h, vector(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 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 void enumerate_all(F f) { FOR(i, len(used)) if (used[i]) f(key[i], val[i]); } private: u32 cap, mask; vc key; vc val; vc 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> 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 struct Edge { int frm, to; T cost; int id; }; template struct Graph { static constexpr bool is_directed = directed; int N, M; using cost_type = T; using edge_type = Edge; vector edges; vector indptr; vector csr_edges; vc 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 deg_array() { if (vc_deg.empty()) calc_deg(); return vc_deg; } pair, vc> 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 new_idx; vc used_e; // G における頂点 V[i] が、新しいグラフで i になるようにする // {G, es} // sum(deg(v)) の計算量になっていて、 // 新しいグラフの n+m より大きい可能性があるので注意 Graph rearrange(vc 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 G(n); vc 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 to_directed_tree(int root = -1) { if (root == -1) root = 0; assert(!is_directed && prepared && M == N - 1); Graph G1(N); vc 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 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" /* だいたいわかったけど解く時間はなかったです X を決めたときの判定を考える 有向辺ごとに - いつまで使える - いつから使える というような情報が決まる で? 両方向ok、一方向ok、両方向ng、全部ありえます 単調性はあるので二分探索でよい あー そんなことはなくて ab: (a-X)/(b-a) から b->a: (X-b)/(b-a) まで bb は常に使える, b->a は期日あり Xa は使用不可能, a->b は使用開始時期あり */ /* 一度でも X 以上になったら、ずっと X 以上です 使用開始時期のある辺を通ったら、以降ずっとそうです (1) a=b, X 以下:常に使える辺 (2) a=b, X より大きい:捨ててよい (3) ab は常に使える, b->a には期日がある (4) aa は禁止, a->b は使用開始時期あり 一度でも (4) を使ったら、あとは (4) だけです まずは (1), (3) について何らかの計算をします (1) も (3) と見なしてよい mod 2 を固定 最速到達時刻を持つ じゃあこれは単に N 頂点の dijkstra みたいな感じでよさそう */ void solve() { LL(N, M); // VEC(ll, H, N + 1); VEC(ll, H, N); H.eb(0); Graph G(N + 1); G.read_graph(M); auto I = argsort(H); auto check = [&](ll X) -> bool { H[N] = X; auto can = [&](int u, int v, ll t) -> bool { if (t == 0) { assert(u == N); return 1; } if (u == N || v == N) return 0; i128 a = H[u], b = H[v]; a -= X, b -= X; // a/t, b/(t+1) return (t + 1) * a <= b * t; }; // 結局、まずは H <= X となる範囲で dijkstra ということでよい? vv(ll, dp, 2, N + 1, infty); { deque que; auto upd = [&](int v, int d) -> void { int k = d & 1; if (chmin(dp[k][v], d)) que.eb(k, v); }; upd(N, 0); while (len(que)) { auto [k, v] = POP(que); for (auto& e : G[v]) { // if (H[e.to] > X) continue; ll t = dp[k][v]; if (can(e.frm, e.to, t)) upd(e.to, t + 1); } } } // とりあえずここまでできた // 可能ならば往復したあと large に突入 // うそで、small 内で往復して small 内を歩くというこtもある vi DP(N + 1, -infty); for (auto& e : G[N]) { if (H[e.to] >= X) chmax(DP[e.to], 1); } { // pq_max que; auto upd = [&](int v, ll d) -> void { // if (chmax(DP[v], d)) que.emplace(d, v); chmax(DP[v], d); }; auto max_can = [&](int u, int v) -> ll { assert(u != N && v != N); ll a = H[u], b = H[v]; if (a > X || b > X) return -1; a -= X, b -= X; assert(a <= 0 && b <= 0); // a/t, b/(t+1) // return (t + 1) * a <= b * t; if (a == b) { return infty; } if (a < b) { return infty; } return floor(-a, a - b); }; FOR(k, 2) { FOR(v, N) { if (dp[k][v] == infty) continue; ll s = dp[k][v]; ll t = s; for (auto& e : G[v]) { if (H[e.to] > X) continue; if (e.to == N) continue; // 往復できる時刻いっぱいまで往復する // ll k = binary_search( // [&](ll k) -> bool { // return can(e.frm, e.to, t + 2 * k) && // can(e.to, e.frm, t + 2 * k + 1); // }, // -1, infty, 0); ll k = infty / 2; { ll t1 = max_can(e.frm, e.to); t1 -= t; if (t1 < 0) { k = -1; } else { chmin(k, t1 / 2); } } { ll t2 = max_can(e.to, e.frm); t2 -= t + 1; if (t2 < 0) { k = -1; } else { chmin(k, floor(t2, 2)); } } // ll god = binary_search( // [&](ll k) -> bool { // return can(e.frm, e.to, t + 2 * k) && // can(e.to, e.frm, t + 2 * k + 1); // }, // -1, infty, 0); SHOW(e.frm, e.to, t, k); t += 2 * k + 2; chmin(t, infty); } SHOW(v, t); // for (auto& e : G[v]) { // // if (H[e.to] <= X) continue; // // SHOW(e.frm, e.to, s, t); // // if (H[e.to] > X && can(e.frm, e.to, t)) upd(e.to, t + 1); // if (can(e.frm, e.to, t)) upd(e.to, t + 1); // } chmax(DP[v], t); } } // H について昇順に for (auto& v : I) { // if (H[v] <= X) continue; ll d = DP[v]; if (d < 0) continue; for (auto& e : G[v]) { // if (can(e.frm, e.to, d)) upd(e.to, d + 1); // if (H[e.to] > X && can(e.frm, e.to, d)) upd(e.to, d + 1); if (can(e.frm, e.to, d)) upd(e.to, d + 1); } } } SHOW(X); SHOW(dp[0]); SHOW(dp[1]); SHOW(DP); // 確認 FOR(v, N + 1) { ll d = min(dp[0][v], dp[1][v]); if (d == infty && DP[v] < 0) { SHOW(v); return 0; } } return 1; }; // SHOW(check(11)); // return; ll ANS = binary_search(check, infty, -1); print(ANS); } signed main() { INT(T); FOR(T) solve(); } // END: main.cpp