Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <queue>
- using namespace std;
- struct vert
- {
- int visited = 0;
- vector<int> points;
- int dist = 0;
- };
- void BFS(vector<vert> &V, int Start)
- {
- V[Start].visited = 1;
- queue<int> Q;
- Q.push(Start);
- while (!Q.empty())
- {
- int U = Q.front();
- Q.pop();
- for (int i = 0; i < V[U].points.size(); ++i)
- if (V[V[U].points[i]].visited == 0)
- {
- V[V[U].points[i]].visited = 1;
- V[V[U].points[i]].dist = V[U].dist + 1;
- Q.push(V[U].points[i]);
- }
- V[U].visited = 2;
- }
- }
- int main()
- {
- ifstream fin("pathbge1.in");
- ofstream fout("pathbge1.out");
- int N, M, S, E;
- fin >> N >> M;
- vector<vert> V(N);
- for (int i = 0; i < M; ++i)
- {
- fin >> S >> E;
- V[S - 1].points.push_back(E - 1);
- V[E - 1].points.push_back(S - 1);
- }
- BFS(V, 0);
- for (int i = 0; i < N; ++i)
- fout << V[i].dist << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement