Pagini recente » Cod sursa (job #343998) | Cod sursa (job #1921738) | Cod sursa (job #2206670) | Cod sursa (job #324177) | Cod sursa (job #3332920)
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("santa.in");
ofstream g("santa.out");
const int NMAX=45100;
int n,m,x,y,Niv[NMAX],Nma[NMAX],pa[NMAX],rad,s,e,q,vf,S[NMAX],viz[NMAX],nrCB,inDrum[NMAX];
vector<int>G[NMAX],CB[NMAX],Drum;
void DFS(int nod,int tata) {
S[++vf]=nod;
viz[nod]=1;
Niv[nod]=Niv[tata]+1;
Nma[nod]=Niv[nod];
for(const int&y:G[nod]) {
if(y==tata) {
continue;
}
if(viz[y]) {
Nma[nod]=min(Nma[nod],Niv[y]);
} else {
DFS(y,nod);
inDrum[nod]|=inDrum[y];
Nma[nod]=min(Nma[nod],Nma[y]);
if(Niv[nod]<=Nma[y]) {
if(!inDrum[y]) {
while(S[vf]!=y) {
vf--;
}
vf--;
} else {
++nrCB;
pa[nrCB]=nod;
while(S[vf]!=y) {
CB[nrCB].push_back(S[vf--]);
}
vf--;
CB[nrCB].push_back(y);
CB[nrCB].push_back(nod);
}
}
}
}
}
bool gasim(int a,int care) {
for(const int &q:CB[care]) {
if(q==a) {
return 1;
}
}
return 0;
}
bool bk(int k,int n,int nod,int fin) {
if(k==n) {
for(const int&x:G[nod]) {
if(!viz[x]) {
if(x==fin||fin==-1) {
viz[x]=1;
Drum.push_back(x);
return 1;
}
}
}
} else {
for(const int&x:G[nod]) {
if(!viz[x]) {
if(x==fin) {
continue;
}
Drum.push_back(x);
viz[x]=1;
bool vizTot=bk(k+1,n,x,fin);
if(vizTot) {
return 1;
}
Drum.pop_back();
viz[x]=0;
}
}
}
return 0;
}
int main() {
f>>n>>m;
while(m--) {
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
f>>s>>e>>q;
rad=s;
inDrum[e]=1;
DFS(rad,0);
bool we=gasim(q,nrCB);
if(!gasim(q,1)&&!we) {
g<<"No chance";
return 0;
}
if(we) {
reverse(pa+1,pa+nrCB+1);
reverse(CB+1,CB+nrCB+1);
} else {
for(int i=nrCB-1; i>=1; --i) {
pa[i+1]=pa[i];
}
}
for(int i=1; i<=n; ++i) {
viz[i]=1;
}
pa[1]=q;
pa[nrCB+1]=-1;
Drum.push_back(q);
for(int i=1; i<=nrCB; ++i) {
for(const int &j:CB[i]) {
viz[j]=0;
}
viz[pa[i]]=1;
bool posibi=bk(2,CB[i].size(),pa[i],pa[i+1]);;
if(posibi==0) {
g<<"No chance";
return 0;
}
}
g<<Drum.size()<<'\n';
for(const int &w:Drum) {
g<<w<<' ';
}
f.close();
g.close();
return 0;
}