Cod sursa(job #2760051)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 22 iunie 2021 18:21:48
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>

using namespace std;

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

struct nod
{
    int dst = 0,dfin = 0;
};

int n,m,x,y,nr[7505];
vector<int>a[7505];
nod v[7505];

void citire()
{
    in >> n >> m >> x >> y;
    for (int i = 1; i <= m; i++)
    {
        int ai,bi;
        in >> ai >> bi;
        a[ai].push_back(bi);
        a[bi].push_back(ai);
    }
}

queue<int>q;

void BFS1()
{
    q.push(x);
    while (!q.empty())
    {
        for (int i = 0; i < a[q.front()].size(); i++)
        {
            if (v[a[q.front()][i]].dst == 0 and a[q.front()][i] != x)
            {
                v[a[q.front()][i]].dst = v[q.front()].dst + 1;
                q.push(a[q.front()][i]);
            }
        }
        q.pop();
    }
}

void BFS2()
{
    v[y].dfin = 0;
    q.push(y);
    while (!q.empty())
    {
        for (int i = 0; i < a[q.front()].size(); i++)
        {
            if (v[a[q.front()][i]].dfin == 0 and a[q.front()][i] != y)
            {
                v[a[q.front()][i]].dfin = v[q.front()].dfin + 1;
                q.push(a[q.front()][i]);
            }
        }
        q.pop();
    }
}

int main()
{
    citire();
    BFS1();
    BFS2();
    vector<int>sol,aux;
    int dist = v[x].dfin;
    for (int i = 1; i <= n; i++)
        if (v[i].dst + v[i].dfin == dist)
            aux.push_back(i);
    for (int i = 0; i < aux.size(); i++)
        nr[v[aux[i]].dst]++;
    for (int i = 0; i < aux.size(); i++)
        if (nr[v[aux[i]].dst] == 1)
            sol.push_back(aux[i]);
    out << sol.size() << '\n';
    for (int i = 0; i < sol.size(); i++)
        out << sol[i] << " ";
    return 0;
}