48912023-04-05 23:17:06SleepyOverlordA lehető legkevesebb metróval utazás (40 pont)cpp17Időlimit túllépés 32/40601ms14320 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, ptr;
vector <VI> a;
VI d, fifo;
VPII p;
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);
	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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base32/40
1Elfogadva0/03ms1700 KiB
2Elfogadva0/04ms3328 KiB
3Elfogadva2/22ms1952 KiB
4Elfogadva2/23ms2176 KiB
5Elfogadva2/23ms2872 KiB
6Elfogadva2/23ms2488 KiB
7Elfogadva2/24ms3292 KiB
8Elfogadva2/23ms3556 KiB
9Elfogadva2/24ms4252 KiB
10Elfogadva2/26ms4300 KiB
11Elfogadva2/23ms4036 KiB
12Elfogadva2/26ms4792 KiB
13Elfogadva2/24ms5092 KiB
14Elfogadva2/26ms5172 KiB
15Időlimit túllépés0/2601ms13724 KiB
16Időlimit túllépés0/2564ms14192 KiB
17Időlimit túllépés0/2573ms14320 KiB
18Időlimit túllépés0/2560ms14200 KiB
19Elfogadva2/24ms4936 KiB
20Elfogadva2/24ms5204 KiB
21Elfogadva2/23ms4724 KiB
22Elfogadva2/24ms5640 KiB