198912025-12-29 00:07:13szabelrKerékpártúra (50 pont)cpp17Elfogadva 50/5054ms3696 KiB
// Kerékpártúra_clean'.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m, k,x,y;
    cin >> n >> m >> k;
    vector<vector<int>> adj(n+1);
    vector<vector<int>> adj_rev(n+1);
    for (int i = 1; i <= m; i++)
    {
        cin >> x >> y;
        adj[x].push_back(y);
        adj_rev[y].push_back(x);
    }
    queue<int> q;
    vector<bool> visitable(n + 1, false);
    q.push(k);
    visitable[k] = true;
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (int i = 0; i < adj_rev[v].size(); i++)
        {
            if (!visitable[adj_rev[v][i]])
            {
                visitable[adj_rev[v][i]] = true;
                q.push(adj_rev[v][i]);
            }
        }
    }
    queue<int> q1;
    q1.push(k);
    vector<bool> visited(n + 1, false);
    vector<int> res;
    while (!q1.empty())
    {
        int v = q1.front();
        q1.pop();
        for (int i = 0; i < adj[v].size(); i++)
        {
            if (!visited[adj[v][i]] and visitable[adj[v][i]])
            {
                res.push_back(adj[v][i]);
                visited[adj[v][i]] = true;
                q1.push(adj[v][i]);
            }
            else if (!visited[adj[v][i]])
            {
                res.push_back(adj[v][i]);
                visited[adj[v][i]] = true;
            }
        }
    }
    if(visited[k])
        cout << res.size()-1 << endl;
    else
        cout << res.size() << endl;
    for (auto x : res)
    {
        if (x != k)
            cout << x << " ";
    }
        
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/09ms1332 KiB
3Elfogadva2/21ms332 KiB
4Elfogadva2/21ms316 KiB
5Elfogadva2/21ms316 KiB
6Elfogadva2/21ms508 KiB
7Elfogadva2/21ms316 KiB
8Elfogadva2/23ms316 KiB
9Elfogadva2/22ms500 KiB
10Elfogadva2/22ms316 KiB
11Elfogadva2/23ms480 KiB
12Elfogadva2/24ms612 KiB
13Elfogadva2/24ms568 KiB
14Elfogadva2/28ms952 KiB
15Elfogadva3/316ms1712 KiB
16Elfogadva4/417ms1672 KiB
17Elfogadva4/425ms2260 KiB
18Elfogadva3/320ms2036 KiB
19Elfogadva3/318ms1868 KiB
20Elfogadva3/346ms3124 KiB
21Elfogadva3/352ms3436 KiB
22Elfogadva3/354ms3696 KiB