QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125269#143. 最大流(随机数据)NOI_AK_ME#37.5 3ms3976kbC++2326.7kb2023-07-16 14:14:172023-07-16 14:14:20

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-16 14:14:20]
  • 评测
  • 测评结果:37.5
  • 用时:3ms
  • 内存:3976kb
  • [2023-07-16 14:14:17]
  • 提交

answer

#include"bits/stdc++.h"
using namespace std;
template<typename T_container,typename T=typename enable_if<!is_same<T_container,string>::value,typename T_container::value_type>::type>istream&operator>>(istream&is,T_container&v){for(T&x:v)is>>x;return is;}
ostream&operator<<(ostream&os,__int128 const&value){
    static char buffer[64];
    int index=0;
    __uint128_t T=(value<0)?(-(value+1))+__uint128_t(1):value;
    if(value<0)os<<'-';
    else if(T==0)return os<<'0';
    for(;T>0;++index)buffer[index]=static_cast<char>('0'+(T%10)),T/=10;
    while(index>0)os<<buffer[--index];
    return os;
}
istream&operator>>(istream &is,__int128&T){
    static char buffer[64];
    is >> buffer;
    size_t len = strlen(buffer), index = 0;
    T = 0;
    int mul = 1;
    if (buffer[index] == '-')
        ++index, mul=-mul;
    for (; index < len; ++index)
        T = T * 10 + static_cast<int>(buffer[index] - '0');
    T *= mul;
    return is;
}
namespace FastIO{
namespace Internal{
template<typename T>struct internal_get_unsigned{typedef typename make_unsigned<T>::type type;};
template<>struct internal_get_unsigned<__int128>{typedef __uint128_t type;};
template<>struct internal_get_unsigned<__uint128_t>{typedef __uint128_t type;};
template<class T>struct is_int{static constexpr bool value=is_integral<T>::value;};
template<>struct is_int<__int128_t>{static constexpr bool value=1;};
template<>struct is_int<__uint128_t>{static constexpr bool value=1;};
template<class T>struct is_char{static constexpr bool value=is_same<T,char>::value;};
template<class T>struct is_bool{static constexpr bool value=is_same<T,bool>::value;};
template<class T>struct is_string{static constexpr bool value=is_same<T,string>::value||is_same<T,const char*>::value||is_same<T,char*>::value||is_same<decay_t<T>,char*>::value;};
template<class T,class D=void>struct is_custom{static constexpr bool value=0;};
template<class T>struct is_custom<T,void_t<typename T::internal_value_type>>{static constexpr bool value=1;};
template<class T>
struct is_default{
    static constexpr bool value = is_char<T>::value || is_bool<T>::value ||
                                  is_string<T>::value ||
                                  is_int<T>::value;
};

template <class T, class D = void>
struct is_iterable {
    static constexpr bool value = false;
};

template <class T>
struct is_iterable <
T, typename std::void_t<decltype(std::begin(std::declval<T>())) >> {
    static constexpr bool value = true;
};

template <class T, class D = void, class E = void>
struct is_applyable {
    static constexpr bool value = false;
};

template <class T>
struct is_applyable<T, std::void_t<typename std::tuple_size<T>::type>,
       std::void_t<decltype(std::get<0>(std::declval<T>()))>> {
           static constexpr bool value = true;
       };
};

struct IOPre {
    static constexpr int TEN = 10, SZ = TEN * TEN * TEN * TEN;
    std::array<char, 4 * SZ> num;
    constexpr IOPre() : num{} {
        for (int i = 0; i < SZ; i++) {
            int n = i;

            for (int j = 3; j >= 0; j--) {
                num[i * 4 + j] = static_cast<char>(n % TEN + '0');
                n /= TEN;
            }
        }
    }
};

template<const bool SAFETY_CHECKS>
struct IO {


#if !HAVE_DECL_FREAD_UNLOCKED
#define fread_unlocked fread
#endif
#if !HAVE_DECL_FWRITE_UNLOCKED
#define fwrite_unlocked fwrite
#endif

    static constexpr int BUFFER_SIZE = 1 << 17, LEN = 64, TEN = 10, HUNDRED = TEN * TEN,
                         THOUSAND = HUNDRED * TEN, TENTHOUSAND = THOUSAND * TEN,
                         MAGIC_MULTIPLY = 205, MAGIC_SHIFT = 11, MASK = 15,
                         TWELVE = 32, SIXTEEN = 36, INTEGER_WIDTH = 64;
    static constexpr IOPre io_pre = {};


    std::array<char, BUFFER_SIZE> input_buffer, output_buffer;
    int input_start_ptr, input_end_ptr, output_end_ptr;
    bool end_of_file;

    FILE *read_fptr, *write_fptr;

    IO(FILE *read_fp, FILE *write_fp): read_fptr(read_fp), write_fptr(write_fp), input_buffer{}, output_buffer{},
        input_start_ptr{}, input_end_ptr{}, output_end_ptr{}, end_of_file(false) {}
    IO(const IO &) = delete;
    IO(IO &&) = delete;
    IO &operator = (const IO &) = delete;
    IO &operator = (const IO &&) = delete;

    ~IO() {
        flush();
    }

    template <class T>
    static constexpr bool needs_newline = (Internal::is_iterable<T>::value ||
                                           Internal::is_applyable<T>::value) &&
                                          (!Internal::is_default<T>::value);

    template <typename T, typename U>
    struct any_needs_newline {
        static constexpr bool value = false;
    };
    template <typename T>
    struct any_needs_newline<T, std::index_sequence<>> {
        static constexpr bool value = false;
    };
    template <typename T, std::size_t I, std::size_t... Is>
    struct any_needs_newline<T, std::index_sequence<I, Is...>> {
        static constexpr bool value =
            needs_newline<decltype(std::get<I>(std::declval<T>()))> ||
            any_needs_newline<T, std::index_sequence<Is...>>::value;
    };


    inline bool
    reload() { // Reloads without already read checks -- add read check for eof in interactive problems?.
        if constexpr(SAFETY_CHECKS) {
            assert(input_end_ptr >= input_start_ptr);
        }


        if (input_start_ptr > (BUFFER_SIZE >> 1))
            [[unlikely]] {
            memmove(std::begin(input_buffer), std::begin(input_buffer) + input_start_ptr, input_end_ptr - input_start_ptr);
            input_end_ptr -= input_start_ptr;
            input_start_ptr = 0;
        }

        if (end_of_file)
            [[unlikely]]
            return false;
        else if (input_end_ptr == BUFFER_SIZE)
            [[unlikely]]
            return true;

        size_t read_length = fread_unlocked(std::begin(input_buffer) + input_end_ptr, sizeof(char),
                                            BUFFER_SIZE - input_end_ptr, read_fptr);

        if (read_length == 0) {
            end_of_file = true;
            input_buffer[input_end_ptr] = '\0';
            read_length = 1;
        }

        input_end_ptr += read_length;
        return true;
    }

    inline bool read_char(char &c) {
        if (input_start_ptr == input_end_ptr)
            [[unlikely]] {
            if (!reload())
                return false;
        }
        c = input_buffer[input_start_ptr++];
        return true;
    }

    inline bool read_string(std::string &s) {
        while (true) {
            while (input_start_ptr < input_end_ptr && input_buffer[input_start_ptr] <= ' ')
                ++input_start_ptr;

            if (input_start_ptr < input_end_ptr)
                break;

            if (!reload())
                return false;
        }

        s.clear();
        char x;

        while (true) {
            if (!read_char(x) || x <= ' ') // read_char will always be true?
                break;

            s += x;
        }

        return !s.empty();
    }

    template <class T>
    inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type read_float(
        T &x) { // No custom implementation -- maybe even slower.
        std::string s;

        if (!read_string(s))
            return false;

        x = std::stold(s);
        return true;
    }

    template <class T>inline typename enable_if<Internal::is_int<T>::value, bool>::type read_int(T&x){
        using U = typename Internal::internal_get_unsigned<T>::type;
        while(1){
            while(input_start_ptr<input_end_ptr&&input_buffer[input_start_ptr]<'-')++input_start_ptr;
            if(input_start_ptr<input_end_ptr)break;
            if(!reload())return false;
        }
        if(input_start_ptr+INTEGER_WIDTH>input_end_ptr)reload();
        if(input_start_ptr==input_end_ptr)return false;
        bool minus=0;
        if(input_buffer[input_start_ptr] == '-')minus=1,++input_start_ptr;
        x=0;
        char c;
        while(input_start_ptr < input_end_ptr) {
            c=input_buffer[input_start_ptr];
            if(c<'0'||c>'9')break;
            ++input_start_ptr;
            x = x * TEN + (c & MASK);
        }

        if (minus)
            x = -x;

        return true;
    }
    inline void flush() {
        fwrite_unlocked(std::begin(output_buffer), sizeof(char), output_end_ptr, write_fptr);
        output_end_ptr = 0;
    }

    template <typename T>
    IO &operator >> (T &x) {
        static_assert(Internal::is_custom<T>::value or Internal::is_default<T>::value or
                      Internal::is_iterable<T>::value or Internal::is_applyable<T>::value or is_floating_point<T>::value);
        static_assert(!Internal::is_bool<T>::value);
        bool was_read = false;

        if constexpr(Internal::is_custom<T>::value){
            typename T::internal_value_type y;
            was_read=read_int(y);
            x=y;
        }
        else if constexpr(Internal::is_default<T>::value){
            if constexpr(Internal::is_string<T>::value)was_read=read_string(x);
            else if constexpr(Internal::is_char<T>::value)was_read=read_char(x);
            else if constexpr(Internal::is_int<T>::value)was_read=read_int(x);
        } else if constexpr(is_floating_point<T>::value) {
            was_read = read_float(x);
        } else if constexpr(Internal::is_iterable<T>::value) {
            for (auto &y : x)
                operator>>(y);
        } else if constexpr(Internal::is_applyable<T>::value) {
            std::apply([this](auto & ... y) {
                ((this->operator>>(y)), ...);
            }, x);
        }

        if constexpr(SAFETY_CHECKS and (Internal::is_custom<T>::value or Internal::is_default<T>::value or
                                        is_floating_point<T>::value)) {
            assert(was_read);
        }

        return *this;
    }

    inline void write_char(char c) {
        if (output_end_ptr == BUFFER_SIZE)
            [[unlikely]] flush();

        output_buffer[output_end_ptr++] = c;
    }

    inline void write_bool(bool b) {
        if (output_end_ptr == BUFFER_SIZE)
            [[unlikely]] flush();

        output_buffer[output_end_ptr++] = b ? '1' : '0';
    }

    inline void write_string(const std::string &s) {
        for (const auto &x : s)
            write_char(x);
    }

    inline void write_string(const char *s) {
        while (*s)
            write_char(*s++);
    }

    inline void write_string(char *s) {
        while (*s)
            write_char(*s++);
    }

    template <typename T>
    inline typename std::enable_if<Internal::is_int<T>::value, void>::type write_int(const T &y) {
        using U = typename Internal::internal_get_unsigned<T>::type;

        if (output_end_ptr > BUFFER_SIZE - INTEGER_WIDTH)
            flush();

        if (!y) {
            output_buffer[output_end_ptr++] = '0';
            return;
        }

        U x = y;

        if (y < 0)
            output_buffer[output_end_ptr++] = '-', x = -y;

        int i = TWELVE;
        static std::array<char, SIXTEEN> buf{};

        while (x >= TENTHOUSAND) {
            memcpy(std::begin(buf) + i,
                   std::begin(io_pre.num) + (x % TENTHOUSAND) * 4, 4);
            x /= TENTHOUSAND;
            i -= 4;
        }

        if (x < HUNDRED) {
            if (x < TEN) {
                output_buffer[output_end_ptr++] = static_cast<char>('0' + x);
            } else {
                std::uint32_t q =
                    (static_cast<std::uint32_t>(x) * MAGIC_MULTIPLY) >>
                    MAGIC_SHIFT;
                std::uint32_t r = static_cast<std::uint32_t>(x) - q * TEN;
                output_buffer[output_end_ptr] = static_cast<char>('0' + q);
                output_buffer[output_end_ptr + 1] =
                    static_cast<char>('0' + r);
                output_end_ptr += 2;
            }
        } else {
            if (x < THOUSAND) {
                memcpy(std::begin(output_buffer) + output_end_ptr,
                       std::begin(io_pre.num) + (x << 2) + 1, 3),
                           output_end_ptr += 3;
            } else {
                memcpy(std::begin(output_buffer) + output_end_ptr,
                       std::begin(io_pre.num) + (x << 2), 4),
                           output_end_ptr += 4;
            }
        }

        memcpy(std::begin(output_buffer) + output_end_ptr,
               std::begin(buf) + i + 4, TWELVE - i);
        output_end_ptr += TWELVE - i;
    }

    template <typename T_>
    IO &operator << (T_ &&x) {
        using T = typename std::remove_cv <
                  typename std::remove_reference<T_>::type >::type;
        static_assert(Internal::is_custom<T>::value or Internal::is_default<T>::value or
                      Internal::is_iterable<T>::value or Internal::is_applyable<T>::value);

        if constexpr(Internal::is_custom<T>::value) {
            write_int(x.get());
        } else if constexpr(Internal::is_default<T>::value) {
            if constexpr(Internal::is_bool<T>::value) {
                write_bool(x);
            } else if constexpr(Internal::is_string<T>::value) {
                write_string(x);
            } else if constexpr(Internal::is_char<T>::value) {
                write_char(x);
            } else if constexpr(Internal::is_int<T>::value) {
                write_int(x);
            }
        } else if constexpr(Internal::is_iterable<T>::value) {
            // strings are immune
            using E = decltype(*std::begin(x));
            constexpr char sep = needs_newline<E> ? '\n' : ' ';
            int i = 0;

            for (const auto &y : x) {
                if (i++)
                    write_char(sep);

                operator<<(y);
            }
        } else if constexpr(Internal::is_applyable<T>::value) {
            // strings are immune
            constexpr char sep =
                (any_needs_newline <
                 T, std::make_index_sequence<std::tuple_size_v<T> >>::value)
                ? '\n'
                : ' ';
            int i = 0;
            std::apply(
            [this, &sep, &i](auto const & ... y) {
                (((i++ ? write_char(sep) : void()), this->operator<<(y)),
                 ...);
            },
            x);
        }

        return *this;
    }
    IO &operator << (IO<SAFETY_CHECKS> &(*func)(IO<SAFETY_CHECKS> &)) {
        return func(*this);
    }
    IO *tie(std::nullptr_t) {
        return this;
    }
    inline void sync_with_stdio(bool) {}
};
IO<false> Io(stdin, stdout);

template <const bool SAFETY_CHECKS>
FastIO::IO<SAFETY_CHECKS> &endl(FastIO::IO<SAFETY_CHECKS> &os) {
    os.write_char('\n');
    os.flush();
    return os;
}

#define cin FastIO::Io
#define cout FastIO::Io

};
using FastIO::endl;
/*/-----------------------------Code begins----------------------------------/*/
// https://loj.ac/s/1481088
// template <typename _Tp, typename _Fp, typename _Compare = std::less<void>>
// bool chmax(_Tp &a, const _Fp &b, _Compare __comp = _Compare()) { return __comp(a, b) ? a = b, true : false; }
// template <typename _Tp, typename _Fp, typename _Compare = std::less<void>>
// bool chmin(_Tp &a, const _Fp &b, _Compare __comp = _Compare()) { return __comp(b, a) ? a = b, true : false; }
namespace OY {
template <typename _Tp>
struct HLPP {
    struct _RawEdge {
        uint32_t from, to;
        _Tp cap;
    };
    struct _Edge {
        uint32_t to, rev;
        _Tp cap;
        bool operator>(const _Edge &other) const {
            return cap > other.cap;
        }
    };
    std::vector<_RawEdge> m_rawEdges;
    std::vector<_Edge> m_edges;
    std::vector<uint32_t> m_starts;
    uint32_t m_vertexNum;
    HLPP(uint32_t __vertexNum, uint32_t __edgeNum) : m_starts(__vertexNum + 1, 0), m_vertexNum(__vertexNum) {
        m_rawEdges.reserve(__edgeNum);
    }
    void addEdge(uint32_t __a, uint32_t __b, _Tp __cap) {
        m_rawEdges.push_back({__a, __b, __cap});
    }
    void build() {
        for (auto &[from, to, cap] : m_rawEdges)
            if (from != to) {
                m_starts[from + 1]++;
                m_starts[to + 1]++;
            }

        std::partial_sum(m_starts.begin(), m_starts.end(), m_starts.begin());
        m_edges.resize(m_starts.back());
        uint32_t cursor[m_vertexNum];
        std::copy(m_starts.begin(), m_starts.begin() + m_vertexNum, cursor);

        for (auto &[from, to, cap] : m_rawEdges)
            if (from != to) {
                m_edges[cursor[from]] = {to, cursor[to], cap};
                m_edges[cursor[to]++] = {from, cursor[from]++, 0};
            }
    }
    template <typename _Compare = std::greater<_Edge>>
    void buildSorted(_Compare __comp = _Compare()) {
        build();

        for (uint32_t i = 0; i < m_vertexNum; i++) {
            uint32_t start = m_starts[i], end = m_starts[i + 1];
            std::sort(m_edges.begin() + start, m_edges.begin() + end, __comp);

            for (uint32_t j = start; j < end; j++)
                m_edges[m_edges[j].rev].rev = j;
        }
    }
    _Tp calc(uint32_t __source, uint32_t __target, _Tp __infiniteCap = std::numeric_limits<_Tp>::max() / 2) {
        uint32_t queue[m_vertexNum], height[m_vertexNum], ex_next[m_vertexNum * 2], gap_prev[m_vertexNum * 2],
                 gap_next[m_vertexNum * 2], ex_highest = 0, gap_highest = 0, discharge_count, it[m_vertexNum],
                                            end[m_vertexNum];
        _Tp ex[m_vertexNum];
        auto ex_insert = [&](uint32_t i, uint32_t h) {
            ex_next[i] = ex_next[m_vertexNum + h];
            ex_next[m_vertexNum + h] = i;
            ex_highest = max(ex_highest, h);
        };
        auto gap_insert = [&](uint32_t i, uint32_t h) {
            gap_prev[i] = m_vertexNum + h;
            gap_next[i] = gap_next[m_vertexNum + h];
            gap_prev[gap_next[i]] = gap_next[gap_prev[i]] = i;
            gap_highest = max(gap_highest, h);
        };
        auto gap_erase = [&](uint32_t i) {
            gap_next[gap_prev[i]] = gap_next[i];
            gap_prev[gap_next[i]] = gap_prev[i];
        };
        auto ex_add = [&](uint32_t i, _Tp f) {
            ex[i] += f;

            if (ex[i] == f)
                ex_insert(i, height[i]);
        };
        auto ex_remove = [&](uint32_t i, _Tp f) {
            ex[i] -= f;
        };
        auto update_height = [&](uint32_t i, uint32_t h) {
            if (~height[i])
                gap_erase(i);

            height[i] = h;

            if (~h) {
                gap_insert(i, h);

                if (ex[i] > 0)
                    ex_insert(i, h);
            }
        };
        auto global_relabel = [&] {
            discharge_count = 0;
            std::iota(ex_next + m_vertexNum, ex_next + m_vertexNum * 2, m_vertexNum);
            std::iota(gap_prev + m_vertexNum, gap_prev + m_vertexNum * 2, m_vertexNum);
            std::iota(gap_next + m_vertexNum, gap_next + m_vertexNum * 2, m_vertexNum);
            std::fill(height, height + m_vertexNum, -1);
            height[__target] = 0;
            uint32_t head = 0, tail = 0;
            queue[tail++] = __target;

            while (head < tail)
                for (uint32_t from = queue[head++], cur = m_starts[from], end = m_starts[from + 1]; cur < end; cur++)
                    if (auto &[to, rev, cap] = m_edges[cur]; m_edges[rev].cap && height[to] > height[from] + 1) {
                        update_height(to, height[from] + 1);
                        queue[tail++] = to;
                    }
        };
        auto push = [&](uint32_t from, uint32_t to, uint32_t rev, _Tp & cap, _Tp f) {
            ex_remove(from, f);
            ex_add(to, f);
            cap -= f;
            m_edges[rev].cap += f;
        };
        auto discharge = [&](uint32_t i) {
            uint32_t h = m_vertexNum;
            uint32_t pos = it[i];

            for (uint32_t &cur = it[i]; cur < end[i]; cur++)
                if (auto &[to, rev, cap] = m_edges[cur]; cap) {
                    if (height[i] == height[to] + 1) {
                        push(i, to, rev, cap, std::min(ex[i], cap));

                        if (!ex[i])
                            return;
                    } else
                        h = min(h, height[to]);
                }

            it[i] = m_starts[i];

            for (uint32_t &cur = it[i]; cur < pos; cur++)
                if (auto &[to, rev, cap] = m_edges[cur]; cap) {
                    if (height[i] == height[to] + 1) {
                        push(i, to, rev, cap, std::min(ex[i], cap));

                        if (!ex[i])
                            return;
                    } else
                        h = min(h, height[to]);
                }

            discharge_count++;

            if (gap_next[gap_next[m_vertexNum + height[i]]] < m_vertexNum)
                update_height(i, h == m_vertexNum ? -1 : h + 1);
            else {
                uint32_t oldh = height[i];

                for (h = oldh; h <= gap_highest; h++)
                    while (gap_next[m_vertexNum + h] < m_vertexNum) {
                        uint32_t j = gap_next[m_vertexNum + h];
                        height[j] = -1;
                        gap_erase(j);
                    }

                gap_highest = oldh - 1;
            }
        };

        for (uint32_t i = 0; i < m_vertexNum; i++)
            it[i] = m_starts[i];

        for (uint32_t i = 0; i < m_vertexNum; i++)
            end[i] = m_starts[i + 1];

        std::fill(ex, ex + m_vertexNum, 0);
        global_relabel();
        ex_add(__source, __infiniteCap);
        ex_remove(__target, __infiniteCap);

        while (~ex_highest) {
            while (true) {
                uint32_t i = ex_next[m_vertexNum + ex_highest];

                if (i >= m_vertexNum)
                    break;

                ex_next[m_vertexNum + ex_highest] = ex_next[i];

                if (height[i] != ex_highest)
                    continue;

                discharge(i);

                if (discharge_count >= 4 * m_vertexNum)
                    global_relabel();
            }

            ex_highest--;
        }

        return ex[__target] + __infiniteCap;
    }
};
}


template <typename Cap = int>
struct HLPP {
    struct Edge {
        int j, q;
        Cap x;
    };
    int N, K = 0;
    vector<vector<Edge>> G;
    HLPP(int n): N(n), G(N) { }
    void addEdge(int i, int j, Cap x, Cap y = 0) {
        int p = G[i].size(), q = G[j].size();
        G[i].push_back({j, q, x});
        G[j].push_back({i, p, y});
    }
    Cap calc(int src, int sink, Cap inf = numeric_limits<Cap>::max()) {
        vector<int> pos(N);
        vector<Cap> ex(N);
        int queue[N], height[N], ex_next[N * 2], gap_prev[N * 2], gap_next[N * 2];
        int ex_highest = 0, gap_highest = 0, discharge_count = 0;
        auto ex_insert = [&](int i, int h) {
            ex_next[i] = ex_next[N + h];
            ex_next[N + h] = i;
            ex_highest = max(ex_highest, h);
        };
        auto gap_insert = [&](int i, int h) {
            gap_prev[i] = N + h;
            gap_next[i] = gap_next[N + h];
            gap_prev[gap_next[i]] = gap_next[gap_prev[i]] = i;
            gap_highest = max(gap_highest, h);
        };
        auto gap_erase = [&](int i) {
            gap_next[gap_prev[i]] = gap_next[i];
            gap_prev[gap_next[i]] = gap_prev[i];
        };
        auto ex_add = [&](int i, Cap f) {
            ex[i] += f;

            if (ex[i] == f)
                ex_insert(i, height[i]);
        };
        auto update_height = [&](int i, int h) {
            if (height[i] != N + 1)
                gap_erase(i);

            height[i] = h;

            if (h == N + 1)
                return;

            gap_insert(i, h);

            if (ex[i] > 0)
                ex_insert(i, h);
        };
        auto global_relabel = [&] {
            discharge_count = 0;
            iota(ex_next + N, ex_next + N * 2, N);
            iota(gap_prev + N, gap_prev + N * 2, N);
            iota(gap_next + N, gap_next + N * 2, N);
            fill(height, height + N, N + 1);
            height[sink] = 0;
            int head = 0, tail = 0;
            queue[tail++] = sink;

            while (head < tail) {
                int i = queue[head++];

                for (auto& [j, q, x] : G[i]) {
                    if (!G[j][q].x || height[j] <= height[i] + 1)
                        continue;

                    update_height(j, height[i] + 1);
                    queue[tail++] = j;
                }
            }
        };
        auto discharge = [&](int i) {
            auto &v = ex[i];
            int h = height[i], nh = N;

            for (int &p = pos[i], n = G[i].size(); n--; p = (p ? : G[i].size()) - 1) {
                auto& [j, q, x] = G[i][p];

                if (!x)
                    continue;

                if (h != height[j] + 1) { // i == sink?
                    nh = min(nh, height[j]);
                    continue;
                }

                auto f = min(v, x);
                v -= f;
                ex_add(j, f);
                x -= f;
                G[j][q].x += f;

                if (!v)
                    return;
            }

            discharge_count++;

            if (gap_next[gap_next[N + h]] < N) {
                update_height(i, nh + 1);
                return;
            }

            for (int oldh = h; gap_highest >= oldh; gap_highest--)
                while (gap_next[N + gap_highest] < N) {
                    int j = gap_next[N + gap_highest];
                    height[j] = N + 1;
                    gap_erase(j);
                }
        };
        global_relabel();
        ex_add(src, inf);
        ex[sink] -= inf;

        while (~ex_highest) {
            int i = ex_next[N + ex_highest];

            if (i >= N) {
                ex_highest--;
                continue;
            }

            ex_next[N + ex_highest] = ex_next[i];

            if (height[i] != ex_highest)
                continue;

            discharge(i);

            if (discharge_count >= 4 * N)
                global_relabel();
        }

        return ex[sink] + inf;
    }
};
int main() {

    cin.tie(nullptr) -> sync_with_stdio(false);
    uint32_t n, m, s, t;
    cin >> n >> m >> s >> t;
    OY::HLPP<uint32_t> G(n, m);

    for (int i = 0; i < m; i++) {
        uint32_t a, b;
        uint32_t c;
        cin >> a >> b >> c;
        G.addEdge(a - 1, b - 1, c);
    }

    G.build();
    cout << G.calc(s - 1, t - 1);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 12.5
Accepted
time: 1ms
memory: 3708kb

input:

52 275 1 2
11 18 1
18 48 9
10 15 1
11 19 1
10 20 1
3 14 1
8 16 1
31 32 2147483647
10 42 9
5 14 1
3 15 1
5 17 1
6 50 9
1 6 9
28 29 2147483647
18 40 9
43 42 2147483647
1 9 9
9 20 1
1 7 9
24 6 9
39 38 2147483647
4 14 1
38 37 2147483647
5 46 9
3 18 1
15 44 9
4 17 1
32 33 2147483647
28 9 9
32 9 9
26 12 9...

output:

729

result:

ok single line: '729'

Test #2:

score: 12.5
Accepted
time: 2ms
memory: 3944kb

input:

67 4489 14 1
25 63 19983
49 18 26963
9 29 23009
25 30 10286
45 6 14693
61 11 8464
12 19 29821
39 36 2365
12 7 20737
56 51 21002
9 63 14701
15 10 24386
21 36 25930
49 21 10680
56 11 25508
26 27 2101
46 4 1770
16 56 19722
23 8 28411
67 32 28897
45 62 22880
30 38 13226
37 56 18650
10 57 700
62 53 19659...

output:

1025243

result:

ok single line: '1025243'

Test #3:

score: 12.5
Accepted
time: 1ms
memory: 3776kb

input:

100 1029 1 2
39 96 19
68 19 19
16 33 1
17 25 1
74 22 19
50 23 19
46 29 19
70 24 19
27 92 19
50 25 19
6 36 1
34 80 19
72 19 19
48 13 19
11 86 19
19 86 19
100 99 2147483647
4 39 1
60 9 19
76 7 19
34 100 19
98 97 2147483647
15 25 1
14 94 19
5 40 1
4 38 1
46 34 19
90 89 2147483647
42 39 19
58 27 19
3 39...

output:

4693

result:

ok single line: '4693'

Test #4:

score: 0
Wrong Answer
time: 1ms
memory: 3808kb

input:

100 500 64 68
97 1 597234350
42 59 1020828575
52 59 1341185789
46 82 534859215
84 98 1408384018
95 85 97421544
50 51 1658946459
71 91 1071433566
16 5 577259372
79 16 941940144
32 66 2144021311
42 94 132280559
100 83 2093384600
34 98 1633024304
31 69 735801701
68 13 632197336
70 25 868338831
60 91 14...

output:

2147483647

result:

wrong answer 1st lines differ - expected: '4259958644', found: '2147483647'

Test #5:

score: 0
Wrong Answer
time: 1ms
memory: 3820kb

input:

100 1500 30 87
12 52 1212854316
66 34 500229329
28 30 1905848380
45 10 1906211267
35 5 1227091997
14 10 797678626
42 39 2119948760
80 55 263028757
72 32 1402091192
2 70 114204531
53 87 1885940117
39 68 1262963681
20 100 363298998
81 19 475298425
86 17 276841422
95 43 940479356
85 55 1720319570
40 65...

output:

2147483647

result:

wrong answer 1st lines differ - expected: '17139501202', found: '2147483647'

Test #6:

score: 0
Wrong Answer
time: 2ms
memory: 3924kb

input:

100 5000 12 73
5 90 596775756
35 20 226786760
28 31 1775982092
79 17 743002845
10 19 150120683
83 96 901953035
91 62 809520329
2 61 1024423315
30 91 1374494188
93 26 751944004
82 82 727762428
1 43 502389284
84 87 1379778919
52 32 1459460146
71 15 983677176
18 3 249963037
80 32 828290820
40 99 159181...

output:

2147483647

result:

wrong answer 1st lines differ - expected: '37381805875', found: '2147483647'

Test #7:

score: 0
Wrong Answer
time: 3ms
memory: 3948kb

input:

100 5000 13 28
74 16 599476
99 76 112185
76 68 887056
13 2 181381
23 72 214611
10 15 955272
57 53 163306
81 44 721618
68 62 71172
70 44 233121
13 52 701794
77 40 298244
54 28 626039
26 63 829000
25 14 91588
97 62 980457
17 15 572847
100 75 273645
4 65 344467
17 47 299474
40 19 270752
50 68 804106
21...

output:

2147483647

result:

wrong answer 1st lines differ - expected: '2193636882', found: '2147483647'

Test #8:

score: 0
Wrong Answer
time: 2ms
memory: 3976kb

input:

100 5000 66 90
35 39 966842
3 56 577708
38 60 515530
3 73 351251
29 27 508007
56 70 185615
73 51 331650
6 32 589603
29 96 822851
9 99 335209
20 45 806531
60 10 460779
93 21 203582
77 27 391590
3 14 315530
86 41 234991
53 69 96865
97 15 203159
14 43 815111
4 24 337097
88 79 288209
64 34 806690
13 26 ...

output:

2147483647

result:

wrong answer 1st lines differ - expected: '4340954172', found: '2147483647'