151162025-02-13 09:25:40tamasmarkA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 18/40510ms1616 KiB
// leheto legkevesebb metro.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <deque>
#include <map>

using namespace std;
struct adat
{
    int lat,lep,veg,honnan;
    set<int>sz;
};
struct adat2
{
    int p;
    vector<int>sorsz;
};
map<int,vector<int> >csp;
vector<adat>x;
deque<int>q;
int akt;
int n, m, i, j, a, b,k,v;
void vissz(int csp)
{
    if (x[csp].honnan)
        vissz(x[csp].honnan);
    cout << csp << " ";
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> m >> n >> k >> v;
    x.resize(m + 1);
    for (i = 1; i <= m; ++i)
    {
        cin >> a;
        for (j = 1; j <= a; ++j)
        {
            cin >> b;
            if (k == b)
            {
                q.push_back(i);
                x[i].lat = true;
                x[i].lep = 1;
            }
            if (v == b)
            {
                x[i].veg = 1;
            }
            if (csp[b].empty())
            {
                csp[b].push_back(i);
            }
            else if(!csp[b].empty())
            {
                for (auto& e : csp[b])
                {
                    x[i].sz.insert(e);
                    x[e].sz.insert(i);
                }
            }
        }
    }

    while (!q.empty())
    {
        akt = q.front();
        q.pop_front();
        x[akt].lat = true;
        for (auto& e : x[akt].sz)
        {
            if (!x[e].lat)
            {
                q.push_back(e);
                x[e].lep = x[akt].lep + 1;
                x[e].honnan = akt;
            }
        }
    }

    int mini = 9999,c;
    for (i = 1; i <= m; ++i)
    {
        if (x[i].veg && x[i].lep < mini)
        {
            mini = x[i].lep;
            c = i;
        }
    }
    if (x[c].lep)
    {
        cout << mini << "\n";
        vissz(c);
    }
    else cout << -1;
    return 0;
}
/*
3 12 1 8
6 1 2 3 4 5 6
6 4 5 6 7 8 9
5 3 10 11 12 7

3 7 1 5
3 1 2 3
3 3 4 5
4 1 6 7 5

2 5 1 5
3 1 2 3
2 4 5
*/
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
base18/40
1Accepted0/01ms316 KiB
2Accepted0/06ms1336 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms508 KiB
5Accepted2/21ms316 KiB
6Accepted2/21ms316 KiB
7Accepted2/22ms564 KiB
8Accepted2/23ms748 KiB
9Accepted2/24ms1080 KiB
10Wrong answer0/24ms880 KiB
11Wrong answer0/22ms580 KiB
12Wrong answer0/210ms1416 KiB
13Wrong answer0/29ms1616 KiB
14Wrong answer0/27ms1552 KiB
15Time limit exceeded0/2504ms1252 KiB
16Time limit exceeded0/2510ms1244 KiB
17Time limit exceeded0/2504ms1244 KiB
18Time limit exceeded0/2509ms1260 KiB
19Wrong answer0/24ms1076 KiB
20Wrong answer0/26ms1332 KiB
21Accepted2/23ms820 KiB
22Accepted2/27ms1452 KiB