結果

問題 No.1775 Love Triangle 2
ユーザー 👑 hitonanodehitonanode
提出日時 2021-11-25 23:50:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,957 bytes
コンパイル時間 2,657 ms
コンパイル使用メモリ 125,324 KB
実行使用メモリ 8,444 KB
最終ジャッジ日時 2023-09-16 22:18:51
合計ジャッジ時間 51,709 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
4,380 KB
testcase_01 AC 12 ms
4,380 KB
testcase_02 AC 35 ms
4,380 KB
testcase_03 AC 22 ms
4,380 KB
testcase_04 AC 5,089 ms
4,460 KB
testcase_05 AC 5,077 ms
4,448 KB
testcase_06 AC 5,141 ms
4,480 KB
testcase_07 AC 1,895 ms
4,380 KB
testcase_08 AC 6,192 ms
4,456 KB
testcase_09 AC 247 ms
4,384 KB
testcase_10 AC 265 ms
4,376 KB
testcase_11 AC 576 ms
4,376 KB
testcase_12 AC 586 ms
4,380 KB
testcase_13 AC 831 ms
4,376 KB
testcase_14 AC 1,276 ms
4,384 KB
testcase_15 AC 2,797 ms
4,380 KB
testcase_16 AC 3,631 ms
4,384 KB
testcase_17 AC 5,110 ms
4,376 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// 方針(正当性なし)
// - 現在使われていない頂点のみを使い x -> y に流量 2 を流して,一方の x-y パスを採択する
// - 現在使われていない頂点のみを使い x -> y, x -> z に流量 1 ずつ同時に流して,両方のパスを採択する
// - x -> y パスを消す
// - ランダムに頂点を使用不能にする
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

#include <tuple>

namespace INPUT {
using namespace std;

tuple<int, int, int, int, vector<vector<int>>> read() {
    int N, M, X, Y, Z;
    cin >> N >> M >> X >> Y >> Z;
    --X, --Y, --Z;
    vector mat(N, vector<int>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) { mat[i][j] = (i != j); }
    }
    while (M--) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        mat[a][b] = mat[b][a] = 0;
    }
    vector<vector<int>> to(N);
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            if (mat[i][j]) to[i].push_back(j);
        }
    }
    return {N, X, Y, Z, to};
}
}; // namespace INPUT
#line 3 "twopaths.hpp"
#include <atcoder/mincostflow>
#include <cassert>
#line 8 "twopaths.hpp"

namespace TWOPATHS {
using namespace std;

// used_vs に含まれる頂点は使わずに,from -> to1 と from->to2 の点素なパスを構成する.
// 両方のパスが構築できなければ empty vector の組を返す.
pair<vector<int>, vector<int>> twopaths(const vector<vector<int>> &to, const vector<int> &used_vs, int from, int to1, int to2) {
    const int N = to.size();
    const int gt = N * 2;
    atcoder::mcf_graph<int, int> graph(gt + 1);

    vector<int> valid_v(N, 1);
    for (auto i : used_vs) valid_v[i] = 0;

    valid_v[to1] = valid_v[to2] = 0;

    for (int i = 0; i < N; ++i) { graph.add_edge(i, i + N, valid_v[i], 0); }
    graph.add_edge(to1, to1 + N, 1, 0);
    graph.add_edge(to2, to2 + N, 1, 0);

    for (int i = 0; i < N; ++i) {
        for (auto j : to[i]) {
            int cost = 1;
            graph.add_edge(i + N, j, 1, cost);
        }
    }
    graph.add_edge(to1 + N, gt, 1, 0);
    graph.add_edge(to2 + N, gt, 1, 0);
    auto f = graph.flow(from + N, gt, 2);
    if (f.first < 2) return {{}, {}};

    vector<vector<int>> conn(N);
    for (auto e : graph.edges()) {
        if (e.flow) {
            if (e.to == gt) continue;
            int s = e.from % N, t = e.to % N;
            if (s != t) conn[s].push_back(t);
        }
    }

    vector<vector<int>> ret;
    while (conn[from].size()) {
        int now = from;
        vector<int> vec{now};
        while (conn[now].size()) {
            int nxt = conn[now].back();
            conn[now].pop_back();
            now = nxt;
            vec.push_back(now);
        }
        ret.push_back(vec);
    }
    assert(ret.size() == 2);
    if (ret[0].back() != to1) swap(ret[0], ret[1]);
    if (ret[1].back() != to2) swap(ret[0], ret[1]);
    return {ret.at(0), ret.at(1)};
}

}

namespace Yuki7140 {
uint32_t rand_int() // XorShift random integer generator
{
    static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

struct State {
    int x, y, z;
    vector<vector<int>> to;
    vector<int> xy, yz, zx;
    State(int x_, int y_, int z_, vector<vector<int>> to_) : x(x_), y(y_), z(z_), to(to_) {}
    bool xy_good() const { return xy.size() and xy.front() == x and xy.back() == y; }
    bool yz_good() const { return yz.size() and yz.front() == y and yz.back() == z; }
    bool zx_good() const { return zx.size() and zx.front() == z and zx.back() == x; }
    bool is_feasible() const { return xy_good() and yz_good() and zx_good(); }

    static vector<int> inner(std::vector<int> vs) {
        if (vs.empty()) return {};
        vs.pop_back();
        swap(vs.front(), vs.back());
        vs.pop_back();
        return vs;
    }

    vector<int> enum_inner_vs() const {
        vector<int> ret;
        for (int i = 1; i + 1 < int(xy.size()); ++i) ret.push_back(xy[i]);
        for (int i = 1; i + 1 < int(yz.size()); ++i) ret.push_back(yz[i]);
        for (int i = 1; i + 1 < int(zx.size()); ++i) ret.push_back(zx[i]);
        return ret;
    }

    void opt_xyz() {
        if (!zx_good()) return;
        auto [yxnew, yznew] = TWOPATHS::twopaths(to, inner(zx), y, x, z);
        xy = yxnew;
        reverse(xy.begin(), xy.end());
        yz = yznew;
    }

    void opt_yzx() {
        if (!xy_good()) return;
        auto [zynew, zxnew] = TWOPATHS::twopaths(to, inner(xy), z, y, x);
        yz = zynew;
        reverse(yz.begin(), yz.end());
        zx = zxnew;
    }

    void opt_zxy() {
        if (!yz_good()) return;
        auto [xznew, xynew] = TWOPATHS::twopaths(to, inner(yz), x, z, y);
        zx = xznew;
        reverse(zx.begin(), zx.end());
        xy = xynew;
    }

    void init_xyxy() {
        auto [xy1, xy2] = TWOPATHS::twopaths(to, {}, x, y, y);
        if (rand_int() & 1) {
            xy = xy1;
        } else {
            xy = xy2;
        }
    }

    vector<int> dump() const { // return [x, ..., y, ..., z, ..., x]
        if (!is_feasible()) return {};
        vector<int> ret = xy;
        ret.pop_back();
        ret.insert(ret.end(), yz.begin(), yz.end());
        ret.pop_back();
        ret.insert(ret.end(), zx.begin(), zx.end());
        return ret;
    }
};
}; // namespace Yuki7140
#line 4 "simple_circle.hpp"

namespace SimpleCircle {
using namespace std;
int solve(int x, int y, int z, const vector<vector<int>> &to) {
    auto [xy1, xy2] = TWOPATHS::twopaths(to, {}, x, y, y);
    bool z_exists = false;
    for (auto i : xy1) z_exists |= (i == z);
    for (auto i : xy2) z_exists |= (i == z);
    if (z_exists) return xy1.size() + xy2.size() - 1;
    return to.size() + 1;
}
};
#line 12 "uso_drop.cpp"

uint32_t rand_int() {
    // XorShift random integer generator
    static uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    uint32_t t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

vector<vector<int>> drop_graph(vector<vector<int>> to, int x, int y, int z, int k) {
    const int n = to.size();

    vector<int> is_erased(n);
    for (int i = 0; i < k; ++i) is_erased[rand_int() % n] = 1;
    is_erased[x] = is_erased[y] = is_erased[z] = 0;

    for (int i = 0; i < n; ++i) {
        if (is_erased[i]) {
            to[i].clear();
        } else {
            for (int j = 0; j < int(to[i].size()); ++j) {
                if (is_erased[to[i][j]]) {
                    swap(to[i][j], to[i].back());
                    to[i].pop_back();
                    --j;
                }
            }
        }
    }
    return to;
}


int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);

    auto [N, X, Y, Z, to] = INPUT::read();

    vector<int> xyz{X, Y, Z};

    int ret = N + 1;
    for (int nerase = 0; nerase < 5; ++nerase) {
        cerr << nerase << ':';
        for (int t = 0; t < 3; ++t) {

            ret = min(ret, SimpleCircle::solve(X, Y, Z, to));

            for (int ntry = 0; ntry < 100; ++ntry) {
                Yuki7140::State state(xyz[0], xyz[1], xyz[2], drop_graph(to, X, Y, Z, nerase));
                state.init_xyxy();
                state.opt_yzx();
                for (int iter = 0; iter < 10; ++iter) {
                    int c = rand_int() % 3;
                    if (c == 0) state.opt_xyz();
                    if (c == 1) state.opt_yzx();
                    if (c == 2) state.opt_zxy();
                }
                if (state.is_feasible()) ret = min<int>(ret, state.dump().size() - 1);
                cerr << ' ' << state.dump().size();
            }
        }
        cerr << '\n';
        rotate(xyz.begin(), xyz.begin() + 1, xyz.end());
    }

    cout << (ret <= N ? ret : -1) << '\n';
}
0