96492024-02-23 17:07:44zeytonxA lehető legkevesebb metróval utazás (40 pont)cpp17Time limit exceeded 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;
}
SubtaskSumTestVerdictTimeMemory
base32/40
1Accepted0/03ms1828 KiB
2Accepted0/06ms3384 KiB
3Accepted2/23ms2348 KiB
4Accepted2/23ms2548 KiB
5Accepted2/23ms2944 KiB
6Accepted2/23ms2888 KiB
7Accepted2/23ms3208 KiB
8Accepted2/23ms3348 KiB
9Accepted2/24ms3764 KiB
10Accepted2/24ms3852 KiB
11Accepted2/23ms3720 KiB
12Accepted2/26ms4760 KiB
13Accepted2/26ms5008 KiB
14Accepted2/26ms5120 KiB
15Time limit exceeded0/2600ms10308 KiB
16Time limit exceeded0/2546ms10372 KiB
17Time limit exceeded0/2566ms10508 KiB
18Time limit exceeded0/2527ms10620 KiB
19Accepted2/24ms4920 KiB
20Accepted2/26ms5264 KiB
21Accepted2/24ms4772 KiB
22Accepted2/26ms5568 KiB