147322025-01-30 10:05:39mateA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 24/40587ms32000 KiB
#include <bits/stdc++.h>
using namespace std;

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();
        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;
        }
    }
    

    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/01ms316 KiB
2Accepted0/0409ms976 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms316 KiB
5Accepted2/26ms508 KiB
6Accepted2/21ms316 KiB
7Accepted2/225ms552 KiB
8Accepted2/232ms580 KiB
9Accepted2/2175ms788 KiB
10Wrong answer0/2140ms564 KiB
11Accepted2/224ms536 KiB
12Time limit exceeded0/2587ms1076 KiB
13Time limit exceeded0/2587ms1076 KiB
14Accepted2/2439ms1016 KiB
15Runtime error0/2330ms32000 KiB
16Runtime error0/2324ms32000 KiB
17Runtime error0/2324ms32000 KiB
18Runtime error0/2331ms32000 KiB
19Accepted2/2181ms820 KiB
20Accepted2/2324ms1004 KiB
21Accepted2/274ms648 KiB
22Wrong answer0/2416ms996 KiB