Pagini recente » Cod sursa (job #1545642) | Cod sursa (job #261358) | Cod sursa (job #1804805) | Cod sursa (job #1386063) | Cod sursa (job #2863321)
#include <bits/stdc++.h>
#define NMAX 45005
using namespace std;
int n, m, s, e, q, niv[NMAX], nra[NMAX], nrCompBiconexe;
bool possiblePath[NMAX], v[NMAX];
vector<int> adj[NMAX], pctArticulatie, ans;
stack<int> st;
set<int> compBiconexe[NMAX];
void pupici() {
cout << "No chance";
exit(0);
}
inline void dfs(int nod, int tata = 0) {
v[nod] = 1;
niv[nod] = niv[tata] + 1;
nra[nod] = niv[nod];
st.push(nod);
for(const auto &el : adj[nod]) {
if(el == tata) continue;
if(v[el]) {
nra[nod] = min(nra[nod], niv[el]);
} else {
dfs(el, nod);
nra[nod] = min(nra[nod], nra[el]);
possiblePath[nod] |= possiblePath[el];
if(niv[nod] <= nra[el]) {
if(!possiblePath[el]) {
while(st.top() != el)
st.pop();
st.pop();
} else {
++nrCompBiconexe;
while(st.top() != nod) { //bagam si punctul de articulatie
compBiconexe[nrCompBiconexe].insert(st.top());
st.pop();
}
compBiconexe[nrCompBiconexe].insert(nod);
pctArticulatie.push_back(nod);
}
}
}
}
}
inline bool bt(int poz, int sz, int nod, int en) {
for(const auto &el : adj[nod]) {
if(!v[el]) {
v[el] = 1;
ans.push_back(el);
if(poz == sz - 1) return (el == en || !en);
else if(el != en) {
const bool crt = bt(poz + 1, sz, el, en);
if(crt) return 1;
}
ans.pop_back();
v[el] = 0;
}
}
return 0;
}
int main()
{
freopen("santa.in", "r", stdin);
freopen("santa.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1, x, y; i <= m; ++i) {
scanf("%d%d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
scanf("%d%d%d", &s, &e, &q);
pctArticulatie.push_back(s);
possiblePath[e] = 1;
dfs(s);
if(!possiblePath[s]) pupici();
if(compBiconexe[1].find(q) == compBiconexe[1].end()) {
reverse(compBiconexe + 1, compBiconexe + nrCompBiconexe + 1);
reverse(pctArticulatie.begin(), pctArticulatie.end());
}
if(compBiconexe[1].find(q) == compBiconexe[1].end()) {
pupici();
}
pctArticulatie[0] = q;
memset(v, 0, sizeof(v));
ans.push_back(q);
for(int i = 1; i <= nrCompBiconexe; ++i) {
v[pctArticulatie[i - 1]] = 1;
if(!bt(1, compBiconexe[i].size(), pctArticulatie[i - 1], (i == nrCompBiconexe ? 0 : pctArticulatie[i])))
pupici();
}
printf("%d\n", (int)ans.size());
for(const auto &el : ans)
printf("%d ", el);
return 0;
}