89832024-02-10 11:46:141478Kerékpártúra (50 pont)cpp17Wrong answer 0/50157ms10204 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
base0/50
1Wrong answer0/03ms2580 KiB
2Wrong answer0/027ms4012 KiB
3Wrong answer0/23ms3040 KiB
4Wrong answer0/23ms3252 KiB
5Wrong answer0/23ms3152 KiB
6Wrong answer0/23ms3152 KiB
7Wrong answer0/23ms3420 KiB
8Wrong answer0/24ms3640 KiB
9Wrong answer0/24ms3720 KiB
10Wrong answer0/26ms3892 KiB
11Wrong answer0/28ms3984 KiB
12Wrong answer0/216ms4712 KiB
13Wrong answer0/214ms4604 KiB
14Wrong answer0/228ms5392 KiB
15Wrong answer0/343ms6096 KiB
16Wrong answer0/446ms6408 KiB
17Wrong answer0/464ms7048 KiB
18Wrong answer0/357ms6884 KiB
19Wrong answer0/350ms6612 KiB
20Wrong answer0/3140ms9204 KiB
21Wrong answer0/3157ms9708 KiB
22Wrong answer0/3157ms10204 KiB