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/yosupo_aplusb_functional_graph.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <cassert>
#include <random>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;

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

#include "../util/fastio.cpp"
#include "../graph/functional_graph.cpp"

tuple<vector<int>, int, int> walk_info(const vector<int> &to, int start) {
    int n = to.size();
    vector<int> pos(n, -1), ord;
    int cur = start;
    while (pos[cur] == -1) {
        pos[cur] = ord.size();
        ord.push_back(cur);
        cur = to[cur];
    }
    return {ord, pos[cur], (int)ord.size() - pos[cur]};
}

int brute_jump(const vector<int> &to, int start, long long k) {
    auto [ord, offset, len] = walk_info(to, start);
    if (k < (int)ord.size()) return ord[k];
    return ord[offset + (k - offset) % len];
}

void self_check() {
    mt19937 rng(0);
    for (int tc = 0; tc < 500; ++tc) {
        int n = rng() % 30 + 1;
        vector<int> to(n);
        for (int v = 0; v < n; ++v) to[v] = rng() % n;

        FunctionalGraph fg(n);
        for (int v = 0; v < n; ++v) fg.set_edge(v, to[v]);
        fg.build();

        FunctionalGraph fg2(to);
        for (int v = 0; v < n; ++v) {
            assert(fg.jump(v, 0) == v);
            assert(fg.jump(v, 1) == to[v]);
            assert(fg.jump(v, 37) == fg2.jump(v, 37));

            auto [ord, offset, len] = walk_info(to, v);
            int entry = ord[offset];
            assert(fg.steps_to_cycle(v) == offset);
            assert(fg.cycle_vertex(v) == entry);
            assert(fg.cycle_size(v) == len);
            assert(fg.in_cycle(v) == (offset == 0));
            assert(fg.cycle_id(v) == fg.cycle_id(entry));

            const auto &cyc = fg.cycle(fg.cycle_id(v));
            assert((int)cyc.size() == len);
            assert(cyc[fg.cycle_index(v)] == entry);

            for (long long k = 0; k <= 100; ++k) {
                assert(fg.jump(v, k) == brute_jump(to, v, k));
            }
            for (int rep = 0; rep < 20; ++rep) {
                long long k = (long long)(rng() % 1000000) * (rng() % 1000000);
                assert(fg.jump(v, k) == brute_jump(to, v, k));
            }
        }

        vector<int> seen_cycle(fg.cycles.size());
        for (int cid = 0; cid < (int)fg.cycles.size(); ++cid) {
            const auto &cyc = fg.cycle(cid);
            for (int i = 0; i < (int)cyc.size(); ++i) {
                int v = cyc[i];
                assert(fg.in_cycle(v));
                assert(to[v] == cyc[(i + 1) % cyc.size()]);
                ++seen_cycle[cid];
            }
        }
        for (int cid = 0; cid < (int)fg.cycles.size(); ++cid) {
            assert(seen_cycle[cid] == (int)fg.cycle(cid).size());
        }
    }
}

int main() {
    self_check();

    Scanner sc;
    Printer pr;
    ll a, b;
    sc.read(a, b);
    pr.println(a + b);
    return 0;
}
#line 1 "test/yosupo_aplusb_functional_graph.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include <cassert>
#include <random>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;

#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/functional_graph.cpp"
struct FunctionalGraph {
    static constexpr int LOG = 63;

    int n;
    vector<int> to;
    vector<vector<int>> up;
    vector<int> comp_id;
    vector<int> cycle_pos;
    vector<int> cycle_len;
    vector<int> dist_to_cycle;
    vector<int> cycle_entry;
    vector<vector<int>> cycles;

    explicit FunctionalGraph(int n)
        : n(n),
          to(n, -1),
          up(LOG, vector<int>(n, -1)),
          comp_id(n, -1),
          cycle_pos(n, -1),
          cycle_len(n, 0),
          dist_to_cycle(n, -1),
          cycle_entry(n, -1) {}

    explicit FunctionalGraph(const vector<int> &to)
        : FunctionalGraph((int)to.size()) {
        this->to = to;
        build();
    }

    void set_edge(int v, int nxt) {
        to[v] = nxt;
    }

    void build() {
        up.assign(LOG, vector<int>(n, -1));
        for (int v = 0; v < n; ++v) up[0][v] = to[v];
        for (int k = 0; k + 1 < LOG; ++k) {
            for (int v = 0; v < n; ++v) {
                up[k + 1][v] = up[k][up[k][v]];
            }
        }

        vector<int> indeg(n);
        for (int v = 0; v < n; ++v) ++indeg[to[v]];

        vector<int> que, order;
        que.reserve(n);
        order.reserve(n);
        for (int v = 0; v < n; ++v) {
            if (indeg[v] == 0) que.push_back(v);
        }
        for (int head = 0; head < (int)que.size(); ++head) {
            int v = que[head];
            order.push_back(v);
            int nxt = to[v];
            if (--indeg[nxt] == 0) que.push_back(nxt);
        }

        comp_id.assign(n, -1);
        cycle_pos.assign(n, -1);
        cycle_len.assign(n, 0);
        dist_to_cycle.assign(n, -1);
        cycle_entry.assign(n, -1);
        cycles.clear();

        vector<int> seen(n);
        for (int v = 0; v < n; ++v) {
            if (indeg[v] == 0 || seen[v]) continue;
            int cid = cycles.size();
            vector<int> cyc;
            int cur = v;
            do {
                seen[cur] = 1;
                comp_id[cur] = cid;
                cycle_pos[cur] = cyc.size();
                dist_to_cycle[cur] = 0;
                cycle_entry[cur] = cur;
                cyc.push_back(cur);
                cur = to[cur];
            } while (cur != v);
            for (int x : cyc) cycle_len[x] = cyc.size();
            cycles.push_back(cyc);
        }

        for (int i = (int)order.size() - 1; i >= 0; --i) {
            int v = order[i];
            int nxt = to[v];
            comp_id[v] = comp_id[nxt];
            cycle_pos[v] = cycle_pos[nxt];
            cycle_len[v] = cycle_len[nxt];
            dist_to_cycle[v] = dist_to_cycle[nxt] + 1;
            cycle_entry[v] = cycle_entry[nxt];
        }
    }

    int jump(int v, long long k) const {
        for (int i = 0; i < LOG; ++i) {
            if ((k >> i) & 1) v = up[i][v];
        }
        return v;
    }

    bool in_cycle(int v) const {
        return dist_to_cycle[v] == 0;
    }

    int cycle_id(int v) const {
        return comp_id[v];
    }

    int cycle_size(int v) const {
        return cycle_len[v];
    }

    int steps_to_cycle(int v) const {
        return dist_to_cycle[v];
    }

    int cycle_vertex(int v) const {
        return cycle_entry[v];
    }

    int cycle_index(int v) const {
        return cycle_pos[v];
    }

    const vector<int> &cycle(int id) const {
        return cycles[id];
    }
};

/**
 * @brief Functional Graph
 */
#line 18 "test/yosupo_aplusb_functional_graph.test.cpp"

tuple<vector<int>, int, int> walk_info(const vector<int> &to, int start) {
    int n = to.size();
    vector<int> pos(n, -1), ord;
    int cur = start;
    while (pos[cur] == -1) {
        pos[cur] = ord.size();
        ord.push_back(cur);
        cur = to[cur];
    }
    return {ord, pos[cur], (int)ord.size() - pos[cur]};
}

int brute_jump(const vector<int> &to, int start, long long k) {
    auto [ord, offset, len] = walk_info(to, start);
    if (k < (int)ord.size()) return ord[k];
    return ord[offset + (k - offset) % len];
}

void self_check() {
    mt19937 rng(0);
    for (int tc = 0; tc < 500; ++tc) {
        int n = rng() % 30 + 1;
        vector<int> to(n);
        for (int v = 0; v < n; ++v) to[v] = rng() % n;

        FunctionalGraph fg(n);
        for (int v = 0; v < n; ++v) fg.set_edge(v, to[v]);
        fg.build();

        FunctionalGraph fg2(to);
        for (int v = 0; v < n; ++v) {
            assert(fg.jump(v, 0) == v);
            assert(fg.jump(v, 1) == to[v]);
            assert(fg.jump(v, 37) == fg2.jump(v, 37));

            auto [ord, offset, len] = walk_info(to, v);
            int entry = ord[offset];
            assert(fg.steps_to_cycle(v) == offset);
            assert(fg.cycle_vertex(v) == entry);
            assert(fg.cycle_size(v) == len);
            assert(fg.in_cycle(v) == (offset == 0));
            assert(fg.cycle_id(v) == fg.cycle_id(entry));

            const auto &cyc = fg.cycle(fg.cycle_id(v));
            assert((int)cyc.size() == len);
            assert(cyc[fg.cycle_index(v)] == entry);

            for (long long k = 0; k <= 100; ++k) {
                assert(fg.jump(v, k) == brute_jump(to, v, k));
            }
            for (int rep = 0; rep < 20; ++rep) {
                long long k = (long long)(rng() % 1000000) * (rng() % 1000000);
                assert(fg.jump(v, k) == brute_jump(to, v, k));
            }
        }

        vector<int> seen_cycle(fg.cycles.size());
        for (int cid = 0; cid < (int)fg.cycles.size(); ++cid) {
            const auto &cyc = fg.cycle(cid);
            for (int i = 0; i < (int)cyc.size(); ++i) {
                int v = cyc[i];
                assert(fg.in_cycle(v));
                assert(to[v] == cyc[(i + 1) % cyc.size()]);
                ++seen_cycle[cid];
            }
        }
        for (int cid = 0; cid < (int)fg.cycles.size(); ++cid) {
            assert(seen_cycle[cid] == (int)fg.cycle(cid).size());
        }
    }
}

int main() {
    self_check();

    Scanner sc;
    Printer pr;
    ll a, b;
    sc.read(a, b);
    pr.println(a + b);
    return 0;
}
Back to top page