9645 2024-02-23 17:01:14 zeytonx A lehető legkevesebb metróval utazás (40 pont) cpp17 Hibás válasz 0/40 130ms 42552 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef set<ll> sll;
typedef queue<ll> qll;
typedef priority_queue<ll> pqll;
typedef vector<vll> v2ll;
typedef map<ll,ll> mll;
typedef vector<pll> vpll;
typedef map<pll,ll> mpll;
typedef vector<vpll> v2pll;
typedef vector<sll> v2sll;
#define fs first
#define sc second
#define pb push_back

const ll MOD = 1e9+7;

void solve()
{
ll n, m, ind, erk;
cin >> n >> m >> ind >> erk;
ind--; erk--;
v2ll g(n);
v2ll st(m), ls(n);
for(ll i = 0; i < n; i++)
{
ll pc;
cin >> pc;
ls[i].resize(pc);
for(ll &j : ls[i])
{
cin >> j; j--;
st[j].pb(i);
}
}

return;

for(ll i = 0; i < m; i++)
{
if(!st[i].empty() && st[i].size() > 1)
for(ll j = 0; j < st[i].size(); j++)
for(ll l = j+1; l < st[i].size(); l++)
{
g[st[i][j]].pb(st[i][l]);
g[st[i][l]].pb(st[i][j]);
}
}

qll q;
vll used(n, -1);

for(ll i : st[ind])
{
q.push(i);
used[i] = -2;
}

ll ans = -1;

while(!q.empty())
{
ll p = q.front();
q.pop();
if(find(st[erk].begin(), st[erk].end(), p) != st[erk].end())
{
ans = p;
break;
}
for(ll i : g[p])
{
if(used[i] == -1)
{
q.push(i);
used[i] = p;
}
}
}

if(ans == -1)
{
cout << -1 << endl;
return;
}

vll v_ans;
while(ans >= 0)
{
v_ans.pb(ans);
ans = used[ans];
}
reverse(v_ans.begin(), v_ans.end());

cout << v_ans.size() << "\n";
for(ll i : v_ans)
cout << i+1 << " ";
cout << endl;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll t = 1;
//cin >> t;
while(t--)
solve();
cout << endl;
return 0;
}