Pagini recente » Istoria paginii planificare/sedinta-20071128 | Cod sursa (job #2968256) | Cod sursa (job #2181130) | Cod sursa (job #3206431) | Cod sursa (job #3228072)
#include <bits/stdc++.h>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
InParser in("bfs.in");
ofstream out("bfs.out");
pair <int, int> much[1000001];
int last[100001];
int coada[100001], st, dr = -1;
int dist[100001];
int main()
{
int n, m, s, x, y;
in >> n >> m >> s;
for(int i = 1; i <= m; i++)
{
in >> x >> y;
much[i] = {y, last[x]};
last[x] = i;
}
for(int i = 1; i <= n; i++)
dist[i] = -1;
dist[s] = 0;
coada[++dr] = s;
while(st <= dr)
{
int x = coada[st++];
for(int y = last[x]; y != 0; y = much[y].second)
if(dist[much[y].first] == -1)
{
dist[much[y].first] = dist[x] + 1;
coada[++dr] = much[y].first;
}
}
for(int i = 1; i <= n; i++)
out << dist[i] << " ";
return 0;
}