-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsol.cpp
47 lines (42 loc) · 1 KB
/
sol.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
// Written by: wyattwismer
#include <bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
int n,m;
cin>>n>>m;
vector<set<int>> g(n);
while(m--){
int u,v;
cin>>u>>v;
u--;v--;
g[u].insert(v);
g[v].insert(u);
}
int s;
cin>>s;
s--;
vector<int> dist(n);
queue<int> q;
q.push(s);
set<int> unseen;
for(int i=0; i<n; i++) if(i!=s) unseen.insert(i);
while (!unseen.empty()){
int x = q.front();
q.pop();
vector<int> chk(unseen.begin(), unseen.end());
for (int y: chk){
if (g[x].find(y) == g[x].end()){
unseen.erase(y);
q.push(y);
dist[y] = dist[x] + 1;
}
}
}
for(int i=0; i<n; i++) if(i != s)
cout << dist[i] << " ";
cout << endl;
}
}