89842024-02-10 11:47:361478Kerékpártúra (50 pont)cpp17Wrong answer 20/50131ms10436 KiB
#include <bits/stdc++.h>  
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())

const int N_MAX = 1e4 + 1;
int n, m, k;

vector<vector<int>> out(N_MAX);
vector<vector<int>> in(N_MAX);

vector<bool> fromK(N_MAX);
vector<bool> toK(N_MAX);

void bfs1(){
    queue<int> q;
    vector<bool> seen(n + 1);

    q.push(k);
    seen[k] = 1;

    int x;

    while(!q.empty()){
        x = q.front();
        q.pop();

        for(int i = 0; i < out[x].size(); i++){
            if(!seen[out[x][i]]){
                seen[out[x][i]] = 1;
                q.push(out[x][i]);
                fromK[out[x][i]] = 1;
            }
        }

    }
}

void bfs2(){
    queue<int> q;
    vector<bool> seen(n + 1);

    q.push(k);
    seen[k] = 1;

    int x;

    while(!q.empty()){
        x = q.front();
        q.pop();

        for(int i = 0; i < in[x].size(); i++){
            if(!seen[in[x][i]]){
                seen[in[x][i]] = 1;
                q.push(in[x][i]);
                toK[in[x][i]] = 1;
            }
        }

    }

}
 
int main()
{
    //ifstream cin("in.txt");

    cin >> n >> m >> k;

    for(int i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        out[a].push_back(b);
        in[b].push_back(a);    
    }

    /*for(int i = 1; i <= n; i++){
        cout << i <<": ";
        for(int j = 0; j < in[i].size(); j++){
            cout << in[i][j] << " ";
        }
        cout << '\n';
    }*/

    bfs1();
    bfs2();

    //cout << "X";



    /*for(int i = 1; i <= n; i++){
        cout << fromK[i] << " " << toK[i] << '\n';
    }*/

    vector<int> ans;
    vector<bool> alreadyIn(n + 1);

    alreadyIn[k] = 1;

    for(int i = 1; i <= n; i++){
        if(fromK[i] && toK[i]){

            if(!alreadyIn[i]){
                ans.push_back(i);
                alreadyIn[i] = 1;
            } 
            
            for(int j = 0; j < out[i].size(); j++){
                if(!alreadyIn[out[i][j]]){
                    ans.push_back(out[i][j]);
                    alreadyIn[out[i][j]] = 1;
                }
            }

        }
    }
    
    cout << ans.size() << '\n';
    for(int i = 0; i < ans.size(); i++){
        cout << ans[i] << " ";
    }
}
SubtaskSumTestVerdictTimeMemory
base20/50
1Accepted0/03ms2584 KiB
2Wrong answer0/021ms4088 KiB
3Accepted2/23ms2808 KiB
4Accepted2/23ms3072 KiB
5Accepted2/23ms3196 KiB
6Wrong answer0/23ms3284 KiB
7Wrong answer0/23ms3420 KiB
8Wrong answer0/24ms3700 KiB
9Accepted2/24ms3992 KiB
10Accepted2/26ms4224 KiB
11Wrong answer0/27ms4220 KiB
12Wrong answer0/214ms4720 KiB
13Wrong answer0/213ms4720 KiB
14Wrong answer0/223ms5244 KiB
15Wrong answer0/335ms6028 KiB
16Wrong answer0/437ms6480 KiB
17Accepted4/454ms7132 KiB
18Accepted3/348ms7096 KiB
19Accepted3/341ms6924 KiB
20Wrong answer0/3115ms9652 KiB
21Wrong answer0/3131ms10128 KiB
22Wrong answer0/3131ms10436 KiB