95312024-02-22 17:11:451478A lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 30/40381ms63652 KiB
#include <bits/stdc++.h>  
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
ll MOD = 1e9 + 7;

const int N_MAX = 200;
const int M_MAX = 1e5;

int n, m, s, e;
vector<set<int>> stops(M_MAX + 1);
bool connections[N_MAX + 1][N_MAX + 1];

int main()
{

	cin >> n >> m >> s >> e;
	for(int i = 1; i <= n; i++){
		int x;
		cin >> x;
		while(x--){
			int a;
			cin >> a;
			stops[a].insert(i);
		}
	}

	//egy vonalon vannak

	for(auto it:stops[s]){
		if(stops[e].find(it) != stops[e].end()){
			cout << 1 << '\n' << it;
			return 0;
		}
	}

	//kulon vonalon vannak
	for(int i = 1; i <= m; i++){
		for(auto it:stops[i]){
			for(auto it2:stops[i]){
				if(it == it2){
					continue;
				}
				connections[it][it2] = 1;
			}
		}
	}

	queue<int> q;
	vector<int> parent(N_MAX, N_MAX + 5);

	for(auto it:stops[s]){
		q.push(it);
		parent[it] = 0;
	}

	while(!q.empty()){

		int t = q.front();
		q.pop();

		for(int i = 1; i <= n; i++){
			if(connections[t][i] && parent[i] == N_MAX + 5){
				
				parent[i] = t;
				q.push(i);

				if(stops[e].find(i) != stops[e].end()){
					stack<int> ans;
					ans.push(i);
					while(parent[i]){
						ans.push(parent[i]);
						i = parent[i];
					}

					cout << ans.size() << '\n';
					while(!ans.empty()){
						cout << ans.top() << " ";
						ans.pop();
					}
					return 0;
				}
			}
		}
	}

	cout << -1;

	/*
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			cout << connections[i][j] << " ";
		}
		cout << '\n';
	}*/


}
SubtaskSumTestVerdictTimeMemory
base30/40
1Accepted0/06ms11300 KiB
2Accepted0/010ms12372 KiB
3Accepted2/26ms11400 KiB
4Accepted2/26ms11336 KiB
5Accepted2/26ms11476 KiB
6Accepted2/24ms11348 KiB
7Accepted2/27ms12064 KiB
8Accepted2/27ms12012 KiB
9Accepted2/28ms12396 KiB
10Accepted2/29ms12612 KiB
11Accepted2/27ms12656 KiB
12Wrong answer0/212ms13616 KiB
13Accepted2/212ms13872 KiB
14Accepted2/210ms13688 KiB
15Runtime error0/2381ms63652 KiB
16Runtime error0/2352ms63620 KiB
17Runtime error0/2374ms63620 KiB
18Runtime error0/2351ms63356 KiB
19Accepted2/28ms13752 KiB
20Accepted2/29ms13988 KiB
21Accepted2/28ms13400 KiB
22Accepted2/212ms14264 KiB