48932023-04-05 23:31:46SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Accepted 40/40112ms25516 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;
vector <VI> a;
VI d, d2, fifo, fifo2;
VPII p, p2;
VVI corresponding;

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);
	FOR(i, 1, n)
	{
		int c;
		cin >> c;
		FOR(j, 1, c) 
		{
			int x;
			cin >> x;
			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;
	int meet = -1;
	while (l <= r || l2 <= r2)
	{
		if (l <= r)
		{
			int node = fifo[l];
			++l;
			for (int i : corresponding[node])
			{
				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])
			{
				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;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1696 KiB
2Accepted0/06ms3720 KiB
3Accepted2/22ms1948 KiB
4Accepted2/23ms2184 KiB
5Accepted2/23ms2276 KiB
6Accepted2/23ms2288 KiB
7Accepted2/24ms2716 KiB
8Accepted2/23ms3188 KiB
9Accepted2/24ms3924 KiB
10Accepted2/24ms3940 KiB
11Accepted2/23ms3480 KiB
12Accepted2/26ms5024 KiB
13Accepted2/26ms5208 KiB
14Accepted2/26ms5172 KiB
15Accepted2/2112ms25500 KiB
16Accepted2/2112ms25372 KiB
17Accepted2/2112ms25360 KiB
18Accepted2/2112ms25516 KiB
19Accepted2/24ms4844 KiB
20Accepted2/24ms5064 KiB
21Accepted2/24ms4552 KiB
22Accepted2/26ms5596 KiB