85972024-01-22 21:42:03zeytonxA lehető legkevesebb metróval utazás (40 pont)cpp17Time limit exceeded 32/40584ms26272 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl "\n"
#define pll pair<ll,ll>
#define vll vector<ll>
#define mll map<ll,ll>
#define fs first
#define sc second

const ll MOD = 1e9+7;


void solve()
{
	ll n, m, ind, erk;
	cin >> n >> m >> ind >> erk;
    ind--; erk--;
	vector<unordered_set<ll>> l(n);
    vector<unordered_set<ll>> s(m);
    vector<unordered_set<ll>> e(n);
    vector<ll> erk_stat(n);
    for(ll i = 0; i < n; i++)
    {
        ll k;
        cin >> k;
        for(ll j = 0; j < k; j++)
        {
            ll inp;
            cin >> inp;
            inp--;
            if(inp == erk)
                erk_stat[i] = 1;
            l[i].insert(inp);
            for(ll p : s[inp])
            {
                e[i].insert(p);
                e[p].insert(i);
            }
            s[inp].insert(i);
        }
    }
    vector<ll> ans(n), ans_v(n);
    queue<ll> q;
    for(ll i : s[ind])
    {
        q.push(i);
        ans[i] = 1;
        ans_v[i] = -1;
    }
    while(!q.empty())
    {
        ll p = q.front();
        q.pop();
        if(erk_stat[p] != 1)
            for(ll p2 : e[p])
            {
                if(ans[p2] == 0 || ans[p2] > ans[p]+1)
                {
                    ans[p2] = ans[p]+1;
                    ans_v[p2] = p;
                    q.push(p2);
                }
            }
    }
    ll min_v = LLONG_MAX;
    ll min_index = 0;
    for(ll i : s[erk])
    {
        if(ans[i] < min_v && ans[i] != 0)
        {
            min_v = ans[i];
            min_index = i;
        }
    }

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

    cout << min_v << endl;
    vector<ll> rans;
    while(min_index != -1)
    {
        rans.push_back(min_index);
        min_index = ans_v[min_index];
    }
    for(ll i = rans.size()-1; i >= 0; i--)
    {
        cout << rans[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();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base32/40
1Accepted0/03ms1828 KiB
2Accepted0/08ms7400 KiB
3Accepted2/23ms2364 KiB
4Accepted2/23ms2468 KiB
5Accepted2/23ms3220 KiB
6Accepted2/23ms2712 KiB
7Accepted2/24ms4064 KiB
8Accepted2/24ms4296 KiB
9Accepted2/27ms5804 KiB
10Accepted2/26ms5488 KiB
11Accepted2/24ms4144 KiB
12Accepted2/29ms8680 KiB
13Accepted2/210ms9016 KiB
14Accepted2/29ms9104 KiB
15Time limit exceeded0/2584ms26060 KiB
16Time limit exceeded0/2583ms24508 KiB
17Time limit exceeded0/2579ms24664 KiB
18Time limit exceeded0/2574ms26272 KiB
19Accepted2/28ms8112 KiB
20Accepted2/28ms9468 KiB
21Accepted2/26ms6980 KiB
22Accepted2/210ms9896 KiB