結果
| 問題 |
No.1576 織姫と彦星
|
| ユーザー |
|
| 提出日時 | 2022-10-13 16:35:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 2,000 ms |
| コード長 | 975 bytes |
| コンパイル時間 | 2,457 ms |
| コンパイル使用メモリ | 211,736 KB |
| 最終ジャッジ日時 | 2025-02-08 02:42:44 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 54 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin>>N;
int from,to;
cin>>from>>to;
vector<int>A(N);
for(int &i:A)cin>>i;
A.push_back(from),A.push_back(to);
sort(A.begin(),A.end());
unordered_map<int,int>to_idx;
array<int,2>sg={0,0};
for(int i=0;i<N+2;i++){
if(A[i]==from){
sg[0]=i;
}
if(A[i]==to){
sg[1]=i;
}
to_idx[A[i]]=i;
}
vector<int>dist(N+2,1e9);
vector<vector<int>>G(N+2);
for(int i=0;i<N+2;i++){
for(int j=0;j<30;j++){
if(to_idx.find(A[i]^(1<<j))!=to_idx.end()){
G[i].push_back(to_idx[A[i]^(1<<j)]);
}
}
}
queue<int>bfs;
dist[sg[0]]=0;
bfs.push(sg[0]);
while(bfs.size()){
int v=bfs.front();
bfs.pop();
for(int i:G[v]){
if(dist[i]>dist[v]+1){
dist[i]=dist[v]+1;
bfs.push(i);
}
}
}
cout<<(dist[sg[1]]==1e9?-1:dist[sg[1]]-1)<<'\n';
}