Pagini recente » Cod sursa (job #2712842) | Cod sursa (job #2491033) | Cod sursa (job #2951615) | Cod sursa (job #3140970) | Cod sursa (job #2415936)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 45e3;
vector < int > g[MAXN + 1], art, stk, path;
vector < vector < int > > bcc;
int lvl[MAXN + 1], low[MAXN + 1], consider[MAXN + 1], seen[MAXN + 1];
inline void get_comp(int node, int son) {
vector < int > comp;
if (consider[son] == 0) {
while (stk.back() ^ son)
stk.pop_back();
stk.pop_back();
return;
}
art.push_back(node);
do {
comp.push_back(stk.back());
stk.pop_back();
} while (comp.back() ^ son);
comp.push_back(node);
bcc.push_back(comp);
}
void bcc_dfs(int node, int father = 0) {
low[node] = lvl[node] = lvl[father] + 1;
stk.push_back(node);
for (auto it : g[node])
if (it ^ father) {
if (lvl[it])
low[node] = min(low[node], lvl[it]);
else {
bcc_dfs(it, node);
low[node] = min(low[node], low[it]);
consider[node] |= consider[it];
if (low[it] >= lvl[node])
get_comp(node, it);
}
}
}
ofstream fout("santa.out");
inline void tzeapa() {
fout << "No chance";
exit(0);
}
int bkt(int num, int targ, int node, int lst) {
seen[node] = 1;
if (num > 1)
path.push_back(node);
if (num == targ) {
if (node == lst || lst == 0)
return 1;
path.pop_back();
seen[node] = 0;
return 0;
}
for (auto it : g[node])
if (seen[it] == 0 && (it != lst || num == targ - 1) && bkt(num + 1, targ, it, lst))
return 1;
path.pop_back();
seen[node] = 0;
return 0;
}
inline void build_path(vector < int >& nodes, int frst, int lst) {
for (auto it : nodes)
seen[it] = 0;
if (bkt(1, nodes.size(), frst, lst) == 0)
tzeapa();
}
int main()
{
int n, m, s, e, q;
ifstream fin("santa.in");
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
fin >> s >> e >> q;
fin.close();
art = {s};
consider[e] = 1;
bcc_dfs(s, 0);
if (consider[s] == 0)
tzeapa();
if (find(bcc[0].begin(), bcc[0].end(), q) == bcc[0].end()) {
reverse(bcc.begin(), bcc.end());
reverse(art.begin(), art.end());
}
if (find(bcc[0].begin(), bcc[0].end(), q) == bcc[0].end())
tzeapa();
art.back() = 0;
reverse(art.begin(), art.end());
art.push_back(q);
reverse(art.begin(), art.end());
path.push_back(q);
memset(seen, 1, sizeof seen);
for (int i = 0; i < (int) bcc.size(); ++i)
build_path(bcc[i], art[i], art[i + 1]);
reverse(path.begin(), path.end());
fout << path.size() << '\n';
for (auto it : path)
fout << it << " ";
return 0;
}