95302024-02-22 17:09:521478A lehető legkevesebb metróval utazás (40 pont)cpp17Runtime error 32/40368ms63688 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()
{
	//ifstream cin("in.txt");

	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 + 1, 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
base32/40
1Accepted0/06ms11024 KiB
2Accepted0/010ms12516 KiB
3Accepted2/26ms11504 KiB
4Accepted2/26ms11456 KiB
5Accepted2/26ms11636 KiB
6Accepted2/26ms11720 KiB
7Accepted2/27ms12272 KiB
8Accepted2/27ms12272 KiB
9Accepted2/28ms12656 KiB
10Accepted2/28ms12760 KiB
11Accepted2/27ms12488 KiB
12Accepted2/212ms13780 KiB
13Accepted2/212ms13736 KiB
14Accepted2/212ms13600 KiB
15Runtime error0/2368ms63688 KiB
16Runtime error0/2361ms63452 KiB
17Runtime error0/2367ms63424 KiB
18Runtime error0/2361ms63408 KiB
19Accepted2/28ms13728 KiB
20Accepted2/29ms13872 KiB
21Accepted2/28ms13220 KiB
22Accepted2/210ms13868 KiB