Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
- vector<ll>g[10000];
- bool v[10000];
- ll dis[10000];
- void bfs(ll r)
- {
- queue<ll>qq;
- v[r]=1;
- dis[r]=0;
- qq.push(r);
- while(!qq.empty())
- {
- ll node=qq.front();
- qq.pop();
- for(ll i=0; i<g[node].size(); i++)
- {
- ll next=g[node][i];
- if(v[next]==0)
- {
- v[next]=1;
- dis[next]=dis[node]+1;
- qq.push(next);
- }
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- ll n,m,a,b,i,j,k,l,root;
- cin>>n>>m;
- for(i=0; i<n; i++)
- {
- g[i].clear();
- v[i]=0;
- }
- for(i=1; i<=m; i++)
- {
- cin>>a>>b;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- cin>>root;
- bfs(root);
- cout << "From node " <<root << endl;
- for (i = 1; i <= n; i++)
- {
- cout << "Distance of " << i << " is : " << dis[i] << endl;
- }
- return 0;
- }
Advertisement
RAW Paste Data
Copied
Advertisement