Cod sursa(job #3203276)

Utilizator laurentiu.maticaMatica Laurentiu-Andrei laurentiu.matica Data 13 februarie 2024 13:50:03
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
// #include <iostream>

#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

ifstream cin("graf.in");
ofstream cout("graf.out");

const int NMAX = 7501;
vector<int>g[NMAX];
queue<int>q;
vector<int>sol;
int dist1[NMAX];
int dist2[NMAX];
int f[NMAX][NMAX];
int n, m, x, y;

void bfs(int st, int dist[NMAX])
{
	dist[st] = 0;
	q.push(st);
	while (!q.empty())
	{
		int p = q.front();
		q.pop();
		for (auto x : g[p])
		{
			if (dist[x] == -1)
			{
				dist[x] = dist[p] + 1;
				q.push(x);
			}
		}
	}
}
int main()
{
	cin >> n >> m >> x >> y;
	q.push(x);
	for (int i = 1; i <= n; i++)
		dist1[i] = dist2[i] = - 1;
	for (int i = 0; i < m; i++)
	{
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	bfs(x, dist1);
	bfs(y, dist2);
	for (int i = 1; i <= n; i++)
		if (dist1[i] + dist2[i] == dist1[y])
			f[dist1[i]][dist2[i]]++;
	for (int i = 1; i <= n; i++)
		if (dist1[i] + dist2[i] == dist1[y] && f[dist1[i]][dist2[i]]==1)
			sol.push_back(i);
	cout << sol.size() << '\n';
	for (int i = 0; i < sol.size(); i++)
		cout << sol[i] << ' ';
	return 0;
}