Pagini recente » Cod sursa (job #348211) | Borderou de evaluare (job #1877765) | Cod sursa (job #3230107) | Cod sursa (job #3127172) | Cod sursa (job #3203276)
// #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;
}