88882024-02-02 21:03:52bovizdbA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 28/40600ms9156 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);
	vector<bool> verk(n);
	for (int i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		for (int j = 0; j < a; j++)
		{
			int in;
			cin >> in;
			if (in == erk) verk[i] = 1;
			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)
				{
					if (verk[i] == 1)
					{
						h[i] = p;
						x = i;
						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/03ms1876 KiB
2Accepted0/08ms3100 KiB
3Wrong answer0/23ms2360 KiB
4Accepted2/23ms2492 KiB
5Wrong answer0/23ms2632 KiB
6Accepted2/23ms2708 KiB
7Accepted2/24ms3168 KiB
8Accepted2/24ms3324 KiB
9Accepted2/26ms3484 KiB
10Accepted2/24ms3572 KiB
11Accepted2/24ms3404 KiB
12Accepted2/28ms4312 KiB
13Accepted2/28ms4276 KiB
14Accepted2/27ms4280 KiB
15Time limit exceeded0/2600ms8600 KiB
16Time limit exceeded0/2574ms8952 KiB
17Time limit exceeded0/2561ms9156 KiB
18Time limit exceeded0/2558ms9128 KiB
19Accepted2/26ms4640 KiB
20Accepted2/27ms4540 KiB
21Accepted2/24ms4372 KiB
22Accepted2/28ms4812 KiB