結果
| 問題 | No.1190 Points |
| コンテスト | |
| ユーザー |
ninoinui
|
| 提出日時 | 2020-08-22 14:28:11 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,199 bytes |
| 記録 | |
| コンパイル時間 | 2,906 ms |
| コンパイル使用メモリ | 213,920 KB |
| 最終ジャッジ日時 | 2025-01-13 08:41:26 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 WA * 7 |
ソースコード
#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";
}
ninoinui