Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- struct Grafo
- {
- vector <vector <int> > adj;
- vector <int> d;
- vector <bool> v;
- void leer()
- {
- int n, m, a, b;
- cin >> n >> m;
- adj = vector <vector <int> > (n+1, vector <int> ());
- d = vector <int> (n+1, 999999999);
- v = vector <bool> (n+1, false);
- for(int i=0; i<m; i++)
- {
- cin >> a >> b;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- int s;
- cin >> s;
- queue <int> cola;
- cola.push(s);
- d[s] = 0;
- while(cola.size())
- {
- s = cola.front();
- cola.pop();
- v[s] = true;
- for(int i=0; i<adj[s].size(); i++)
- if(d[adj[s][i]] > d[s]+1)
- {
- d[adj[s][i]] = d[s]+1;
- cola.push(adj[s][i]);
- }
- }
- for(int i=1; i<=n; i++)
- {
- if(d[i] == 999999999)
- cout << -1 << " ";
- else if(d[i])
- cout << d[i]*6 << " ";
- }
- cout << endl;
- }
- };
- int main()
- {
- int tc;
- cin >> tc;
- while(tc--)
- {
- Grafo g;
- g.leer();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment