結果
| 問題 |
No.1576 織姫と彦星
|
| ユーザー |
|
| 提出日時 | 2021-07-20 21:21:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 49 ms / 2,000 ms |
| コード長 | 10,691 bytes |
| コンパイル時間 | 3,557 ms |
| コンパイル使用メモリ | 185,712 KB |
| 最終ジャッジ日時 | 2025-01-23 03:23:40 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 54 |
ソースコード
#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;
}