#include <iostream>
#include <vector>
// #include <algorithm>
#include <queue>
// #include <string>
// #include <bitset>
using namespace std;int main(){
int q;
cin>>q;
while(q--){
int n,e;
cin>>n>>e;
vector<vector<int> > a(n+1,vector<int>());
vector<int> dist(n+1,-1);
for(int i=0;i<e;i++){
int x,y;
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
int s;
cin>>s;
dist[s]=0;
queue<int> q;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=0;i<a[u].size();i++){
if(dist[a[u][i]]==-1){
dist[a[u][i]]=dist[u]+6;
q.push(a[u][i]);
}
}
}
int i=1;
if(s!=1){
cout<<dist[1];
i=2;
}
else {
cout<<dist[2];
i=3;
}
for(;i<=n;i++){
if(i==s)continue;
else cout<<' '<<dist[i];
}
cout<<endl;
}
}
#include <vector>
// #include <algorithm>
#include <queue>
// #include <string>
// #include <bitset>
using namespace std;int main(){
int q;
cin>>q;
while(q--){
int n,e;
cin>>n>>e;
vector<vector<int> > a(n+1,vector<int>());
vector<int> dist(n+1,-1);
for(int i=0;i<e;i++){
int x,y;
cin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
int s;
cin>>s;
dist[s]=0;
queue<int> q;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=0;i<a[u].size();i++){
if(dist[a[u][i]]==-1){
dist[a[u][i]]=dist[u]+6;
q.push(a[u][i]);
}
}
}
int i=1;
if(s!=1){
cout<<dist[1];
i=2;
}
else {
cout<<dist[2];
i=3;
}
for(;i<=n;i++){
if(i==s)continue;
else cout<<' '<<dist[i];
}
cout<<endl;
}
}
Comments
Post a Comment