Pagini recente » Cod sursa (job #3221750) | Cod sursa (job #1724192) | Istoria paginii utilizator/edfgtrewq | Cod sursa (job #1703476) | Cod sursa (job #1646087)
#include <iostream>
#include <limits.h>
#include <cmath>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <map>
#include <fstream>
#include <list>
#include <queue>
#include <iomanip>
#include <deque>
#include <set>
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.14159265
#define nmax 100005
#define inf (1<<30)
ifstream f("bfs.in");
ofstream g("bfs.out");
int n,m,seen[nmax];
int dist[nmax];
vector<int> v[nmax];
queue<int> q;
void BFS(int nod)
{
for(int i = 1; i <= n; i++)
dist[i] = -1;
dist[nod] = 0;
seen[nod] = 1;
q.push(nod);
while(!q.empty())
{
int n = q.front();
q.pop();
for(int i = 0; i < v[n].size(); i++)
if(!seen[v[n][i]])
{
dist[v[n][i]] = dist[n] + 1;
seen[v[n][i]] = 1;
q.push(v[n][i]);
}
}
}
int main()
{
int nod;
f >> n >> m >> nod;
for(int i = 1; i <= m; i++)
{
int x,y,z;
f >> x >> y;
v[x].pb(y);
}
BFS(nod);
for(int i = 1; i <= n; i++)
g<<dist[i]<<" ";
}