#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;
memset(visited
, 0, sizeof(visited
));
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;
}
curr = curr->next;
}
first++;
}
//freopen(FOUT, "w", stdout);
for(int i
= 0; i
< nodes
; ++i
) printf("%d ", queue
[i
]); }
int main(int argc, char const *argv[])
{
Node *list[SIZE];
int nodes, edges, i, j;
//freopen(FIN, "r", stdin);
scanf("%d %d" , &nodes
, &edges
);
while(edges--) {
addEdge(list, i,j);
}
bfs(list, nodes, 4);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYWxsb2MuaD4KI2luY2x1ZGUgPG1lbW9yeS5oPgojZGVmaW5lIEZJTiAiYmZzLmluIgojZGVmaW5lIEZPVVQgImJmcy5vdXQiCiNkZWZpbmUgU0laRSAxMDAxMDAKIApzdHJ1Y3QgVE5vZGUgewoJICAgaW50IGRhdGE7CgkgICBzdHJ1Y3QgVE5vZGUgKm5leHQ7Cn07CiAKdHlwZWRlZiBzdHJ1Y3QgVE5vZGUgTm9kZTsKIAogCnZvaWQgYWRkRWRnZShOb2RlKiogbGlzdCwgaW50IHgsIGludCB5KSB7CiAKCU5vZGUgKmMgPSAoTm9kZSopbWFsbG9jKHNpemVvZihOb2RlKSk7CiAKCSAgICAgIGMtPmRhdGEgPSB5OyAKCSAgICAgIGMtPm5leHQgPSBsaXN0W3hdOwoJICAgICAgbGlzdFt4XSA9IGM7IAp9CiAKdm9pZCBiZnMoTm9kZSAqKmxpc3QsIGludCBub2RlcywgaW50IHN0YXJ0KSB7CiAKICAgICAgICBpbnQgcXVldWVbIFNJWkUgXSwKIAogICAgICAgICAgICB2aXNpdGVkWyBTSVpFIF0sCiAKICAgICAgICAgICAgZmlyc3QgPSAwLAogCiAgICAgICAgICAgIGxhc3QgPSAwOwoKICAgICAgICBtZW1zZXQodmlzaXRlZCwgMCwgc2l6ZW9mKHZpc2l0ZWQpKTsKCiAgICAgICAgdmlzaXRlZFsgc3RhcnQgXSA9IDE7CiAKICAgICAgICBxdWV1ZVsgZmlyc3QgXSA9IHN0YXJ0OwogCiAgICAgICAgd2hpbGUoIGZpcnN0IDw9IGxhc3QgKSB7CiAKICAgICAgICAgICAgICBpbnQgbm9kZSA9IHF1ZXVlWyBmaXJzdCBdOwogCiAgICAgICAgICAgICAgTm9kZSpjdXJyID0gbGlzdFtub2RlXTsKIAogICAgICAgICAgICAgIHdoaWxlKGN1cnIpIHsKIAogICAgICAgICAgICAgICAgaWYodmlzaXRlZFtjdXJyLT5kYXRhXSA9PSAwKSB7CiAKICAgICAgICAgICAgICAgICAgIHF1ZXVlWysrbGFzdF0gPSBjdXJyLT5kYXRhOwogCiAgICAgICAgICAgICAgICAgICB2aXNpdGVkW2N1cnItPmRhdGFdID0gMTsKIAogICAgICAgICAgICAgICAgfSAgICAgCiAgICAgICAgICAgICAgICBjdXJyID0gY3Vyci0+bmV4dDsKICAgICAgICAgICAgICB9CiAKICAgICAgICAgICAgICBmaXJzdCsrOyAgICAgICAgICAgICAgICAgICAgIAogCiAgICAgICAgfQogCiAgICAgICAgLy9mcmVvcGVuKEZPVVQsICJ3Iiwgc3Rkb3V0KTsKIAogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBub2RlczsgKytpKSBwcmludGYoIiVkICIsIHF1ZXVlW2ldKTsKfQogCmludCBtYWluKGludCBhcmdjLCBjaGFyIGNvbnN0ICphcmd2W10pCnsKCU5vZGUgKmxpc3RbU0laRV07CgkKICAJaW50IG5vZGVzLCBlZGdlcywgaSwgajsgCiAKICAgIC8vZnJlb3BlbihGSU4sICJyIiwgc3RkaW4pOwogCiAgICBzY2FuZigiJWQgJWQiICwgJm5vZGVzLCAmZWRnZXMpOyAKIAogICAgd2hpbGUoZWRnZXMtLSkgewogCiAgICAgICAgICBzY2FuZigiJWQgJWQiICwgJmksICZqKTsKIAogICAgICAgICAgYWRkRWRnZShsaXN0LCBpLGopOwogICAgfQogCiAgICBiZnMobGlzdCwgbm9kZXMsIDQpOwoJcmV0dXJuIDA7Cn0=