firiexp's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub firiexp/library

:heavy_check_mark: test/aoj0275_static_bitset.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0275"

#include <algorithm>
#include <limits>
#include <queue>
#include <vector>
using namespace std;

using ll = long long;

template<class T> constexpr T INF = numeric_limits<T>::max() / 32 * 15 + 208;

#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>

#include "../util/fastio.cpp"
#include "../graph/dijkstra.cpp"
#include "../datastructure/static_bitset.cpp"

int main() {
    Scanner in;
    Printer out;

    int n, m;
    in.read(n, m);
    vector<vector<edge<ll>>> g(n);
    vector<int> u(m), v(m), c(m);
    for (int i = 0; i < m; ++i) {
        in.read(u[i], v[i], c[i]);
        --u[i], --v[i];
        g[u[i]].emplace_back(v[i], c[i]);
        g[v[i]].emplace_back(u[i], c[i]);
    }

    int s, t, q;
    in.read(s, t, q);
    --s, --t;

    auto ds = dijkstra(s, g);
    auto dt = dijkstra(t, g);
    ll best = ds[t];

    vector<int> active_id(n, -1);
    vector<int> active_vertices;
    active_vertices.reserve(n);
    for (int i = 0; i < n; ++i) {
        if (ds[i] != INF<ll> && dt[i] != INF<ll> && ds[i] + dt[i] == best) {
            active_id[i] = (int)active_vertices.size();
            active_vertices.push_back(i);
        }
    }

    int k = active_vertices.size();
    vector<vector<int>> dag(k);
    vector<int> indeg(k, 0);
    for (int i = 0; i < m; ++i) {
        int a = active_id[u[i]];
        int b = active_id[v[i]];
        if (a != -1 && b != -1 && ds[u[i]] + c[i] == ds[v[i]]) {
            dag[a].push_back(b);
            ++indeg[b];
        }
        if (a != -1 && b != -1 && ds[v[i]] + c[i] == ds[u[i]]) {
            dag[b].push_back(a);
            ++indeg[a];
        }
    }

    vector<int> ord;
    ord.reserve(k);
    queue<int> que;
    for (int i = 0; i < k; ++i) if (indeg[i] == 0) que.push(i);
    while (!que.empty()) {
        int x = que.front();
        que.pop();
        ord.push_back(x);
        for (int to : dag[x]) {
            if (--indeg[to] == 0) que.push(to);
        }
    }

    vector<pair<int, int>> qs(q);
    for (auto &[a, b] : qs) {
        in.read(a, b);
        --a, --b;
    }

    constexpr int BLOCK = 2048;
    vector<StaticBitset<BLOCK>> dp(k);
    vector<unsigned char> active(k, 0);
    for (int l = 0; l < q; l += BLOCK) {
        int r = min(q, l + BLOCK);
        for (auto &bs : dp) bs.reset();
        fill(active.begin(), active.end(), 0);
        for (int i = l; i < r; ++i) {
            int a = active_id[qs[i].first];
            if (a != -1) {
                dp[a].set(i - l);
                active[a] = 1;
            }
        }
        for (int vtx : ord) {
            if (!active[vtx]) continue;
            for (int to : dag[vtx]) {
                dp[to] |= dp[vtx];
                active[to] = 1;
            }
        }
        for (int i = l; i < r; ++i) {
            int b = active_id[qs[i].second];
            out.println(b != -1 && dp[b].test(i - l) ? "Yes" : "No");
        }
    }
    return 0;
}
#line 1 "test/aoj0275_static_bitset.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0275"

#include <algorithm>
#include <limits>
#include <queue>
#include <vector>
using namespace std;

using ll = long long;

template<class T> constexpr T INF = numeric_limits<T>::max() / 32 * 15 + 208;

#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>

#line 1 "util/fastio.cpp"
using namespace std;

extern "C" int fileno(FILE *);
extern "C" int isatty(int);

template<class T, class = void>
struct is_fastio_range : false_type {};

template<class T>
struct is_fastio_range<T, void_t<decltype(declval<T &>().begin()), decltype(declval<T &>().end())>> : true_type {};

template<class T, class = void>
struct has_fastio_value : false_type {};

template<class T>
struct has_fastio_value<T, void_t<decltype(declval<const T &>().value())>> : true_type {};

struct FastIoDigitTable {
    char num[40000];

    constexpr FastIoDigitTable() : num() {
        for (int i = 0; i < 10000; ++i) {
            int x = i;
            for (int j = 3; j >= 0; --j) {
                num[i * 4 + j] = char('0' + x % 10);
                x /= 10;
            }
        }
    }
};

struct Scanner {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE + 1];
    int idx, size;
    bool interactive;

    Scanner() : idx(0), size(0), interactive(isatty(fileno(stdin))) {}

    inline void load() {
        int len = size - idx;
        memmove(buf, buf + idx, len);
        if (interactive) {
            if (fgets(buf + len, BUFSIZE + 1 - len, stdin)) size = len + (int)strlen(buf + len);
            else size = len;
        } else {
            size = len + (int)fread(buf + len, 1, BUFSIZE - len, stdin);
        }
        idx = 0;
        buf[size] = 0;
    }

    inline void ensure() {
        if (idx + OFFSET > size) load();
    }

    inline void ensure_interactive() {
        if (idx == size) load();
    }

    inline char skip() {
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            return buf[idx++];
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        return buf[idx++];
    }

    template<class T, typename enable_if<is_integral<T>::value, int>::type = 0>
    void read(T &x) {
        if (interactive) {
            char c = skip();
            bool neg = false;
            if constexpr (is_signed<T>::value) {
                if (c == '-') {
                    neg = true;
                    ensure_interactive();
                    c = buf[idx++];
                }
            }
            x = 0;
            while (c >= '0') {
                x = x * 10 + (c & 15);
                ensure_interactive();
                c = buf[idx++];
            }
            if constexpr (is_signed<T>::value) {
                if (neg) x = -x;
            }
            return;
        }
        char c = skip();
        bool neg = false;
        if constexpr (is_signed<T>::value) {
            if (c == '-') {
                neg = true;
                c = buf[idx++];
            }
        }
        x = 0;
        while (c >= '0') {
            x = x * 10 + (c & 15);
            c = buf[idx++];
        }
        if constexpr (is_signed<T>::value) {
            if (neg) x = -x;
        }
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void read(T &x) {
        long long v;
        read(v);
        x = T(v);
    }

    template<class Head, class Next, class... Tail>
    void read(Head &head, Next &next, Tail &...tail) {
        read(head);
        read(next, tail...);
    }

    template<class T, class U>
    void read(pair<T, U> &p) {
        read(p.first, p.second);
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void read(T &a) {
        for (auto &x : a) read(x);
    }

    void read(char &c) {
        c = skip();
    }

    void read(string &s) {
        s.clear();
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            while (true) {
                int start = idx;
                while (idx < size && buf[idx] > ' ') ++idx;
                s.append(buf + start, idx - start);
                if (idx < size) break;
                load();
                if (size == 0) break;
            }
            if (idx < size) ++idx;
            return;
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        while (true) {
            int start = idx;
            while (idx < size && buf[idx] > ' ') ++idx;
            s.append(buf + start, idx - start);
            if (idx < size) break;
            load();
        }
        if (idx < size) ++idx;
    }
};

struct Printer {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE];
    int idx;
    bool interactive;
    inline static constexpr FastIoDigitTable table{};

    Printer() : idx(0), interactive(isatty(fileno(stdout))) {}
    ~Printer() { flush(); }

    inline void flush() {
        if (idx) {
            fwrite(buf, 1, idx, stdout);
            idx = 0;
        }
    }

    inline void pc(char c) {
        if (idx > BUFSIZE - OFFSET) flush();
        buf[idx++] = c;
        if (interactive && c == '\n') flush();
    }

    inline void print_range(const char *s, size_t n) {
        size_t pos = 0;
        while (pos < n) {
            if (idx == BUFSIZE) flush();
            size_t chunk = min(n - pos, (size_t)(BUFSIZE - idx));
            memcpy(buf + idx, s + pos, chunk);
            idx += (int)chunk;
            pos += chunk;
        }
    }

    void print(const char *s) {
        print_range(s, strlen(s));
    }

    void print(const string &s) {
        print_range(s.data(), s.size());
    }

    void print(char c) {
        pc(c);
    }

    void print(bool b) {
        pc(char('0' + (b ? 1 : 0)));
    }

    template<class T, typename enable_if<is_integral<T>::value && !is_same<T, bool>::value, int>::type = 0>
    void print(T x) {
        if (idx > BUFSIZE - 100) flush();
        using U = typename make_unsigned<T>::type;
        U y;
        if constexpr (is_signed<T>::value) {
            if (x < 0) {
                buf[idx++] = '-';
                y = U(0) - static_cast<U>(x);
            } else {
                y = static_cast<U>(x);
            }
        } else {
            y = x;
        }
        if (y == 0) {
            buf[idx++] = '0';
            return;
        }
        static constexpr int TMP_SIZE = sizeof(U) * 10 / 4;
        char tmp[TMP_SIZE];
        int pos = TMP_SIZE;
        while (y >= 10000) {
            pos -= 4;
            memcpy(tmp + pos, table.num + (y % 10000) * 4, 4);
            y /= 10000;
        }
        if (y >= 1000) {
            memcpy(buf + idx, table.num + (y << 2), 4);
            idx += 4;
        } else if (y >= 100) {
            memcpy(buf + idx, table.num + (y << 2) + 1, 3);
            idx += 3;
        } else if (y >= 10) {
            unsigned q = (unsigned(y) * 205) >> 11;
            buf[idx] = char('0' + q);
            buf[idx + 1] = char('0' + (unsigned(y) - q * 10));
            idx += 2;
        } else {
            buf[idx++] = char('0' + y);
        }
        memcpy(buf + idx, tmp + pos, TMP_SIZE - pos);
        idx += TMP_SIZE - pos;
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void print(const T &x) {
        print(x.value());
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void print(const T &a) {
        bool first = true;
        for (auto &&x : a) {
            if (!first) pc(' ');
            first = false;
            print(x);
        }
    }

    template<class T>
    void println(const T &x) {
        print(x);
        pc('\n');
    }

    template<class Head, class... Tail>
    void println(const Head &head, const Tail &...tail) {
        print(head);
        ((pc(' '), print(tail)), ...);
        pc('\n');
    }

    void println() {
        pc('\n');
    }
};

template<class T>
Scanner &operator>>(Scanner &in, T &x) {
    in.read(x);
    return in;
}

template<class T>
Printer &operator<<(Printer &out, const T &x) {
    out.print(x);
    return out;
}

/**
 * @brief 高速入出力(Fast IO)
 */
#line 1 "graph/dijkstra_common.cpp"



template <typename T>
struct edge {
    int from, to;
    T cost;

    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};

template <typename T>
struct DijkstraPriorityQueue {
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<>> q;

    bool empty() const { return q.empty(); }

    void push(T cost, int v) {
        q.emplace(cost, v);
    }

    pair<T, int> pop() {
        auto res = q.top();
        q.pop();
        return res;
    }
};

template <typename T, class Queue, class OnRelax>
vector<T> dijkstra_internal(int s, const vector<vector<edge<T>>> &G, Queue &Q, OnRelax on_relax) {
    int n = (int)G.size();
    vector<T> dist(n, INF<T>);
    dist[s] = 0;
    Q.push(T(0), s);
    while (!Q.empty()) {
        auto [cost, v] = Q.pop();
        if (dist[v] < cost) continue;
        for (auto &&e : G[v]) {
            T nxt = cost + e.cost;
            if (dist[e.to] <= nxt) continue;
            dist[e.to] = nxt;
            on_relax(v, e);
            Q.push(nxt, e.to);
        }
    }
    return dist;
}

template <typename T, class Queue>
vector<T> dijkstra_internal(int s, const vector<vector<edge<T>>> &G, Queue &Q) {
    return dijkstra_internal(s, G, Q, [](int, const edge<T> &) {});
}


#line 2 "graph/dijkstra.cpp"

template <typename T>
vector<T> dijkstra(int s, const vector<vector<edge<T>>> &G) {
    DijkstraPriorityQueue<T> Q;
    return dijkstra_internal(s, G, Q);
}

/**
 * @brief Dijkstra法
 */
#line 1 "datastructure/static_bitset.cpp"
#if defined(__x86_64__) || defined(_M_X64)
#include <immintrin.h>
#endif

using namespace std;

namespace static_bitset_detail {
using Word = unsigned long long;
constexpr int avx2_threshold_words = 8;

#if defined(__x86_64__) || defined(_M_X64)
__attribute__((target("avx2"))) inline void and_assign_avx2(Word *dst, const Word *src, int m) {
    int i = 0;
    for (; i + 4 <= m; i += 4) {
        __m256i x = _mm256_loadu_si256((const __m256i *)(dst + i));
        __m256i y = _mm256_loadu_si256((const __m256i *)(src + i));
        _mm256_storeu_si256((__m256i *)(dst + i), _mm256_and_si256(x, y));
    }
    for (; i < m; ++i) dst[i] &= src[i];
}

__attribute__((target("avx2"))) inline void or_assign_avx2(Word *dst, const Word *src, int m) {
    int i = 0;
    for (; i + 4 <= m; i += 4) {
        __m256i x = _mm256_loadu_si256((const __m256i *)(dst + i));
        __m256i y = _mm256_loadu_si256((const __m256i *)(src + i));
        _mm256_storeu_si256((__m256i *)(dst + i), _mm256_or_si256(x, y));
    }
    for (; i < m; ++i) dst[i] |= src[i];
}

__attribute__((target("avx2"))) inline void xor_assign_avx2(Word *dst, const Word *src, int m) {
    int i = 0;
    for (; i + 4 <= m; i += 4) {
        __m256i x = _mm256_loadu_si256((const __m256i *)(dst + i));
        __m256i y = _mm256_loadu_si256((const __m256i *)(src + i));
        _mm256_storeu_si256((__m256i *)(dst + i), _mm256_xor_si256(x, y));
    }
    for (; i < m; ++i) dst[i] ^= src[i];
}

__attribute__((target("avx2"))) inline bool any_avx2(const Word *src, int m) {
    __m256i acc = _mm256_setzero_si256();
    int i = 0;
    for (; i + 4 <= m; i += 4) {
        acc = _mm256_or_si256(acc, _mm256_loadu_si256((const __m256i *)(src + i)));
    }
    if (!_mm256_testz_si256(acc, acc)) return true;
    for (; i < m; ++i) {
        if (src[i]) return true;
    }
    return false;
}

__attribute__((target("avx2"))) inline bool all_full_words_avx2(const Word *src, int m) {
    const __m256i ones = _mm256_set1_epi64x(-1);
    int i = 0;
    for (; i + 4 <= m; i += 4) {
        __m256i x = _mm256_loadu_si256((const __m256i *)(src + i));
        __m256i eq = _mm256_cmpeq_epi64(x, ones);
        if (_mm256_movemask_epi8(eq) != -1) return false;
    }
    for (; i < m; ++i) {
        if (src[i] != ~0ULL) return false;
    }
    return true;
}

inline bool has_avx2() {
    static const bool cached = __builtin_cpu_supports("avx2");
    return cached;
}
#endif
}  // namespace static_bitset_detail

template<int N>
class StaticBitset {
    static_assert(N >= 0);
    static constexpr int B = 64;
    static constexpr int M = (N + B - 1) >> 6;
    using Word = unsigned long long;

    Word a[M > 0 ? M : 1];

    static int popcount(Word x) {
        return __builtin_popcountll(x);
    }
    static int ctz(Word x) {
        return __builtin_ctzll(x);
    }
    static int clz(Word x) {
        return __builtin_clzll(x);
    }
    static constexpr Word tail_mask() {
        return N % B ? ((1ULL << (N % B)) - 1) : ~0ULL;
    }
    void normalize() {
        if constexpr (M > 0) a[M - 1] &= tail_mask();
    }

public:
    explicit StaticBitset(bool x = false) : a{} {
        if (x) set();
    }

    int size() const { return N; }
    bool empty() const { return N == 0; }

    void reset() {
        fill(a, a + M, 0);
    }
    void set() {
        fill(a, a + M, ~0ULL);
        normalize();
    }
    void flip() {
        for (Word &x : a) x = ~x;
        normalize();
    }

    bool test(int k) const {
        return (a[k >> 6] >> (k & 63)) & 1ULL;
    }
    void set(int k) {
        a[k >> 6] |= 1ULL << (k & 63);
    }
    void reset(int k) {
        a[k >> 6] &= ~(1ULL << (k & 63));
    }
    void flip(int k) {
        a[k >> 6] ^= 1ULL << (k & 63);
    }
    void assign(int k, bool x) {
        if (x) set(k);
        else reset(k);
    }

    bool any() const {
        const Word *p = a;
#if defined(__x86_64__) || defined(_M_X64)
        if constexpr (M >= static_bitset_detail::avx2_threshold_words) {
            if (static_bitset_detail::has_avx2()) return static_bitset_detail::any_avx2(p, M);
        }
#endif
        for (int i = 0; i < M; ++i) {
            if (p[i]) return true;
        }
        return false;
    }
    bool none() const { return !any(); }
    bool all() const {
        if constexpr (M == 0) return true;
        const Word *p = a;
#if defined(__x86_64__) || defined(_M_X64)
        if constexpr (M > 1 + static_bitset_detail::avx2_threshold_words) {
            if (static_bitset_detail::has_avx2()) {
                if (!static_bitset_detail::all_full_words_avx2(p, M - 1)) return false;
                return p[M - 1] == tail_mask();
            }
        }
#endif
        for (int i = 0; i + 1 < M; ++i) {
            if (p[i] != ~0ULL) return false;
        }
        return p[M - 1] == tail_mask();
    }
    int count() const {
        const Word *p = a;
        int res = 0;
        int i = 0;
        for (; i + 4 <= M; i += 4) {
            res += popcount(p[i + 0]) + popcount(p[i + 1]) + popcount(p[i + 2]) + popcount(p[i + 3]);
        }
        for (; i < M; ++i) res += popcount(p[i]);
        return res;
    }

    int find_first() const {
        const Word *p = a;
        for (int i = 0; i < M; ++i) {
            if (p[i]) return (i << 6) + ctz(p[i]);
        }
        return -1;
    }
    int find_last() const {
        const Word *p = a;
        for (int i = M - 1; i >= 0; --i) {
            if (p[i]) return (i << 6) + (B - 1 - clz(p[i]));
        }
        return -1;
    }
    int find_next(int k) const {
        ++k;
        if (k >= N) return -1;
        const Word *p = a;
        int i = k >> 6;
        Word x = p[i] & (~0ULL << (k & 63));
        if (x) return (i << 6) + ctz(x);
        for (++i; i < M; ++i) {
            if (p[i]) return (i << 6) + ctz(p[i]);
        }
        return -1;
    }
    int find_prev(int k) const {
        --k;
        if (k < 0) return -1;
        const Word *p = a;
        int i = k >> 6;
        int rem = k & 63;
        Word mask = rem == B - 1 ? ~0ULL : ((1ULL << (rem + 1)) - 1);
        Word x = p[i] & mask;
        if (x) return (i << 6) + (B - 1 - clz(x));
        for (--i; i >= 0; --i) {
            if (p[i]) return (i << 6) + (B - 1 - clz(p[i]));
        }
        return -1;
    }

    StaticBitset &operator&=(const StaticBitset &r) {
        Word *p = a;
        const Word *q = r.a;
#if defined(__x86_64__) || defined(_M_X64)
        if constexpr (M >= static_bitset_detail::avx2_threshold_words) {
            if (static_bitset_detail::has_avx2()) {
                static_bitset_detail::and_assign_avx2(p, q, M);
                return *this;
            }
        }
#endif
        for (int i = 0; i < M; ++i) p[i] &= q[i];
        return *this;
    }
    StaticBitset &operator|=(const StaticBitset &r) {
        Word *p = a;
        const Word *q = r.a;
#if defined(__x86_64__) || defined(_M_X64)
        if constexpr (M >= static_bitset_detail::avx2_threshold_words) {
            if (static_bitset_detail::has_avx2()) {
                static_bitset_detail::or_assign_avx2(p, q, M);
                return *this;
            }
        }
#endif
        for (int i = 0; i < M; ++i) p[i] |= q[i];
        return *this;
    }
    StaticBitset &operator^=(const StaticBitset &r) {
        Word *p = a;
        const Word *q = r.a;
#if defined(__x86_64__) || defined(_M_X64)
        if constexpr (M >= static_bitset_detail::avx2_threshold_words) {
            if (static_bitset_detail::has_avx2()) {
                static_bitset_detail::xor_assign_avx2(p, q, M);
                normalize();
                return *this;
            }
        }
#endif
        for (int i = 0; i < M; ++i) p[i] ^= q[i];
        normalize();
        return *this;
    }

    friend StaticBitset operator&(StaticBitset l, const StaticBitset &r) { return l &= r; }
    friend StaticBitset operator|(StaticBitset l, const StaticBitset &r) { return l |= r; }
    friend StaticBitset operator^(StaticBitset l, const StaticBitset &r) { return l ^= r; }

    StaticBitset &operator<<=(int s) {
        if (s <= 0 || N == 0) return *this;
        if (s >= N) {
            reset();
            return *this;
        }
        if (s == 1) {
            Word carry = 0;
            for (int i = 0; i < M; ++i) {
                Word next = a[i] >> (B - 1);
                a[i] = (a[i] << 1) | carry;
                carry = next;
            }
            normalize();
            return *this;
        }
        int block = s >> 6;
        int rem = s & 63;
        if (rem == 0) {
            memmove(a + block, a, sizeof(Word) * (M - block));
        } else {
            for (int i = M - 1; i > block; --i) {
                a[i] = (a[i - block] << rem) | (a[i - block - 1] >> (B - rem));
            }
            a[block] = a[0] << rem;
        }
        fill(a, a + block, 0);
        normalize();
        return *this;
    }
    StaticBitset &operator>>=(int s) {
        if (s <= 0 || N == 0) return *this;
        if (s >= N) {
            reset();
            return *this;
        }
        if (s == 1) {
            for (int i = 0; i < M; ++i) {
                Word next = i + 1 < M ? (a[i + 1] << (B - 1)) : 0;
                a[i] = (a[i] >> 1) | next;
            }
            normalize();
            return *this;
        }
        int block = s >> 6;
        int rem = s & 63;
        if (rem == 0) {
            memmove(a, a + block, sizeof(Word) * (M - block));
        } else {
            int last = M - block - 1;
            for (int i = 0; i < last; ++i) {
                a[i] = (a[i + block] >> rem) | (a[i + block + 1] << (B - rem));
            }
            a[last] = a[M - 1] >> rem;
        }
        fill(a + (M - block), a + M, 0);
        normalize();
        return *this;
    }

    friend StaticBitset operator<<(StaticBitset l, int s) { return l <<= s; }
    friend StaticBitset operator>>(StaticBitset l, int s) { return l >>= s; }
};

/**
 * @brief 固定長bitset(Static Bitset)
 */
#line 21 "test/aoj0275_static_bitset.test.cpp"

int main() {
    Scanner in;
    Printer out;

    int n, m;
    in.read(n, m);
    vector<vector<edge<ll>>> g(n);
    vector<int> u(m), v(m), c(m);
    for (int i = 0; i < m; ++i) {
        in.read(u[i], v[i], c[i]);
        --u[i], --v[i];
        g[u[i]].emplace_back(v[i], c[i]);
        g[v[i]].emplace_back(u[i], c[i]);
    }

    int s, t, q;
    in.read(s, t, q);
    --s, --t;

    auto ds = dijkstra(s, g);
    auto dt = dijkstra(t, g);
    ll best = ds[t];

    vector<int> active_id(n, -1);
    vector<int> active_vertices;
    active_vertices.reserve(n);
    for (int i = 0; i < n; ++i) {
        if (ds[i] != INF<ll> && dt[i] != INF<ll> && ds[i] + dt[i] == best) {
            active_id[i] = (int)active_vertices.size();
            active_vertices.push_back(i);
        }
    }

    int k = active_vertices.size();
    vector<vector<int>> dag(k);
    vector<int> indeg(k, 0);
    for (int i = 0; i < m; ++i) {
        int a = active_id[u[i]];
        int b = active_id[v[i]];
        if (a != -1 && b != -1 && ds[u[i]] + c[i] == ds[v[i]]) {
            dag[a].push_back(b);
            ++indeg[b];
        }
        if (a != -1 && b != -1 && ds[v[i]] + c[i] == ds[u[i]]) {
            dag[b].push_back(a);
            ++indeg[a];
        }
    }

    vector<int> ord;
    ord.reserve(k);
    queue<int> que;
    for (int i = 0; i < k; ++i) if (indeg[i] == 0) que.push(i);
    while (!que.empty()) {
        int x = que.front();
        que.pop();
        ord.push_back(x);
        for (int to : dag[x]) {
            if (--indeg[to] == 0) que.push(to);
        }
    }

    vector<pair<int, int>> qs(q);
    for (auto &[a, b] : qs) {
        in.read(a, b);
        --a, --b;
    }

    constexpr int BLOCK = 2048;
    vector<StaticBitset<BLOCK>> dp(k);
    vector<unsigned char> active(k, 0);
    for (int l = 0; l < q; l += BLOCK) {
        int r = min(q, l + BLOCK);
        for (auto &bs : dp) bs.reset();
        fill(active.begin(), active.end(), 0);
        for (int i = l; i < r; ++i) {
            int a = active_id[qs[i].first];
            if (a != -1) {
                dp[a].set(i - l);
                active[a] = 1;
            }
        }
        for (int vtx : ord) {
            if (!active[vtx]) continue;
            for (int to : dag[vtx]) {
                dp[to] |= dp[vtx];
                active[to] = 1;
            }
        }
        for (int i = l; i < r; ++i) {
            int b = active_id[qs[i].second];
            out.println(b != -1 && dp[b].test(i - l) ? "Yes" : "No");
        }
    }
    return 0;
}
Back to top page