48922023-04-05 23:29:26SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Accepted 40/4035ms26300 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;
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;
	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/03ms1828 KiB
2Accepted0/04ms3904 KiB
3Accepted2/23ms2232 KiB
4Accepted2/23ms2444 KiB
5Accepted2/23ms2880 KiB
6Accepted2/23ms2868 KiB
7Accepted2/23ms3316 KiB
8Accepted2/23ms3756 KiB
9Accepted2/24ms4528 KiB
10Accepted2/23ms4624 KiB
11Accepted2/23ms4056 KiB
12Accepted2/24ms5328 KiB
13Accepted2/24ms5456 KiB
14Accepted2/24ms5432 KiB
15Accepted2/234ms25592 KiB
16Accepted2/235ms26072 KiB
17Accepted2/235ms26300 KiB
18Accepted2/235ms26084 KiB
19Accepted2/24ms5400 KiB
20Accepted2/24ms5828 KiB
21Accepted2/23ms5184 KiB
22Accepted2/24ms6060 KiB