48882023-04-05 23:06:18SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Időlimit túllépés 32/40583ms8244 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, fifo;
VPII p;

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	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);
	fifo.RS(m + 1);
	int l = 1, r = 1;
	fifo[1] = source;
	d[source] = 0;
	while (l <= r)
	{
		int node = fifo[l];
		++l;
		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[++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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base32/40
1Elfogadva0/03ms1828 KiB
2Elfogadva0/097ms2544 KiB
3Elfogadva2/23ms2308 KiB
4Elfogadva2/22ms2476 KiB
5Elfogadva2/23ms2844 KiB
6Elfogadva2/23ms2984 KiB
7Elfogadva2/24ms3256 KiB
8Elfogadva2/23ms3576 KiB
9Elfogadva2/23ms3784 KiB
10Elfogadva2/221ms3868 KiB
11Elfogadva2/23ms3864 KiB
12Elfogadva2/2118ms4324 KiB
13Elfogadva2/261ms4548 KiB
14Elfogadva2/296ms4604 KiB
15Időlimit túllépés0/2574ms8108 KiB
16Időlimit túllépés0/2563ms8244 KiB
17Időlimit túllépés0/2560ms8208 KiB
18Időlimit túllépés0/2583ms8236 KiB
19Elfogadva2/239ms4648 KiB
20Elfogadva2/230ms4776 KiB
21Elfogadva2/217ms4608 KiB
22Elfogadva2/248ms4864 KiB