88872024-02-02 20:58:33bovizdbA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 28/40601ms9368 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[erk-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[ind-1])
					{
						if (i == b)
						{
							h[i] = p;
							x = b;
							break;
						}
					}
				}
				if (v[p][i] == 1 && c[i] == 0)
				{
					c[i] = 1;
					q.push(i);
					h[i] = p;
				}
			}
			if (p == u)
			{
				l++;
				u = q.back();
			}
			q.pop();
		}
		if (x >= 0 && l > 1 && 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
base28/40
1Accepted0/03ms1812 KiB
2Accepted0/08ms3044 KiB
3Wrong answer0/23ms2360 KiB
4Accepted2/23ms2512 KiB
5Wrong answer0/23ms2908 KiB
6Accepted2/23ms2936 KiB
7Accepted2/24ms3244 KiB
8Accepted2/24ms3280 KiB
9Accepted2/26ms3688 KiB
10Accepted2/24ms3856 KiB
11Accepted2/24ms3512 KiB
12Accepted2/28ms4252 KiB
13Accepted2/28ms4512 KiB
14Accepted2/28ms4472 KiB
15Time limit exceeded0/2601ms9160 KiB
16Time limit exceeded0/2550ms9152 KiB
17Time limit exceeded0/2566ms9332 KiB
18Time limit exceeded0/2569ms9368 KiB
19Accepted2/26ms4920 KiB
20Accepted2/27ms5052 KiB
21Accepted2/24ms4964 KiB
22Accepted2/28ms5392 KiB