Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const ll N=1000000;
- vector<ll>adj[N+10];
- bool vis[N+100];
- ll n,m;
- ll level[N+1];
- void BFS(ll s)
- {
- vis[s]=true;
- level[s]=0;
- queue<ll>q;
- q.push(s);
- while(!q.empty())
- {
- ll u=q.front();
- q.pop();
- for(ll i=0;i<adj[u].size();i++)
- {
- if(vis[adj[u][i]]==false)
- {
- level[adj[u][i]]=level[u]+6;
- vis[adj[u][i]]=true;
- q.push(adj[u][i]);
- }
- }
- }
- }
- int main()
- {
- ll t;
- cin>>t;
- while(t--)
- {
- cin>>n>>m;
- ll a,b;
- for(ll i=0;i<m;i++)
- {
- cin>>a>>b;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- ll s;
- cin>>s;
- BFS(s);
- for(ll i=1;i<=n;i++)
- {
- if(s==i)continue;
- if(level[i])cout<<level[i]<<" ";
- else cout<<-1<<" ";
- }cout<<endl;
- for(ll i=1;i<=n;i++)
- {
- level[i]=0;
- adj[i].clear();
- vis[i]=false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement