MCPcopy Create free account
hub / github.com/E869120/math-algorithm-book / main

Function main

codes/cpp/Code_4_05_3.cpp:10–40  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8vector<int> G[100009];
9
10int main() {
11 // ����
12 cin >> N >> M;
13 for (int i = 1; i <= M; i++) {
14 cin >> A[i] >> B[i];
15 G[A[i]].push_back(B[i]);
16 G[B[i]].push_back(A[i]);
17 }
18
19 // ���D��T���̏������idist[i]=-1 �̂Ƃ��A�����B�̔��F���_�ł���j
20 for (int i = 1; i <= N; i++) dist[i] = -1;
21 queue<int> Q; // �L���[ Q ���`����
22 Q.push(1); dist[1] = 0; // Q �� 1 ��lj��i���� 1�j
23
24 // ���D��T��
25 while (!Q.empty()) {
26 int pos = Q.front(); // Q �̐擪�𒲂ׂ�i���� 2�j
27 Q.pop(); // Q �̐擪�����o���i���� 3�j
28 for (int i = 0; i < (int)G[pos].size(); i++) {
29 int nex = G[pos][i];
30 if (dist[nex] == -1) {
31 dist[nex] = dist[pos] + 1;
32 Q.push(nex); // Q �� nex ��lj��i���� 1�j
33 }
34 }
35 }
36
37 // ���_ 1 ����e���_�܂ł̍ŒZ�������o��
38 for (int i = 1; i <= N; i++) cout << dist[i] << endl;
39 return 0;
40}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected