48862023-04-05 23:02:36SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Time limit exceeded 32/40568ms7720 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;
VPII p;

int main()
{
	cin >> n >> m >> source >> dest;
	a.RS(n + 1);
	FOR(i, 1, n)
	{
		int c;
		cin >> c;
		FOR(j, 1, c) 
		{
			int x;
			cin >> x;
			a[i].PB(x);
		}
	}

	d.RS(m + 1, -1);
	p.RS(m + 1);
	queue<int> fifo;
	fifo.push(source);
	d[source] = 0;
	while (!fifo.empty())
	{
		int node = fifo.front();
		fifo.pop();
		FOR(i, 1, n)
		{
			bool found = false;
			for (int x : a[i])
			{
				if (x == node)
				{
					found = true;
					break;
				}
			}
			if (found)
			{
				for (int next : a[i])
					if (d[next] == -1)
					{
						d[next] = d[node] + 1;
						p[next] = {node, i};
						if (next == dest) break;
						fifo.push(next);
					}
				if (d[dest] != -1) break;
			}
		}
		if (d[dest] != -1) break;
	}

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

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base32/40
1Accepted0/03ms1812 KiB
2Accepted0/0104ms2504 KiB
3Accepted2/23ms2220 KiB
4Accepted2/23ms2424 KiB
5Accepted2/23ms2660 KiB
6Accepted2/22ms2880 KiB
7Accepted2/26ms3148 KiB
8Accepted2/24ms3376 KiB
9Accepted2/24ms3508 KiB
10Accepted2/224ms3404 KiB
11Accepted2/24ms3304 KiB
12Accepted2/2127ms3852 KiB
13Accepted2/265ms3888 KiB
14Accepted2/2103ms3804 KiB
15Time limit exceeded0/2550ms7412 KiB
16Time limit exceeded0/2564ms7608 KiB
17Time limit exceeded0/2568ms7720 KiB
18Time limit exceeded0/2560ms7700 KiB
19Accepted2/246ms4188 KiB
20Accepted2/234ms4236 KiB
21Accepted2/220ms4272 KiB
22Accepted2/252ms4504 KiB