126152024-12-26 21:16:46BucsMateKerékpártúra (50 pont)cpp17Elfogadva 50/50126ms3652 KiB
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void BFS_rev(bool canReturn[], vector<vector<int>> &adj_rev, int start)
{
    queue<int> q;
    q.push(start);
    canReturn[start] = true;

    while(!q.empty()){
        int curr = q.front();
        q.pop();
        for(int i = 0; i < adj_rev[curr].size(); i++){
            int newNode = adj_rev[curr][i];
            if(!canReturn[newNode]){
                q.push(newNode);
                canReturn[newNode] = true;
            }
        }
    }
}

void BFS_main(bool canReturn[], bool valid[], vector<vector<int>> &adj, int start)
{
    queue<int> q;
    q.push(start);
    valid[start] = true;

    while(!q.empty()){
        int curr = q.front();
        q.pop();
        if(!canReturn[curr]){
            continue;
        }
        for(int i = 0; i < adj[curr].size(); i++){
            int newNode = adj[curr][i];
            if(!valid[newNode]){
                valid[newNode] = true;
                q.push(newNode);
            }
        }
    }
}

int main()
{
    int N, M, K;
    cin >> N >> M >> K;
    vector<vector<int>> adj(N+1, vector<int>()), adj_rev(N+1, vector<int>());
    for(int i = 0; i < M; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj_rev[b].push_back(a);
    }
    bool canReturn[10001] = {};
    BFS_rev(canReturn, adj_rev, K);

    bool valid[10001] = {};
    BFS_main(canReturn, valid, adj, K);

    vector<int> sol;

    for(int i = 1; i <= N; i++){
        if(valid[i] && i != K){
            sol.push_back(i);
        }
    }

    cout << sol.size() << endl;
    for(int i = 0; i < sol.size(); i++){
        cout << sol[i] << " ";
    }
    cout << endl;
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms320 KiB
2Elfogadva0/018ms1472 KiB
3Elfogadva2/21ms508 KiB
4Elfogadva2/21ms320 KiB
5Elfogadva2/21ms320 KiB
6Elfogadva2/21ms320 KiB
7Elfogadva2/21ms320 KiB
8Elfogadva2/23ms320 KiB
9Elfogadva2/23ms320 KiB
10Elfogadva2/24ms320 KiB
11Elfogadva2/24ms568 KiB
12Elfogadva2/210ms600 KiB
13Elfogadva2/29ms592 KiB
14Elfogadva2/220ms960 KiB
15Elfogadva3/335ms1648 KiB
16Elfogadva4/435ms1904 KiB
17Elfogadva4/450ms2272 KiB
18Elfogadva3/345ms2104 KiB
19Elfogadva3/337ms1852 KiB
20Elfogadva3/3111ms3348 KiB
21Elfogadva3/3125ms3536 KiB
22Elfogadva3/3126ms3652 KiB