48852023-04-05 22:45:42SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Runtime error 32/40305ms62772 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 <USI> 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].INS(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)
			if (a[i].CNT(node))
			{
				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/03ms2088 KiB
2Accepted0/064ms3532 KiB
3Accepted2/23ms2372 KiB
4Accepted2/23ms2564 KiB
5Accepted2/23ms2980 KiB
6Accepted2/22ms2864 KiB
7Accepted2/24ms3416 KiB
8Accepted2/24ms3668 KiB
9Accepted2/26ms4140 KiB
10Accepted2/28ms4160 KiB
11Accepted2/24ms3928 KiB
12Accepted2/265ms5256 KiB
13Accepted2/235ms5496 KiB
14Accepted2/261ms5476 KiB
15Runtime error0/2305ms62772 KiB
16Runtime error0/2301ms62580 KiB
17Runtime error0/2298ms62552 KiB
18Runtime error0/2298ms62400 KiB
19Accepted2/232ms5532 KiB
20Accepted2/223ms5792 KiB
21Accepted2/219ms5244 KiB
22Accepted2/234ms6012 KiB