88832024-02-02 20:21:06bovizdbA lehető legkevesebb metróval utazás (40 pont)cpp17Wrong answer 4/40583ms9488 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> mn;
		queue<int> q;
		q.push(a);
		bool br = 1;
		int l = 1, u = a;
		while(!q.empty() && br)
		{
			int p = q.front();
			mn.push_back(p);
			for (int i = 0; i < n; i++)
			{
				if (v[p][i] == 1)
				{
					for (int b : vm[erk-1])
					{
						if (i == b)
						{
							mn.push_back(i);
							br = 0;
							break;
						}
					}
				}
				if (v[p][i] == 1 && c[v[p][i]] == 0)
				{
					c[v[p][i]] = 1;
					q.push(i);
				}
			}
			if (p == u)
			{
				l++;
				u = q.back();
			}
			q.pop();
		}
		if (mn.size() < out.size() && !br) out = mn;
	}
	if (out.size() == 210) cout << -1;
	else
	{		
		cout << out.size() << endl;
		for (int i : out) cout << i+1 << " ";
	}
}
SubtaskSumTestVerdictTimeMemory
base4/40
1Accepted0/03ms1812 KiB
2Wrong answer0/07ms3036 KiB
3Wrong answer0/23ms2368 KiB
4Accepted2/23ms2440 KiB
5Wrong answer0/23ms2864 KiB
6Accepted2/22ms2740 KiB
7Wrong answer0/24ms3000 KiB
8Wrong answer0/24ms3032 KiB
9Wrong answer0/24ms3184 KiB
10Wrong answer0/24ms3272 KiB
11Wrong answer0/24ms3112 KiB
12Wrong answer0/28ms3808 KiB
13Wrong answer0/28ms3804 KiB
14Wrong answer0/27ms4060 KiB
15Time limit exceeded0/2555ms8708 KiB
16Time limit exceeded0/2583ms9008 KiB
17Time limit exceeded0/2555ms9224 KiB
18Time limit exceeded0/2574ms9488 KiB
19Wrong answer0/26ms4984 KiB
20Wrong answer0/27ms5204 KiB
21Wrong answer0/24ms4832 KiB
22Wrong answer0/27ms5288 KiB