結果

問題 No.1190 Points
ユーザー ninoinuininoinui
提出日時 2020-08-22 14:28:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,199 bytes
コンパイル時間 2,674 ms
コンパイル使用メモリ 218,728 KB
実行使用メモリ 23,524 KB
最終ジャッジ日時 2024-04-23 08:49:02
合計ジャッジ時間 6,402 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 102 ms
15,488 KB
testcase_04 AC 76 ms
13,440 KB
testcase_05 AC 88 ms
14,336 KB
testcase_06 AC 105 ms
17,036 KB
testcase_07 AC 158 ms
20,480 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 112 ms
15,740 KB
testcase_14 AC 23 ms
9,984 KB
testcase_15 AC 191 ms
23,152 KB
testcase_16 AC 13 ms
5,760 KB
testcase_17 AC 172 ms
21,416 KB
testcase_18 WA -
testcase_19 AC 18 ms
10,624 KB
testcase_20 WA -
testcase_21 AC 33 ms
9,472 KB
testcase_22 AC 37 ms
13,568 KB
testcase_23 AC 204 ms
23,524 KB
testcase_24 AC 190 ms
23,524 KB
testcase_25 AC 86 ms
17,664 KB
testcase_26 AC 92 ms
17,664 KB
testcase_27 AC 93 ms
17,664 KB
権限があれば一括ダウンロードができます

ソースコード

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> 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 (ans.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) ans.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