Pagini recente » Cod sursa (job #252809) | Cod sursa (job #1155199) | Cod sursa (job #3287872) | Cod sursa (job #448070) | Cod sursa (job #1867961)
#include <cstdio>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
#define MAXN 45050
#define inf 0x3f3f3f3f
using namespace std;
ifstream cin("santa.in");
ofstream cout("santa.out");
vector<int> graf[MAXN];
vector<vector<int> > compo;
vector<int> temp, sol;
stack<pair<int, int> > st;
int depth[MAXN],viz[MAXN],low[MAXN],parent[MAXN],need[MAXN],art[MAXN],inc[MAXN];
int n,m,s,e,q,goon=1;
void citire(){
int x,y;
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x>>y;
graf[x].push_back(y);
graf[y].push_back(x);
}
cin>>s>>e>>q;
need[s]=1;
}
void addCompo(int nod, int must)
{
if (!must) {
while(st.top().first != nod)
st.pop();
st.pop();
return ;
}
temp.clear();
while (!st.empty()) {
int cx = st.top().first;
int cy = st.top().second;
st.pop();
temp.push_back(cy);
if (cx == nod) break;
}
temp.push_back(nod);
compo.push_back(temp);
art[compo.size()] = nod;
}
void biconex(int nod, int d)
{
low[nod] = depth[nod] = d;
viz[nod] = 1;
for (int y : graf[nod]) {
if (parent[nod] == y) continue;
if (viz[y])
low[nod] = min(low[nod], depth[y]);
else {
parent[y] = nod;
st.push(make_pair(nod, y));
biconex(y, d+1);
low[nod] = min(low[nod], low[y]);
need[nod] |= need[y];
if (d <= low[y]) {
addCompo(nod, need[y]);
}
}
}
}
void biconex(int nod, int d)
{
low[nod] = depth[nod] = d;
viz[nod] = 1;
for (int y : graf[nod]) {
if (parent[nod] == y) continue;
if (viz[y])
low[nod] = min(low[nod], depth[y]);
else {
parent[y] = nod;
st.push(make_pair(nod, y));
biconex(y, d+1);
low[nod] = min(low[nod], low[y]);
need[nod] |= need[y];
if (d <= low[y]) {
addCompo(nod, need[y]);
}
}
}
}
void afisare()
{
if (!goon) {
cout<<"No chance";
return;
}
cout<<sol.size()<<'\n';
for (int nod : sol)
cout<<nod;
}
int hamilton(int crt, int pas, int npas, int fin) /// lant hamilton cu back
{
viz[crt] = 1;
sol.push_back(crt);
if (pas == npas && fin == -1) return 1;
if (pas == npas) return crt == fin;
for (int y : graf[crt]) {
if (viz[y] || !inc[y]) continue;
if (pas+1 != npas && y == fin) continue;
if (hamilton(y, pas+1, npas, fin))
return 1;
else {
sol.pop_back();
viz[y] = 0;
}
}
return 0;
}
void buildSolution()
{
for (int i = 0; i < compo.size() && goon; i++) {
for (int y : compo[i])
inc[y] = 1, viz[y] = 0;
goon = hamilton(art[i], 0, compo[i].size()-1, art[i+1]);
if (!sol.empty() && i+1 < compo.size()) sol.pop_back();
for (int y : compo[i])
inc[y] = 0;
}
}
int main(){
citire();
biconex(e, 1);
goon = prepare();
buildSolution();
afisare();
return 0;
}