結果

問題 No.1576 織姫と彦星
ユーザー jutamajutama
提出日時 2021-07-20 21:21:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 10,691 bytes
コンパイル時間 3,658 ms
コンパイル使用メモリ 194,356 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-17 13:48:41
合計ジャッジ時間 6,275 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 13 ms
6,944 KB
testcase_08 AC 9 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 34 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 22 ms
6,944 KB
testcase_13 AC 42 ms
6,940 KB
testcase_14 AC 28 ms
6,944 KB
testcase_15 AC 28 ms
6,940 KB
testcase_16 AC 39 ms
6,940 KB
testcase_17 AC 27 ms
6,940 KB
testcase_18 AC 6 ms
6,944 KB
testcase_19 AC 9 ms
6,940 KB
testcase_20 AC 28 ms
6,940 KB
testcase_21 AC 26 ms
6,944 KB
testcase_22 AC 33 ms
6,944 KB
testcase_23 AC 4 ms
6,940 KB
testcase_24 AC 5 ms
6,944 KB
testcase_25 AC 37 ms
6,940 KB
testcase_26 AC 11 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 34 ms
6,940 KB
testcase_29 AC 11 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 13 ms
6,944 KB
testcase_32 AC 26 ms
6,940 KB
testcase_33 AC 48 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 16 ms
6,944 KB
testcase_36 AC 4 ms
6,944 KB
testcase_37 AC 52 ms
6,940 KB
testcase_38 AC 34 ms
6,940 KB
testcase_39 AC 20 ms
6,944 KB
testcase_40 AC 14 ms
6,940 KB
testcase_41 AC 17 ms
6,944 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 6 ms
6,940 KB
testcase_44 AC 46 ms
6,940 KB
testcase_45 AC 13 ms
6,940 KB
testcase_46 AC 31 ms
6,944 KB
testcase_47 AC 7 ms
6,940 KB
testcase_48 AC 18 ms
6,940 KB
testcase_49 AC 17 ms
6,944 KB
testcase_50 AC 2 ms
6,940 KB
testcase_51 AC 4 ms
6,940 KB
testcase_52 AC 49 ms
6,944 KB
testcase_53 AC 27 ms
6,940 KB
testcase_54 AC 36 ms
6,940 KB
testcase_55 AC 48 ms
6,940 KB
testcase_56 AC 51 ms
6,940 KB
testcase_57 AC 2 ms
6,940 KB
testcase_58 AC 11 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bitset>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace atcoder;

typedef long long ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::pair<int, ll> pil;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, ld> pid;
typedef std::pair<int, std::string> pis;
typedef std::pair<ll, std::string> pls;
typedef std::vector<bool> vb;
typedef std::vector<vb> vvb;
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
typedef std::vector<vvi> vvvi;
typedef std::vector<vvvi> vvvvi;
typedef std::vector<ll> vl;
typedef std::vector<vl> vvl;
typedef std::vector<vvl> vvvl;
typedef std::vector<vvvl> vvvvl;
typedef std::vector<ld> vd;
typedef std::vector<vd> vvd;
typedef std::vector<std::string> vs;
#define rep(i,n) for(auto i=0; i<n; ++i)
#define repm(i,s,n) for(auto i=s; i<n; ++i)
#define repd(i,n) for(auto i=n-1; i>=0; --i)
#define repdm(i,e,n) for(auto i=n-1; i>=e; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

template <class T> inline bool chmax(T& a, T b, int eq = 0) { if (a < b || (a == b && eq)) { a = b; return 1; } return 0; }
template <class T> inline bool chmin(T& a, T b, int eq = 0) { if (a > b || (a == b && eq)) { a = b; return 1; } return 0; }

template <class mint, internal::is_modint_t<mint>* = nullptr> constexpr istream& operator>>(istream& is, mint& x) noexcept {long long v = 0; std::cin >> v; x = v; return is;}
template <class mint, internal::is_modint_t<mint>* = nullptr> constexpr ostream& operator<<(ostream& os, const mint& x) noexcept {os << x.val(); return os;}
inline void _n() { std::cout << std::endl; }
template <class T> inline void _(const T a) { std::cout << a; }
template <class T> inline void _l(const T a) { _(a); _n(); }
template <class T> inline void _s(const T a) { _(a); _(' '); }
template <class T> inline void _v(const std::vector<T> v) { for(auto a : v) _(a); }
template <class T> inline void _vl(const std::vector<T> v) { for(auto a : v) _l(a); }
template <class T> inline void _vs(const std::vector<T> v) { for(auto a : v) _s(a); _n(); }
template <class T> inline void _vvl(const std::vector<std::vector<T>> v) { for(auto a : v) { _v(a); _n(); } }
template <class T> inline void _vvs(const std::vector<std::vector<T>> v) { for(auto a : v) { _vs(a); } }
inline void ynl(const bool b) {_l(b ? "yes" : "no");}
inline void yns(const bool b) {_l(b ? "Yes" : "No");}
inline void ynu(const bool b) {_l(b ? "YES" : "NO");}

constexpr int INF = numeric_limits<int>::max() >> 1;
constexpr long long INF_LL = numeric_limits<long long>::max() >> 1LL;
constexpr long long MOD1 = 1000000007;
constexpr long long MOD9 = 998244353;
using mint1 = atcoder::modint1000000007;
using mint9 = atcoder::modint998244353;

//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//

template <class Cost> struct graph_l {

    public :
    graph_l(int n) : _n(n), _graph(n), _dist(n, 0) {}

    void add (int from, int to, Cost cost){
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        _graph[from].push_back(_edge{(unsigned int)from, (unsigned int)to, cost});
    }
    void add_bi (int from, int to, Cost cost){
        add(from, to, cost);
        add(to, from, cost);
    }
    std::vector<int> get_edges(int v) {
        std::vector<int> edges;
        for (auto edge : _graph[v]) {
            edges.push_back(edge.to);
        }
        return edges;
    }

    bool is_Reachable (int v) {
        assert(0 <= v && v < _n);
        return _dist[v] < DIST_INF;
    }
    Cost get_dist (int v) {
        assert(0 <= v && v < _n);
        return _dist[v];
    }
    int get_lca(int u, int v) {
        assert(0 <= u && u < _n);
        assert(0 <= v && v < _n);
        if (_depth[u] < _depth[v]) swap(u, v);
        int K = (int)_parent.size();

        for (int k = 0; k < K; k++) {
            if ((_depth[u] - _depth[v]) >> k & 1) {
                u = _parent[k][u];
            }
        }

        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (_parent[k][u] != _parent[k][v]) {
                u = _parent[k][u];
                v = _parent[k][v];
            }
        }
        return _parent[0][u];
    }
    Cost get_dist_with_lca (int u, int v) {
        assert(0 <= u && u < _n);
        assert(0 <= v && v < _n);
        return _dist[u] + _dist[v] - 2 * _dist[get_lca(u, v)];
    }
    std::vector<int> get_articulation_points(int s = 0) {
        if(s > 0) std::sort(_aps.begin(), _aps.end());
        return _aps;
    }
    std::vector<std::pair<int, int>> get_bridges(int s = 0) {
        if(s > 0) std::sort(_bridges.begin(), _bridges.end());
        return _bridges;
    }

    void dijkstra (int s) {
        assert(0 <= s && s < _n);
        _dist.assign(_n, DIST_INF);
        _dist[s] = 0;

        auto edge_compare = [] (_edge e1, _edge e2) {
            if (e1.cost != e2.cost) return e1.cost > e2.cost;
            else if (e1.to != e2.to) return e1.to > e2.to;
            else return e1.from > e2.from;
        };
        priority_queue<_edge, vector<_edge>, function<bool(_edge, _edge)>> edge_que(edge_compare);
        edge_que.push(_edge{(unsigned)s, (unsigned)s, 0});
        // for (auto e : _graph[s]) {
        //     edge_que.push(_edge{e.from, e.to, e.cost});
        //     if(_dist[e.to] > e.cost) _dist[e.to] = e.cost;
        // }
        while (!edge_que.empty()) {
            _edge p = edge_que.top(); edge_que.pop();
            unsigned int v = p.to;
            if (_dist[v] < p.cost) continue;
            for (auto e : _graph[v]) {
                auto next_cost = e.cost + _dist[v];
                if (_dist[e.to] <= next_cost) continue;
                _dist[e.to] = next_cost;
                edge_que.push(_edge{e.from, e.to, _dist[e.to]});
            }
        }
    }

    bool bellman_ford (int s) {
        assert(0 <= s && s < _n);
        _dist.assign(_n, DIST_INF);
        _dist[s] = 0;

        for(int i = 0; i < _n - 1; ++i) {
            for(int j = 0; j < _n; ++j ){
                for(auto e : _graph[j]) {
                    if(_dist[j] == DIST_INF) continue;
                    _dist[e.to] = min(_dist[e.to], _dist[j] + e.cost);
                }
            }
        }
        for(int j = 0; j < _n; ++j ){
            for(auto e : _graph[j]) {
                if(_dist[j] == DIST_INF) continue;
                if(_dist[j] + e.cost < _dist[e.to]) return false;
            }
        }
        return true;
    }

    void lca(int r = 0) {
        assert(0 <= r && r < _n);
        int K = 1; while ((1 << K) < _n) K++;
        _dist.assign(_n, DIST_INF);
        _depth.assign(_n, -1);
        _parent.assign(K, std::vector<int>(_n, -1));

        dfs_lca(r, -1, 0, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < _n; v++) {
                if (_parent[k][v] < 0) {
                    _parent[k+1][v] = -1;
                } else {
                    _parent[k+1][v] = _parent[k][_parent[k][v]];
                }
            }
        }
    }

    Cost kruskal() {
        std::vector<_edge> edge_vec;
        for (int v = 0; v < _n; ++v) {
            for (auto e : _graph[v]) {
                edge_vec.push_back(e);
            }
        }
        auto edge_compare = [] (_edge e1, _edge e2) {
            if (e1.cost != e2.cost) return e1.cost < e2.cost;
            else if (e1.to != e2.to) return e1.to < e2.to;
            else return e1.from < e2.from;
        };
        std::sort(edge_vec.begin(), edge_vec.end(), edge_compare);

        Cost _cost = 0;
        atcoder::dsu _dsu(_n);
        for (auto e : edge_vec) {
            if (!_dsu.same(e.from, e.to)) {
                _dsu.merge(e.from, e.to);
                _cost += e.cost;
            }
        }
        return _dsu.groups().size() == 1 ? _cost : -1;
    }

    void lowlink() {
        _used.resize(_n);
        _ord.resize(_n);
        _low.resize(_n);
        int j = 0;
        for (int i = 0; i < _n; i++) {
            if (!_used[i]) j = dfs_lowlink(i, j, -1);
        }
    }

private:
    void dfs_lca(unsigned int v, int p, unsigned int d, Cost c) {
        _parent[0][v] = p;
        _depth[v] = d;
        _dist[v] = c;
        for (auto e : _graph[v]) {
            if (e.to != p) dfs_lca(e.to, v, d + 1, c + e.cost);
        }
    }

    int dfs_lowlink(unsigned int i, unsigned int j, int par) {
        _used[i] = true;
        _ord[i] = j++;
        _low[i] = _ord[i];
        bool is_aps = false;
        int count = 0;
        for (auto e : _graph[i]) {
            if (!_used[e.to]) {
                count++;
                j = dfs_lowlink(e.to, j, i);
                _low[i] = min(_low[i], _low[e.to]);
                if (par != -1 && _ord[i] <= _low[e.to]) is_aps = true;
                if (_ord[i] < _low[e.to]) _bridges.emplace_back(min(i, e.to), max(i, e.to));
            } else if (e.to != par) {
                _low[i] = min(_low[i], _ord[e.to]);
            }
        }
        if (par == -1 && count >= 2) is_aps = true;
        if (is_aps) _aps.push_back(i);
        return j;
    }

private:
    int _n;
    const Cost DIST_INF = numeric_limits<Cost>::max();

    struct _edge {
        unsigned int from;
        unsigned int to;
        Cost cost;
    };
    std::vector<std::vector<_edge>> _graph;
    std::vector<Cost> _dist;

    std::vector<int> _depth;
    std::vector<std::vector<int>> _parent;

    std::vector<int> _used, _ord, _low;
    std::vector<int> _aps;
    std::vector<std::pair<int, int>> _bridges;
};

void solve() {
    
    auto dist = [](int a, int b) -> int {
        bitset<64> ab(a), bb(b);
        int res = 0;
        rep(i, 64) if(ab.test(i) != bb.test(i)) res++;
        return res;
    };
    
    int N; cin >> N;
    vi S(N+2); rep(i, N+2) cin >> S[i];
    graph_l<int> G(N+2);
    
    rep(i, N+2) {
        repm(j, i, N+2) {
            if(i == j) G.add(i, i, 0);
            else if(dist(S[i], S[j]) <= 1) G.add_bi(i, j, 1);
        }
    }
    G.dijkstra(0);
    int ans = G.is_Reachable(1) ? G.get_dist(1) : 0;
    _l(--ans);
    
    
    
    
    
}

//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//

int main() {
    
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
    std::cin.tie(0);
    std::cout.tie(0);
    std::ios::sync_with_stdio(false);
    
    solve();
    
    return 0;
}
0