91772024-02-17 14:30:31xxxA lehető legkevesebb metróval utazás (40 pont)cpp17Runtime error 38/40483ms64208 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
	int n, m, ind, erk;
	cin >> n >> m >> ind >> erk;
	vector<vector<int> > megall(m+1);
	vector<vector<bool> > el(n, vector<bool>(n, false));
	for(int i = 0; i < n; i++) {
		int db;
		cin >> db;
		for(int j = 0; j < db; j++) {
			int x;
			cin >> x;
			megall[x].push_back(i);
		}
	}

	

	for(int i = 1; i <= m; i++) {
		if (megall[i].size() > 1) {
			for(int j = 0; j < megall[i].size(); j++) {
				for(int k = j+1; k < megall[i].size(); k++) {
					el[megall[i][j]][megall[i][k]] = 1;
					el[megall[i][k]][megall[i][j]] = 1;
				}
			}
		}
	}

	queue<int> q;
	vector<int> tav(n+1, -1), parent(n+1, -1);

	for(int i : megall[ind]) {
		q.push(i);
		tav[i] = 1;
	}

	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(int i = 0; i < n; i++) {
			if (el[u][i]) {
				if (tav[i] == -1) {
					tav[i] = tav[u]+1;
					parent[i] = u;
					q.push(i);
				}
			}
		}
	}

	int ans = INT_MAX, mini = 0;
	for(int i : megall[erk]) {
		if (ans > tav[i]) {
			ans = tav[i];
			mini = i;
		}
	}
	cout << ans << '\n';

	vector<int> ansv;
	
	while(ans--) {
		ansv.push_back(mini);
		mini = parent[mini];
	}
	reverse(ansv.begin(), ansv.end());
	for(int x : ansv) {
		cout << x+1 << ' ';
	}

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base38/40
1Accepted0/03ms1828 KiB
2Accepted0/04ms3040 KiB
3Accepted2/23ms2268 KiB
4Accepted2/23ms2448 KiB
5Accepted2/23ms2592 KiB
6Runtime error0/259ms64208 KiB
7Accepted2/23ms3264 KiB
8Accepted2/23ms3276 KiB
9Accepted2/24ms3800 KiB
10Accepted2/24ms3872 KiB
11Accepted2/23ms3556 KiB
12Accepted2/26ms4388 KiB
13Accepted2/26ms4544 KiB
14Accepted2/24ms4556 KiB
15Accepted2/2474ms15896 KiB
16Accepted2/2483ms15756 KiB
17Accepted2/2479ms15832 KiB
18Accepted2/2481ms16320 KiB
19Accepted2/24ms4852 KiB
20Accepted2/24ms5200 KiB
21Accepted2/24ms4876 KiB
22Accepted2/24ms5488 KiB