Untitled

mail@pastecode.io avatarunknown
plain_text
24 days ago
878 B
1
Indexable
Never
#include<bits/stdc++.h>
using namespace std;


const int N = 1e4;
vector<int> adj[N];
bool vis[N];
int level[N];



void bfs(int source){
    queue<int> que;
	que.push(source);
	vis[source] = true;
	while(!que.empty()){
		int par = que.front();
		que.pop();
		for(auto x : adj[par]){
			if(vis[x]) continue;
			que.push(x);
			vis[x] = true;
			level[x]=level[par]+1;
		}
	}
}



int main(){
    int t;
    cin>>t;
    while(t--){

	int v,e;
	cin >> v >> e;
	for(int i=1;i<=e;i++){
		int v1,v2;
		cin >> v1 >> v2;
		adj[v1].push_back(v2);
		adj[v2].push_back(v1);
	}
	int src;
	cin>>src;
	bfs(src);
	for(int i=1;i<=v;i++){
            if(i==src)continue;
        if(level[i]==0)cout<< "-1"<< " ";
        else cout<<level[i]*6<<" ";
	}
	cout<<endl;
    }
    memset(adj,0,sizeof(adj));
    memset(level,0,sizeof(level));
}