96492024-02-23 17:07:44zeytonxA lehető legkevesebb metróval utazás (40 pont)cpp17Időlimit túllépés 32/40600ms10620 KiB
#include <bits/stdc++.h>

using namespace std;

typedef int 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--;
    v2sll g(n);
    v2ll st(m);
    for(ll i = 0; i < n; i++)
    {
        ll pc;
        cin >> pc;
        for(ll j = 0; j < pc; j++)
        {
            ll in; cin >> in; in--;
            st[in].pb(i);
        }
    }

    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]].insert(st[i][l]);
                    g[st[i][l]].insert(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;
}