82682024-01-13 23:01:59Balki22Kerékpártúra (50 pont)cpp17Accepted 50/50141ms12292 KiB
#include <bits/stdc++.h>

using namespace std;

void dfs2(int curr, vector<vector<int>>& e, vector<bool>& visited, vector<bool>& pathnode, vector<int>& solutions) {
    for (int s : e[curr]) {
        if (!visited[s]) {
            visited[s] = true;
            solutions.push_back(s);
            if (pathnode[s]) dfs2(s, e, visited, pathnode, solutions);
        }
    }
}

int main()
{
    int n, m, k; cin >> n >> m >> k;
    vector<vector<int>> e(n + 1, vector<int>(0));
    vector<vector<int>> visszae(n + 1, vector<int>(0));
    vector<int> solutions(0);
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        e[u].push_back(v);
		visszae[v].push_back(u);
    }
    vector<bool> pathnode(n, false);
    
	queue<int> q;
	q.push(k);
	while(!q.empty()) {
		int curr = q.front(); q.pop();
		for (int s : visszae[curr]) {
			if (!pathnode[s]) {
				q.push(s);
				pathnode[s] = true;
			}
		}
	}

    vector<bool> visited(n, false);
    visited[k] = true;
    dfs2(k, e, visited, pathnode, solutions);

    if (solutions.size() == 0) cout << 0;
    else {
       cout << solutions.size() << '\n';
        for (int s : solutions) {
            cout << s << ' ';
        }
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1816 KiB
2Accepted0/020ms4052 KiB
3Accepted2/23ms2216 KiB
4Accepted2/23ms2424 KiB
5Accepted2/23ms2684 KiB
6Accepted2/23ms2856 KiB
7Accepted2/23ms2948 KiB
8Accepted2/24ms3120 KiB
9Accepted2/24ms3364 KiB
10Accepted2/24ms3596 KiB
11Accepted2/26ms3876 KiB
12Accepted2/213ms4520 KiB
13Accepted2/212ms4536 KiB
14Accepted2/221ms5136 KiB
15Accepted3/335ms7080 KiB
16Accepted4/437ms7028 KiB
17Accepted4/454ms8444 KiB
18Accepted3/346ms8608 KiB
19Accepted3/341ms8776 KiB
20Accepted3/3118ms10748 KiB
21Accepted3/3131ms11680 KiB
22Accepted3/3141ms12292 KiB