9531 2024-02-22 17:11:45 1478 A lehető legkevesebb metróval utazás (40 pont) cpp17 Hibás válasz 30/40 381ms 63652 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
ll MOD = 1e9 + 7;

const int N_MAX = 200;
const int M_MAX = 1e5;

int n, m, s, e;
vector<set<int>> stops(M_MAX + 1);
bool connections[N_MAX + 1][N_MAX + 1];

int main()
{

cin >> n >> m >> s >> e;
for(int i = 1; i <= n; i++){
int x;
cin >> x;
while(x--){
int a;
cin >> a;
stops[a].insert(i);
}
}

//egy vonalon vannak

for(auto it:stops[s]){
if(stops[e].find(it) != stops[e].end()){
cout << 1 << '\n' << it;
return 0;
}
}

//kulon vonalon vannak
for(int i = 1; i <= m; i++){
for(auto it:stops[i]){
for(auto it2:stops[i]){
if(it == it2){
continue;
}
connections[it][it2] = 1;
}
}
}

queue<int> q;
vector<int> parent(N_MAX, N_MAX + 5);

for(auto it:stops[s]){
q.push(it);
parent[it] = 0;
}

while(!q.empty()){

int t = q.front();
q.pop();

for(int i = 1; i <= n; i++){
if(connections[t][i] && parent[i] == N_MAX + 5){

parent[i] = t;
q.push(i);

if(stops[e].find(i) != stops[e].end()){
stack<int> ans;
ans.push(i);
while(parent[i]){
ans.push(parent[i]);
i = parent[i];
}

cout << ans.size() << '\n';
while(!ans.empty()){
cout << ans.top() << " ";
ans.pop();
}
return 0;
}
}
}
}

cout << -1;

/*
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << connections[i][j] << " ";
}
cout << '\n';
}*/

}