結果

問題 No.1576 織姫と彦星
ユーザー Hydrogen332
提出日時 2025-03-20 00:53:56
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,043 bytes
コンパイル時間 3,643 ms
コンパイル使用メモリ 282,404 KB
実行使用メモリ 7,328 KB
最終ジャッジ日時 2025-03-20 00:54:03
合計ジャッジ時間 6,260 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define all(a) (a).begin(),(a).end()

int hamDist(ll a, ll b) {
    int res = 0;
    while (a > 0 || b > 0) {
        if ((a & 1) ^ (b & 1)) ++res;
        a >>= 1;
        b >>= 1;
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    
    int N;
    cin >> N;
    vector<ll> S(N + 2);
    cin >> S[0] >> S[N + 1];
    rep(i, 1, N + 1) cin >> S[i];
    
    vector<int> dist(N + 2, -1);
    queue<int> que;
    dist[0] = 0;
    que.push(0);
    
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        
        rep(next, 1, N + 2) {
            if (dist[next] != -1 || v == next) continue;
            if (hamDist(S[v], S[next]) <= 1) {
                dist[next] = dist[v] + 1;
                que.push(next);
            }
        }
    }
    
    if (dist[N + 1] == -1) cout << -1 << '\n';
    else cout << dist[N + 1] - 1 << '\n';
}
0