121222024-12-02 22:33:05RRoliA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 28/40600ms7564 KiB
#include <bits/stdc++.h>
using namespace std;

int n, m, ind, erk;
vector<vector<int>> allomas;
vector<set<int>> szom;
set<int> kezd, veg;

int main() {
	cin >> n >> m >> ind >> erk;
    szom.resize(n+1);
    allomas.resize(m+1, vector<int>(0));
    for(int i = 1; i <= n; i++) {
        int a;
        cin >> a;
        for(int j = 1; j <= a; j++) {
            int b;
            cin >> b;
            allomas[b].push_back(i);
        }
    }
    for(int i = 1; i <= m; i++) {
        for(int j = 0; j < allomas[i].size(); j++) {
            for(int k = j+1; k < allomas[i].size(); k++) {
                szom[allomas[i][j]].insert(allomas[i][k]);
                szom[allomas[i][k]].insert(allomas[i][j]);
            }
        }
    }
    for(auto j : allomas[ind]) kezd.insert(j);
    for(auto j : allomas[erk]) veg.insert(j);

    int vsor[201], L[201], e = 0, u = -1, elso = -1;
    for(int i = 1; i <= n; i++) L[i] = -2;
    for(auto j : kezd) {
        u++;
        vsor[u] = j;
        L[j] = -1;
    }

    while(e <= u && elso == -1) {
        for(auto j : szom[vsor[e]]) {
            if(L[j] == -2) {
                u++;
                vsor[u] = j;
                L[j] = e;
                if(veg.count(j) == 1) {
                    elso = u;
                    break;
                }
            }
        }
        e++;
    }

    if(elso == -1) cout << -1;
    else {
        vector<int> sorrend = {vsor[elso]};
        while(L[sorrend[sorrend.size()-1]] != -1) sorrend.push_back(vsor[L[sorrend[sorrend.size()-1]]]);
        cout << sorrend.size() << '\n';
        for(int i = sorrend.size()-1; i >= 0; i--) cout << sorrend[i] << ' ';
    }

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base28/40
1Accepted0/01ms320 KiB
2Accepted0/07ms1080 KiB
3Wrong answer0/21ms320 KiB
4Accepted2/21ms320 KiB
5Wrong answer0/21ms320 KiB
6Accepted2/21ms320 KiB
7Accepted2/22ms332 KiB
8Accepted2/23ms568 KiB
9Accepted2/24ms776 KiB
10Accepted2/24ms568 KiB
11Accepted2/22ms320 KiB
12Accepted2/27ms1084 KiB
13Accepted2/26ms856 KiB
14Accepted2/26ms1056 KiB
15Time limit exceeded0/2600ms7472 KiB
16Time limit exceeded0/2600ms7440 KiB
17Time limit exceeded0/2600ms7480 KiB
18Time limit exceeded0/2600ms7564 KiB
19Accepted2/24ms824 KiB
20Accepted2/26ms824 KiB
21Accepted2/23ms568 KiB
22Accepted2/27ms1040 KiB