#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#define FIN "bfs.in"
#define FOUT "bfs.out"
#define SIZE 100100
struct TNode {
int data;
struct TNode *next;
};
typedef struct TNode Node;
void addEdge(Node**list, int x, int y) {
Node
*c
= (Node
*)malloc(sizeof(Node
));
c->data = y;
c->next = list[x];
list[x] = c;
}
void bfs(Node **list, int nodes, int start) {
int queue[ SIZE ],
visited[ SIZE ],
first = 0,
last = 0,
costs[nodes+1];
memset(visited
, 0, sizeof(visited
));
memset(costs
, -1, sizeof(costs
));
costs[start] = 0;
visited[ start ] = 1;
queue[ first ] = start;
while( first <= last ) {
int node = queue[ first ];
Node*curr = list[node];
while(curr) {
if(visited[curr->data] == 0) {
queue[++last] = curr->data;
visited[curr->data] = 1;
costs[curr->data] = costs[node] + 1;
}
curr = curr->next;
}
first++;
}
//freopen(FOUT, "w", stdout);
for(int i
= 1; i
<= nodes
; ++i
) printf("%d ", costs
[i
]); }
int main(int argc, char const *argv[])
{
int nodes, edges, s, i, j;
Node *list[SIZE];
//freopen(FIN, "r", stdin);
scanf("%d %d %d" , &nodes
, &edges
, &s
);
while(edges--) {
addEdge(list, i,j);
}
bfs(list, nodes, s);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KI2luY2x1ZGUgPG1lbW9yeS5oPgojZGVmaW5lIEZJTiAiYmZzLmluIgojZGVmaW5lIEZPVVQgImJmcy5vdXQiCiNkZWZpbmUgU0laRSAxMDAxMDAKCnN0cnVjdCBUTm9kZSB7CgkgICBpbnQgZGF0YTsKCSAgIHN0cnVjdCBUTm9kZSAqbmV4dDsKfTsKCnR5cGVkZWYgc3RydWN0IFROb2RlIE5vZGU7CgoKdm9pZCBhZGRFZGdlKE5vZGUqKmxpc3QsIGludCB4LCBpbnQgeSkgewoKCU5vZGUgKmMgPSAoTm9kZSopbWFsbG9jKHNpemVvZihOb2RlKSk7CgoJICAgICAgYy0+ZGF0YSA9IHk7IAoJICAgICAgYy0+bmV4dCA9IGxpc3RbeF07CgkgICAgICBsaXN0W3hdID0gYzsgCn0KCnZvaWQgYmZzKE5vZGUgKipsaXN0LCBpbnQgbm9kZXMsIGludCBzdGFydCkgewoKICAgICAgICBpbnQgcXVldWVbIFNJWkUgXSwKCiAgICAgICAgICAgIHZpc2l0ZWRbIFNJWkUgXSwKCiAgICAgICAgICAgIGZpcnN0ID0gMCwKCiAgICAgICAgICAgIGxhc3QgPSAwLAoKICAgICAgICAgICAgY29zdHNbbm9kZXMrMV07CiAgICAgICAgCiAgICAgICAgbWVtc2V0KHZpc2l0ZWQsIDAsIHNpemVvZih2aXNpdGVkKSk7CgogICAgICAgIG1lbXNldChjb3N0cywgLTEsIHNpemVvZihjb3N0cykpOwoKICAgICAgICBjb3N0c1tzdGFydF0gPSAwOwoKICAgICAgICB2aXNpdGVkWyBzdGFydCBdID0gMTsKCiAgICAgICAgcXVldWVbIGZpcnN0IF0gPSBzdGFydDsKCiAgICAgICAgd2hpbGUoIGZpcnN0IDw9IGxhc3QgKSB7CgogICAgICAgICAgICAgIGludCBub2RlID0gcXVldWVbIGZpcnN0IF07CgogICAgICAgICAgICAgIE5vZGUqY3VyciA9IGxpc3Rbbm9kZV07CgogICAgICAgICAgICAgIHdoaWxlKGN1cnIpIHsKCiAgICAgICAgICAgICAgICBpZih2aXNpdGVkW2N1cnItPmRhdGFdID09IDApIHsKCiAgICAgICAgICAgICAgICAgICBxdWV1ZVsrK2xhc3RdID0gY3Vyci0+ZGF0YTsKCiAgICAgICAgICAgICAgICAgICB2aXNpdGVkW2N1cnItPmRhdGFdID0gMTsKICAgICAgICAKICAgICAgICAgICAgICAgICAgIGNvc3RzW2N1cnItPmRhdGFdID0gY29zdHNbbm9kZV0gKyAxOwogICAgICAgICAKICAgICAgICAgICAgICAgIH0gICAgIAogICAgICAgICAgICAgICAgY3VyciA9IGN1cnItPm5leHQ7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgICBmaXJzdCsrOyAgICAgICAgICAgICAgICAgICAgIAoKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgLy9mcmVvcGVuKEZPVVQsICJ3Iiwgc3Rkb3V0KTsKICAgICAgICAKICAgICAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG5vZGVzOyArK2kpIHByaW50ZigiJWQgIiwgY29zdHNbaV0pOwp9CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKQp7CiAgCWludCBub2RlcywgZWRnZXMsIHMsIGksIGo7IAogIAlOb2RlICpsaXN0W1NJWkVdOwoKICAgIC8vZnJlb3BlbihGSU4sICJyIiwgc3RkaW4pOwoKICAgIHNjYW5mKCIlZCAlZCAlZCIgLCAmbm9kZXMsICZlZGdlcywgJnMpOyAKICAgIAogICAgd2hpbGUoZWRnZXMtLSkgewoKICAgICAgICAgIHNjYW5mKCIlZCAlZCIgLCAmaSwgJmopOwogICAgICAgICAgCiAgICAgICAgICBhZGRFZGdlKGxpc3QsIGksaik7CiAgICB9CgogICAgYmZzKGxpc3QsIG5vZGVzLCBzKTsKCXJldHVybiAwOwp9