-
Notifications
You must be signed in to change notification settings - Fork 0
/
Birdwatching.cc
71 lines (64 loc) · 1.4 KB
/
Birdwatching.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define ALL(s) s.begin(),s.end()
#define fill(a,c) memset(&a, c, sizeof(a))
#define fore(i,x,y) for(ll i=x;i<y;i++)
#define SZ(x) ((int)(x).size())
#define PI acos(-1)
#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
typedef long long ll;
using namespace std;
int n,m,t;
const int MAXN = 1e5+10;
vector<int> g[MAXN];
int reach[MAXN], vis[MAXN];
bool dfs(int u, int star){
if(u == t) return true;
if(vis[u]){
return 1;
}
if(u != star && reach[u]){
return 0;
}
vis[u] = 1;
int can = 1;
for(auto v: g[u]){
if(v!= t && v != star){
can = can && dfs(v,star);
}
}
if(!can) reach[u]=1;
return can;
}
int main() {FIN;
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#define endl '\n'
#endif
cin>>n>>m>>t;
vector<int> cand;
memset(reach,0,sizeof(reach));
memset(vis,0,sizeof(vis));
fore(i,0,m){
int u,v; cin>>u>>v;
g[u].pb(v);
if(v == t){
cand.pb(u);
reach[u]=1;
}
}
vector<int> ans;
fore(i,0,SZ(cand)){
memset(vis,0,sizeof(vis));
if(dfs(cand[i], cand[i]))
ans.pb(cand[i]);
}
cout<<SZ(ans)<<"\n";
sort(ALL(ans));
fore(i,0,SZ(ans)) cout<<ans[i]<<"\n";
return 0;
}