48952023-04-05 23:44:56SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Elfogadva 40/4037ms25868 KiB
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <unordered_set>
#include <unordered_map>
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 
#include <random>
#include <chrono>

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>
#include <climits>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count

int n, m, source, dest, ptr;
vector <VI> a;
VI d, d2, fifo, fifo2;
VPII p, p2;
VVI corresponding;
VB used;
string s;

int getInt()
{
	int ans = 0;
	while (!isdigit(s[ptr])) ++ptr;
	while (isdigit(s[ptr])) ans = ans * 10 + (s[ptr++] - '0');
	return ans;
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	cin >> n >> m >> source >> dest;
	corresponding.RS(m + 1);
	a.RS(n + 1);
	getline(cin, s);
	FOR(i, 1, n)
	{
		getline(cin, s);
		ptr = 0;
		int c = getInt();
		FOR(j, 1, c) 
		{
			int x = getInt();
			a[i].PB(x);
			corresponding[x].PB(i);
		}
	}

	d.RS(m + 1, -1);
	d2.RS(m + 1, -1);
	p.RS(m + 1);
	p2.RS(m + 1);
	fifo.RS(m + 1);
	fifo2.RS(m + 1);
	int l = 1, r = 1, l2 = 1, r2 = 1;
	fifo[1] = source;
	fifo2[1] = dest;
	d[source] = 0;
	d2[dest] = 0;
	used.RS(n + 1);
	int meet = -1;
	while (l <= r || l2 <= r2)
	{
		if (l <= r)
		{
			int node = fifo[l];
			++l;
			for (int i : corresponding[node])
			{
				if (used[i]) continue;
				used[i] = true;
				for (int next : a[i])
					if (d[next] == -1)
					{
						d[next] = d[node] + 1;
						p[next] = {node, i};
						if (d2[next] != -1) 
						{
							meet = next;
							break;
						}
						fifo[++r] = next;
					}
				if (meet != -1) break;
			}
			if (meet != -1) break;
		}
		if (l2 <= r2)
		{
			int node = fifo2[l2];
			++l2;
			for (int i : corresponding[node])
			{
				if (used[i]) continue;
				used[i] = true;
				for (int next : a[i])
					if (d2[next] == -1)
					{
						d2[next] = d2[node] + 1;
						p2[next] = {node, i};
						if (d[next] != -1) 
						{
							meet = next;
							break;
						}
						fifo2[++r2] = next;
					}
				if (meet != -1) break;
			}
			if (meet != -1) break;
		}
	}

	if (meet != -1)
	{
		cout << d[meet] + d2[meet] << endl;
		int node = meet;
		VI sol;
		while (node != source)
		{
			sol.PB(p[node].Y);
			node = p[node].X;
		}
		reverse(ALL(sol));
		node = meet;
		while (node != dest)
		{
			sol.PB(p2[node].Y);
			node = p2[node].X;
		}
		for (int x : sol) cout << x << " ";
	}
	else cout << -1;

	return 0;
}