Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct node{
- vector <int> polaczenia;
- int self = 10000000;
- };
- node graph[1000000];
- void BFS(int START = 1, int value = -1)
- {
- graph[START].self = value + 1;
- int ile;
- for(int i = 0; i < graph[START].polaczenia.size(); i++){
- ile = graph[START].polaczenia[i];
- if(graph[START].self + 1 < graph[ile].self){
- BFS(ile,graph[START].self);
- }
- }
- }
- int main()
- {
- int n,m,from, to, cost;
- cin >> n >> m;
- for(int i = 0;i < m; i++)
- {
- cin >> from >> to;
- graph[from].polaczenia.push_back(to);
- graph[to].polaczenia.push_back(from);
- }
- BFS();
- for(int i = 1; i <= n; i++)
- if(graph[i].self != 10000000)
- cout << graph[i].self << endl;
- else
- cout << -1 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement