結果

問題 No.1190 Points
ユーザー ninoinuininoinui
提出日時 2020-08-22 14:31:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,210 bytes
コンパイル時間 2,696 ms
コンパイル使用メモリ 216,900 KB
実行使用メモリ 28,992 KB
最終ジャッジ日時 2024-04-23 08:53:03
合計ジャッジ時間 6,944 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,880 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 73 ms
15,488 KB
testcase_04 AC 48 ms
13,568 KB
testcase_05 AC 67 ms
14,336 KB
testcase_06 AC 75 ms
17,152 KB
testcase_07 AC 114 ms
20,608 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T> class Dijkstra {
public:
  struct edge {
    int to; T cost;
  };
  int V;
  vector<vector<edge>> G;
  vector<T> dist;
  vector<int> pre;
  using pti = pair<T,int>;
  Dijkstra(int n) : V(n), G(V), dist(V, numeric_limits<T>::max()), pre(V) {}
  void add(int u, int v, T cost) {
    G.at(u).push_back((edge) {v, cost});
    G.at(v).push_back((edge) {u, cost});
  }
  void solve(int s) {
    priority_queue<pti, vector<pti>, greater<pti>> Q;
    dist.at(s) = 0;
    Q.push(pti(0, s));
    while (!Q.empty()) {
      pti p = Q.top();
      Q.pop();
      int v = p.second;
      if (dist.at(v) < p.first) continue;
      for (auto &w : G.at(v)) {
        if (dist.at(w.to) > dist.at(v) + w.cost) {
          dist.at(w.to) = dist.at(v) + w.cost;
          pre.at(w.to) = v;
          Q.push(pti(dist.at(w.to), w.to));
        }
      }
    }
  }
  vector<int> route(int from, int to) {
    vector<int> res;
    while (true) {
      res.push_back(to);
      if (to == from) break;
      to = pre.at(to);
    }
    reverse(res.begin(), res.end());
    return res;
  }
};

vector<vector<int>> G;
set<int> SE;
set<int> ans;
int lim;

void dfs(int now, int pre, int d) {
  if (d * 2 > lim) return;
  ans.insert(now);
  for (auto next : G.at(now)) {
    if (next == pre) continue;
    if (SE.count(next)) continue;
    dfs(next, now, d + 1);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N, M, P, s, g;
  cin >> N >> M >> P >> s >> g;
  s--, g--;
  vector<int> U(M), V(M);
  for (int i = 0; i < M; i++) {
    cin >> U.at(i) >> V.at(i);
    U.at(i)--, V.at(i)--;
  }
  Dijkstra<long> D(N);
  G.resize(N);
  for (int i = 0; i < M; i++) {
    D.add(U.at(i), V.at(i), 1);
    G.at(U.at(i)).push_back(V.at(i));
    G.at(V.at(i)).push_back(U.at(i));
  }
  D.solve(s);
  if (D.dist.at(g) > P) return cout << -1 << "\n", 0;
  if (D.dist.at(g) % 2 != P % 2) return cout << -1 << "\n", 0;
  lim = P - D.dist.at(g);
  auto R = D.route(s, g);
  for (auto r : R) SE.insert(r);
  for (auto r : R) dfs(r, -1, 0);
  if (!ans.size()) return cout << -1 << "\n", 0;
  cout << ans.size() << "\n";
  for (auto a : ans) cout << a + 1 << "\n";
}
0