結果

問題 No.3291 K-step Navigation
ユーザー ir5
提出日時 2025-09-18 00:17:01
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 4,072 bytes
コンパイル時間 1,994 ms
コンパイル使用メモリ 171,896 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-18 00:17:05
合計ジャッジ時間 3,917 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 41 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <numeric>
#include <vector>
#include <map>
#include <unordered_map>
#include <bitset>
#include <queue>
#include <functional>
#include <iomanip>
#include <ranges>
using namespace std;
using ll = long long;

template<typename T> auto range(T s, T e) { return views::iota(s, max(s, e)); }
template<typename T> auto range(T n) { return range<T>(0, n); }
template<typename T> void take(vector<T>& vec, int n) { vec.resize(n); for (int i = 0; i < n; ++i) cin >> vec[i]; }
template<class... Args> void sout(const Args &...args) { ((cout << args << ' '), ...); }
template<class... Args> void soutn(const Args &...args) { ((cout << args << ' '), ...); cout << '\n'; }
template<typename T1, typename T2> struct In2 {
  T1 a; T2 b; friend std::istream& operator>>(std::istream& is, In2& obj) { T1 t1; T2 t2; is >> t1 >> t2; obj = {t1, t2}; return is; } };
template<typename T1, typename T2, typename T3> struct In3 {
  T1 a; T2 b; T3 c; friend std::istream& operator>>(std::istream& is, In3& obj) { T1 t1; T2 t2; T3 t3; is >> t1 >> t2 >> t3; obj = {t1, t2, t3}; return is; } };
template<typename T1, typename T2, typename T3, typename T4> struct In4 {
  T1 a; T2 b; T3 c; T4 d; friend std::istream& operator>>(std::istream& is, In4& obj) { T1 t1; T2 t2; T3 t3; T4 t4; is >> t1 >> t2 >> t3 >> t4; obj = {t1, t2, t3, t4}; return is; } };

#ifdef LOCAL
#include <debug.h>
#else
#define dump(...) ;
#endif

using EdgeCost = ll;
struct E {
  int fr, to, eid; EdgeCost cost;
  E(int fr_, int to_, int eid_, EdgeCost cost_) : fr(fr_), to(to_), eid(eid_), cost(cost_) {}
  friend ostream &operator<<(ostream &os, const E &e) { os << "(" << e.fr << " -> " << e.to << ")"; return os; }
};
using V = vector<E>;
struct G {
  int n, e_count;
  vector<V> adj;
  G(int n_ = 0) : n(n_), e_count(0), adj(n) {}

  void add_undirected_edge(int fr, int to, EdgeCost cost = (EdgeCost)1) {
    assert(0 <= fr && fr < n); assert(0 <= to && to < n); E e1(fr, to, e_count, cost), e2(to, fr, e_count++, cost);
    adj[fr].push_back(e1); adj[to].push_back(e2);
  }
};

G from_edges(int n, vector<In2<int, int>> edges) {
  G g(n);
  for (auto e : edges) g.add_undirected_edge(e.a - 1, e.b - 1);
  return g;
}

namespace {

ll n, m, k, s, t;
vector<In2<int, int>> edges;

void read() {
  cin >> n >> m >> k >> s >> t; take(edges, m);
}

using Bit = bitset<20>;

G g;

const ll inf = 1LL << 60;

EdgeCost shortest_path(int src, int dst) {
  vector<char> vis(g.n, 0);
  priority_queue<pair<EdgeCost, int>> q;
  q.push(make_pair((EdgeCost)0, src));

  while (!q.empty()) {
    EdgeCost curr_cost = -q.top().first;
    int idx = q.top().second;
    q.pop();
    if (vis[idx])
      continue;
    vis[idx] = 1;

    if (idx == dst) {
      return curr_cost;
    }

    for (auto it : g.adj[idx]) {
      if (vis[it.to])
        continue;
      if (it.fr == src && it.to == dst)
        continue;
      q.push({-(curr_cost + it.cost), it.to});
    }
  }

  return inf;
}

ll oddcycle() {
  ll res = inf;
  for (int i : range(n)) for (auto e : g.adj[i]) {
    ll cand = shortest_path(e.fr, e.to);
    if (cand % 2 == 0) res = min(res, 1 + cand);
  }
  return res;
}

string run() {
  s--;
  t--;
  Bit b;
  b[s] = 1;

  g = from_edges(n, edges);

  if (k % 2) return "Yes";

  for (int it : range(2)) {
    if (ssize(g.adj[s]) > 1) return "Yes";
    if (ssize(g.adj[s]) == 1 && g.adj[s][0].to != t) return "Yes";
    swap(s, t);
  }
  if (ssize(g.adj[s]) == 0 && ssize(g.adj[t]) == 0) return "No";

  return "Yes";
  /*
  ll od = oddcycle();
  dump("odd cycle", od);
  return od + 3 <= k ? "Yes" : "No";
  */
}

}  // namespace

template <typename F>
void exec(F f) { if constexpr (std::is_same_v<decltype(f()), void>) f(); else cout << f() << endl; }

int main(int argc, char** argv) {
  cerr << fixed << setprecision(12);
  cout << fixed << setprecision(12);
  int testcase = 1;
  if (argc > 1) testcase = atoi(argv[1]);
  while (testcase--) {
    read();
  }
  exec(run);
}
0