結果
問題 |
No.1190 Points
|
ユーザー |
![]() |
提出日時 | 2020-08-22 14:31:45 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,210 bytes |
コンパイル時間 | 13,226 ms |
コンパイル使用メモリ | 278,160 KB |
最終ジャッジ日時 | 2025-01-13 08:44:48 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 MLE * 1 |
other | AC * 13 WA * 6 TLE * 6 |
ソースコード
#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"; }