-
Notifications
You must be signed in to change notification settings - Fork 0
/
HackerRank.cpp
72 lines (65 loc) · 1.27 KB
/
HackerRank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
#include<bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin);
#define write() freopen("output.txt", "w", stdout);
#define ll long long int
#define pb push_back
vector<int>adj[1009];
int main()
{
#ifdef asiuzzaman
read(); write();
#endif
int t,n,m,x,y,s,i;
cin>>t;
while(t--)
{
cin>>n>>m;
for(i=0;i<n+3;i++)
adj[i].clear();
for(i=0;i<m;i++)
{
cin>>x>>y;
adj[x].pb(y);
adj[y].pb(x);
}
cin>>s;
bool vis[n+1];
memset(vis,false,sizeof(vis));
int dist[n+1];
memset(dist,0,sizeof(dist));
list<int>q;
q.push_back(s);
vis[s]=true;
dist[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop_front();
for(i=0;i<adj[u].size();i++)
{
int v= adj[u][i];
if(!vis[v])
{
vis[v]=true;
dist[v]=dist[u]+6;
q.push_back(v);
}
}
}
for(i=1;i<=n;i++)
{
if(i!=s)
{
if(dist[i]==0)
cout<<"-1 ";
else
cout<<dist[i]<<" ";
}
}
cout<<endl;
}
return 0;
}