Pagini recente » Cod sursa (job #307151) | Istoria paginii runda/monthly-2014-runda-9/clasament | Cod sursa (job #1804687) | Cod sursa (job #2878358) | Cod sursa (job #976018)
Cod sursa(job #976018)
#include<stdio.h>
#include<queue>
#define DIM 100003
using namespace std;
int n,m,s;
typedef struct list{
int node;
list* next;
} List;
List *graph[DIM];
int dist[DIM];
bool visited[DIM];
void read_data(){
int i,x,y;
List *temp;
scanf("%d %d %d",&n,&m,&s);
for(i=0;i<m;i++){
scanf("%d %d",&x,&y);
temp=new List;
temp->node=y;
temp->next=graph[x];
graph[x]=temp;
}
}
void bfs(){
int x;
List *it;
queue<int> q;
q.push(s);
visited[s]=true;
while(!q.empty()){
x=q.front();
q.pop();
it=graph[x];
while(it!=NULL){
if(!visited[it->node]){
q.push(it->node);
visited[it->node]=true;
dist[it->node]=dist[x]+1;
}
it=it->next;
}
}
}
void write_data(){
int i;
for(i=1;i<=n;i++){
if(dist[i] || i==s){
printf("%d ",dist[i]);
}else{
printf("%d ",-1);
}
}
printf("\n");
}
int main(){
freopen("bfs.in","r",stdin);
freopen("bfs.out","w",stdout);
read_data();
bfs();
write_data();
return 0;
}