-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_Environment_Friendly_Travel.cpp
76 lines (67 loc) · 2.21 KB
/
A_Environment_Friendly_Travel.cpp
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
72
73
74
75
76
#include "bits/stdc++.h"
using namespace std;
void __dump(int x){cerr << x;}
void __dump(long long x){cerr << x;}
void __dump(long double x){cerr << fixed << setprecision(3) << x;}
void __dump(char x){cerr << '\'' << x << '\'';}
void __dump(const string &x){cerr << '"' << x << '"';}
void __dump(const char *x){cerr << '"' << x << '"';}
void __dump(bool x){cerr << (x ? "true" : "false");}
void _dump(){cerr << "\n";}
template <typename T, typename U> void __dump(const pair<T, U> &x){cerr << '{'; __dump(x.first); cerr << ','; __dump(x.second); cerr << '}';}
template <typename T, typename U, typename V> void __dump(const tuple<T, U, V> &x){cerr << '{'; __dump(get<0>(x)); cerr << ','; __dump(get<1>(x)); cerr << ','; __dump(get<2>(x)); cerr << '}';}
template <typename T> void __dump(const T& x){int f=0; cerr << '{'; for(auto&i:x) cerr << (f++ ? "," : ""), __dump(i); cerr << "}";}
template <typename T, typename ... U> void _dump(T t, U ... u){__dump(t); if(sizeof...(u)) cerr << ", "; _dump(u...);}
#ifdef ilyes
#define dump(x ...) cerr << "|dumping| " << # x << " = ", _dump(x)
#else
#define dump(...) 1998
#endif
//#define int long long
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define size(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define ln '\n'
#define __ ' '
#define LL long long
#define LD long double
#define pii pair<int,int>
const int INF = 1e9;
const int N = 1e5;
int n, m, node;
set<int> adj[N+2], revadj[N+2];
set<int> good, bad;
int main(){ FAST
cin >> n >> m >> node; ++node;
for(int i=1; i<=m; ++i){
int u, v; cin >> u >> v; ++u, ++v;
if(u == node) continue;
adj[u].insert(v);
revadj[v].insert(u);
}
for(int u=1; u<=n; ++u){
if(adj[u].count(node)) good.insert(u);
}
set<pii> ss;
for(int u=1; u<=n; ++u){
ss.insert({size(adj[u]), u});
}
while(!empty(ss)){
int deg, u;
tie(deg, u) = *begin(ss);
ss.erase(begin(ss));
for(int v: revadj[u]){
ss.erase({size(adj[v]), v});
adj[v].erase(u);
ss.insert({size(adj[v]), v});
if(good.count(u)) bad.insert(v);
}
revadj[u].clear();
}
set<int> result;
for(int u=1; u<=n; ++u){
if(good.count(u) && !bad.count(u)) result.insert(u);
}
cout << size(result) << ln;
for(int u: result) cout << u-1 << ln;
}