Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- #include<vector>
- #include<string.h>
- using namespace std;
- const int N = 6e2;
- vector<int> graph[2*N];
- int father[2*N];
- bool viz[2*N];
- int T, L, O, D, C, x;
- void BFS(int O){
- memset(father, -1, sizeof(father));
- memset(viz, false, sizeof(viz));
- int at, neighbor;
- queue<int> q;
- q.push(O);
- while(q.size()){
- at = q.front();
- viz[at] = true;
- q.pop();
- for(int i=0; i < graph[at].size(); i++){
- neighbor = graph[at][i];
- if(!viz[neighbor]){
- q.push(neighbor);
- father[neighbor] = at;
- }
- }
- }
- }
- int solve(int O, int D){
- int at = D;
- int qtd = 0;
- while(father[at] != O){
- at = father[at];
- qtd++;
- }
- return qtd;
- }
- int main(){
- cin >> T >> L >> O >> D;
- for(int i=0; i < L; i++){
- cin >> C;
- for(int j=0; j < C; j++){
- cin >> x;
- graph[N+i].push_back(x);
- graph[x].push_back(N+i);
- }
- }
- BFS(O);
- cout << 1 + solve(O, D)/2 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement