Cod sursa(job #303928)

Utilizator tibiletsKoos Tiberiu Iosif tibilets Data 10 aprilie 2009 15:29:22
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include<fstream.h>
int N,M,S,x,y,c[100001],in,sf,d[100001];
short s[100001];
struct nod
{int v;
 nod *adr;}*L[100000],*p;
void push(int n,int v)
{nod *p=new nod;
 p->v=v;
 p->adr=L[n];
 L[n]=p;}
int main()
{ifstream f("bfs.in");
ofstream g("bfs.out");
f>>N>>M>>S;
for(;M;--M)
{f>>x>>y;
 push(x,y);}
memset(d,-1,N*4+4);
c[0]=S;s[S]=1;
d[S]=0;
while(in<=sf)
{p=L[c[in]];
 while(p)
 {if(!s[p->v])
  {c[++sf]=p->v;
   s[p->v]=1;
   d[p->v]=d[c[in]]+1;}
  p=p->adr;
 }
 ++in;
}
for(x=1;x<=N;++x)
 g<<d[x]<<' ';
return 0;
}