Pagini recente » Cod sursa (job #1624385) | Cod sursa (job #1073113) | Cod sursa (job #213447) | Cod sursa (job #172310) | Cod sursa (job #681835)
Cod sursa(job #681835)
using namespace std;
#include <set>
#include <map>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <utility>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
#define oo (1<<30)
#define f first
#define s second
#define II inline
#define db double
#define ll long long
#define pb push_back
#define mp make_pair
#define Size(V) ((ll)(V.size()))
#define all(V) (V).begin() , (V).end()
#define CC(V) memset((V),0,sizeof((V)))
#define CP(A,B) memcpy((A),(B),sizeof((B)))
#define FOR(i,a,b) for(ll (i)=(a);(i)<=(b);++(i))
#define REP(i, N) for (ll (i)=0;(i)<(ll)(N);++(i))
#define FORit(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define printll(x) printf("%lld",(x))
#define printsp() printf(" ")
#define newline() printf("\n")
#define readll(x) scanf("%lld",&(x))
#define debugll(x) fprintf(stderr,"%lld\n",(x))
#define IN "code.in"
#define OUT "code.out"
#define ONLINE_JUDGE
int nodeCount = 0;
int edgeCount = 0;
int origin = 0;
static const int MAX = 100010;
list<int> neighbors[MAX];
//indexing from 1, since node labels are >0
int q[MAX];
int cost[MAX];
int queueEnd = 0;
void buildGraph()
{
int x;
int y;
for (int i = 0; i < edgeCount; i++)
{
cin >> x >> y;
neighbors[x].push_back(y);
}
}
void bfs(int origin)
{
queueEnd = 1;
q[queueEnd] = origin;
memset(cost, -1, sizeof(cost));
cost[origin] = 0;
for (int i = 1; i <= queueEnd; i++)
{
list<int>::iterator it;
list<int>* neighs = &neighbors[q[i]];
for (it = neighs->begin(); it != neighs->end(); it++)
{
// cerr << "cost of " << *it << " is " << cost[*it] << endl;
// cerr << "PArent is " << q[i] << endl;
if (cost[*it] == -1)
{
queueEnd++;
q[queueEnd] = *it;
// cerr << "cost of " << *it << " is " << cost[q[i]] + 1 << endl;
cost[*it] = cost[q[i]] + 1;
}
}
}
}
void solve(int test) {
cin >> nodeCount >> edgeCount >> origin;
buildGraph();
bfs(origin);
for (int i = 1; i <= nodeCount; i++)
{
cout << cost[i] << " ";
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen(IN,"r",stdin);
freopen(OUT,"w",stdout);
#endif
solve(1);
return 0;
}