48902023-04-05 23:12:57SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Time limit exceeded 32/40601ms14128 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

#define IN_FILE "a.in"
#define OUT_FILE "a.out"

int n, m, source, dest;
vector <VI> a;
VI d, fifo;
VPII p;
VVI corresponding;
string s;

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);
		stringstream ss(s);
		int c;
		ss >> c;
		FOR(j, 1, c) 
		{
			int x;
			ss >> x;
			a[i].PB(x);
			corresponding[x].PB(i);
		}
	}

	d.RS(m + 1, -1);
	p.RS(m + 1);
	fifo.RS(m + 1);
	int l = 1, r = 1;
	fifo[1] = source;
	d[source] = 0;
	while (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 (next == dest) break;
					fifo[++r] = 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/03ms1828 KiB
2Accepted0/06ms3540 KiB
3Accepted2/23ms2228 KiB
4Accepted2/22ms2316 KiB
5Accepted2/23ms2660 KiB
6Accepted2/23ms2656 KiB
7Accepted2/24ms3056 KiB
8Accepted2/23ms3408 KiB
9Accepted2/24ms3804 KiB
10Accepted2/26ms3748 KiB
11Accepted2/23ms3508 KiB
12Accepted2/27ms4516 KiB
13Accepted2/26ms4776 KiB
14Accepted2/26ms4728 KiB
15Time limit exceeded0/2601ms13648 KiB
16Time limit exceeded0/2580ms13928 KiB
17Time limit exceeded0/2577ms14120 KiB
18Time limit exceeded0/2572ms14128 KiB
19Accepted2/24ms4856 KiB
20Accepted2/24ms5412 KiB
21Accepted2/24ms4656 KiB
22Accepted2/26ms5416 KiB