Pagini recente » Cod sursa (job #454951) | Cod sursa (job #2232121) | Cod sursa (job #1044816) | Cod sursa (job #1581791) | Cod sursa (job #3323392)
#include <iostream>
#include<fstream>
#include<vector>
#include<set>
#include<bitset>
#include<queue>
using namespace std;
const int NMAX=450001;
ifstream fin("santa.in");
ofstream fout("santa.out");
struct muchie
{
int x,y;
muchie(int xx=0,int yy=0)
{
x=xx,y=yy;
}
};
int p,n,m,nrfii,nrcb,st,e,q,nrs,nrm,nrq,elem1,elem2,
niv[NMAX],nma[NMAX],
s[NMAX],vf,v[NMAX];
vector<int>g[NMAX],cb[NMAX];
vector<muchie>mc;
set<int>pa;
bitset<NMAX>viz;
void citire()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
int x,y;
fin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
fin>>st>>e>>q;
}
void dfscb(int x,int tata)
{
s[++vf]=x;
viz[x]=1;
niv[x]=niv[tata]+1;
nma[x]=niv[x];
for(const auto &y:g[x])
{
if(y==tata) continue;
if(viz[y])
nma[x]=min(nma[x],niv[y]);
else
{
dfscb(y,x);
nma[x]=min(nma[x],nma[y]);
if(niv[x]<=nma[y])
{
nrcb++;
if(st==x||st==y) nrs=nrcb;
if(e==x||e==y) nrm=nrcb;
if(q==x||q==y) nrq=nrcb;
while(s[vf]!=y)
{
if(s[vf-1]==st ) nrs=nrcb;
if(s[vf-1]==e) nrm=nrcb;
if(s[vf-1]==q) nrq=nrcb;
cb[nrcb].push_back(s[vf--]);
}
cb[nrcb].push_back(y);
--vf;
cb[nrcb].push_back(x);
}
}
}
}
void dfspa(int x,int tata)
{
viz[x]=1;
niv[x]=niv[tata]+1;
nma[x]=niv[x];
for(const auto &y:g[x])
{
if(y==tata) continue;
if(viz[y])
nma[x]=min(nma[x],niv[y]);
else
{
dfspa(y,x);
nma[x]=min(nma[x],nma[y]);
if(x==1)
nrfii++;
else
{
if(niv[x]<=nma[y])
pa.insert(x);
}
}
}
}
void bfs(int nod)
{
queue<int>q;
v[nod]=1;
q.push(nod);
while(!q.empty())
{
int crt=q.front();
q.pop();
for(const auto &i:g[crt])
if(v[i]==0)
{
q.push(i);
v[i]=v[crt]+1;
}
}
}
int main()
{
citire();
dfscb(1,0);
for(int i=1; i<=n; i++)
viz[i]=0;
dfspa(1,0);
if(nrfii>=2) pa.insert(1);
bfs(q);
if(nrs==nrm)
{
if(nrs==nrq)
fout<<cb[nrs].size()-1;
else
{
for(const auto &x:cb[nrs])
if(pa.find(x)!=pa.end())
{
elem1=x;
break;
}
fout<<v[elem1]+cb[nrs].size()-2;
}
}
else
{
if(nrs==nrq||nrq==nrm)
fout<<cb[nrs].size()+cb[nrm].size()-2;
else
{
for(const auto &x:cb[nrm])
if(pa.find(x)!=pa.end())
{
elem1=x;
break;
}
for(const auto &x:cb[nrs])
if(pa.find(x)!=pa.end())
{
elem2=x;
break;
}
if(elem1==e&&elem2==st)
fout<<"No chance";
else
fout<<min(v[elem1],v[elem2])+cb[nrm].size()+cb[nrs].size()-3;
}
}
fin.close();
fout.close();
return 0;
}