88852024-02-02 20:52:24bovizdbA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 8/40569ms8568 KiB
#include <bits/stdc++.h>
using namespace std;

int n, m, ind, erk;

vector<vector<bool>> v;
vector<vector<int>> vm;

int main()
{
	cin >> n >> m >> ind >> erk;
	v.resize(n, vector<bool>(n));
	vm.resize(m);
	for (int i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		for (int j = 0; j < a; j++)
		{
			int in;
			cin >> in;
			vm[in-1].push_back(i);
		}
	}
	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < vm[i].size(); j++)
		{
			for (int k = j+1; k < vm[i].size(); k++)
			{
				v[vm[i][j]][vm[i][k]] = 1;
				v[vm[i][k]][vm[i][j]] = 1;
			}
		}
	}
	vector<int> out(210);
	for (int a : vm[ind-1])
	{
		vector <int> c(n);
		vector<int> h(n);
		queue<int> q;
		int x = -1;
		q.push(a);
		int l = 1, u = a;
		while(!q.empty() && x == -1)
		{
			int p = q.front();
			for (int i = 0; i < n; i++)
			{
				if (v[p][i] == 1)
				{
					for (int b : vm[erk-1])
					{
						if (i == b)
						{
							h[i] = p;
							x = b;
							break;
						}
					}
				}
				if (v[p][i] == 1 && c[v[p][i]] == 0)
				{
					c[v[p][i]] = 1;
					q.push(i);
					h[i] = p;
				}
			}
			if (p == u)
			{
				l++;
				u = q.back();
			}
			q.pop();
		}
		if (x >= 0 && l < out.size())
		{
			out.resize(0);
			int i = x;
			while(i != a)
			{
				out.push_back(i);
				i = h[i];
			}
			out.push_back(i);
			reverse(out.begin(), out.end());
		}
	}
	if (out.size() == 210) cout << -1;
	else
	{		
		cout << out.size() << endl;
		for (int i : out) cout << i+1 << " ";
	}
}
SubtaskSumTestVerdictTimeMemory
base8/40
1Accepted0/03ms2072 KiB
2Wrong answer0/07ms2988 KiB
3Wrong answer0/23ms2312 KiB
4Accepted2/23ms2428 KiB
5Wrong answer0/23ms2744 KiB
6Accepted2/23ms2772 KiB
7Wrong answer0/24ms3220 KiB
8Accepted2/24ms3344 KiB
9Accepted2/26ms3496 KiB
10Wrong answer0/24ms3720 KiB
11Wrong answer0/24ms3164 KiB
12Wrong answer0/28ms4156 KiB
13Wrong answer0/28ms4112 KiB
14Wrong answer0/27ms4108 KiB
15Time limit exceeded0/2544ms8564 KiB
16Time limit exceeded0/2558ms8564 KiB
17Time limit exceeded0/2550ms8504 KiB
18Time limit exceeded0/2569ms8568 KiB
19Wrong answer0/26ms3960 KiB
20Wrong answer0/27ms4196 KiB
21Wrong answer0/24ms3636 KiB
22Wrong answer0/27ms4108 KiB