147302025-01-30 09:56:45mateA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 24/40584ms32000 KiB
#include <bits/stdc++.h>
using namespace std;

vector <vector <int>> graf;
vector <set <int>> lines;
vector <vector <int>> szom;
vector <int> parent;

void bfs(int p, int ve){
    queue <int> q;
    q.push(p);
    parent[p] = p; 
    while(!q.empty()){
        int csucs = q.front();
        cerr << csucs << ' ';
        q.pop();
        for(int x : szom[csucs]){
            if(parent[x] == 0){
                q.push(x);
                parent[x] = csucs;
            }
            if(x == ve){
                return;
            }
        }
    }
}


int main() {
	int n,m,kezdo,veg; cin >> n >> m >> kezdo >> veg;
    szom.resize(n+1);
    lines.resize(n+1);
    parent.resize(n+1,0);
    int kez = 0,ve = 0;

    for(int i = 1; i <= n; i++){
        int a,b; cin >> a;
        for(int j = 0; j < a; j++){
            cin >> b;
            if(kezdo == b){
                kez = i;
            }
            if(veg == b){
                ve = i;
            }
            lines[i].insert(b);
        }
    }
    bool kesz = 0;
    for(int i = 1; i < n; i++){
        for(int j = i + 1;  j <= n; j++){
            for(int x : lines[i]){
                if(kesz)    break;
                for(int y :lines[j]){
                    if(x == y){
                        szom[i].push_back(j);
                        szom[j].push_back(i);
                        kesz = 1;
                        break;
                    }
                }
            }
            kesz = 0;
        }
    }
    for(int i = 1; i <= n; i++){
        for(int x : szom[i]){
            cerr << x << ' ';
        }
        cerr << '\n';
    }

    bfs(kez, ve);

    if(parent[ve] == 0){
        cout << -1;
        return 0;
    }
    int k = ve;
    vector <int> ans;
    ans.push_back(k);
    while(parent[k] != k){
        ans.push_back(parent[k]);
        k = parent[k];
    }
    reverse(ans.begin(),ans.end());
    cout << ans.size() << '\n';
    for(int x : ans){
        cout << x << ' ';
    }
}
SubtaskSumTestVerdictTimeMemory
base24/40
1Accepted0/01ms508 KiB
2Accepted0/0405ms820 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms316 KiB
5Accepted2/26ms316 KiB
6Accepted2/21ms316 KiB
7Accepted2/225ms552 KiB
8Accepted2/232ms580 KiB
9Accepted2/2177ms784 KiB
10Wrong answer0/2138ms740 KiB
11Accepted2/224ms536 KiB
12Time limit exceeded0/2578ms1268 KiB
13Time limit exceeded0/2584ms1120 KiB
14Accepted2/2437ms1016 KiB
15Runtime error0/2316ms32000 KiB
16Runtime error0/2319ms32000 KiB
17Runtime error0/2312ms32000 KiB
18Runtime error0/2317ms32000 KiB
19Accepted2/2182ms564 KiB
20Accepted2/2324ms820 KiB
21Accepted2/275ms648 KiB
22Wrong answer0/2414ms996 KiB