Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int G[580][580];
- int32_t main() {
- int t, l, o, d;
- cin >> t >> l >> o >> d; o--; d--;
- while(l--) {
- int c;
- cin >> c;
- vector<int> L(c);
- for(int j = 0; j < c; j++)
- cin >> L[j], L[j]--;
- for(int j = 0; j < c; j++)
- for(int k = j + 1; k < c; k++)
- G[L[j]][L[k]] = G[L[k]][L[j]] = 1;
- }
- queue<int> q;
- vector<int> dist(t + 1, -1);
- dist[o] = 0;
- q.push(o);
- while(!q.empty()) {
- int u = q.front();
- q.pop();
- for(int w = 0; w < t; w++)
- if(G[u][w] and dist[w] == -1) {
- dist[w] = dist[u] + 1;
- q.push(w);
- }
- }
- cout << dist[d] << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement