Kiara studies an odd species of birds that travel in a very peculiar way. Their movements are best explained using the language of graphs: there exists a directed graph where the nodes are trees, and a bird can only fly from a tree to when is an edge of .
Kiara does not know the real graph governing the flight of these birds but, in her previous field study, Kiara has collected data from the journey of many birds. Using this, she has devised a graph explaining how they move. Kiara has spent so much time watching them that she is confident that if a bird can fly directly from to , then she has witnessed at least one such occurrence. However, it is possible that a bird flew from to to but she only witnessed the stops and and then added to . It is also possible that a bird flew from to to to and she only witnessed and , and added to , etc. To sum up, she knows that contains all the edges of and that might contain some other edges for which there is a path from to in (note that might not contain all such edges).
For her next field study, Kiara has decided to install her base next to a given tree . To be warned of the arrival of birds on , she would also like to install detectors on the trees where the birds can come from (i.e. the trees such that there is an edge in ). As detectors are not cheap, she only wants to install detectors on the trees for which she is sure that belongs to .
Kiara is sure that an edge belongs to when is an edge of and all the paths in starting from and ending in use the edge . Kiara asks you to compute the set of trees for which she is sure that is an edge of .
Input
The input describes the graph . The first line contains three space-separated integers , , and : is the number of nodes of , is the number of edges of and is the node corresponding to the tree on which Kiara will install her base.
The next lines describe the edges of the graph . Each contains two space-separated integers and stating that . It is guaranteed that the same pair will not appear twice.
Limits
;
.
Output
Your output should describe the set . The first line should contain an integer , which is the number of nodes in , followed by lines, each containing a different element of . The elements of should be printed in increasing order, with one element per line.
Examples
input
3 3 2
0 1
0 2
1 2
output
1
1
input
6 8 2
0 1
0 2
1 2
2 0
2 3
3 4
4 2
2 5
output
2
1
4
Note
Sample Explanation 1
The graph corresponding to this example is depicted below. The node belongs to because the (only) path from to uses . The node does not belong to because the path does not use the edge .
Sample Explanation 2
The graph corresponding to this example is depicted below. For the same reason as in Sample , the node does not belong to while 1 does. The nodes and do not belong to because we do not have edges or . Finally belongs to because all paths from to use the edge .
中存儲的元素爲且能夠到達點的點。
表示與對於存在邊點是否存在其他路徑可以到達點。
表示點是否爲存在邊點。
將所有有向邊反向建圖,從所有的點出發,如果某個某個點滿足並且中有不是的點,則,最終答案爲所有爲的點。
然而如果直接會超時,這裏需要進行一些優化。
對與要從訪問到的點,如果中有,說明點已經被從出發訪問過了;如果說明至少有從兩個不同點出發的訪問到該點,即該點所有出入邊都被覆蓋過至少一次這兩種情況都沒必要訪問。
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 1e5 + 10;
int head[N], ver[N << 1], Next[N << 1], tot;
int n, m, st;
vi p;
bool v2[N], v3[N];
unordered_set<int> v1[N];
inline void add(int x, int y) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
inline void read() {
n = qr(), m = qr(), st = qr();
while (m--) {
int x = qr(), y = qr();
add(y, x);
}
}
inline void bfs() {
queue<pii > q;
for (auto it:p)q.push({it, it});
while (!q.empty()) {
pii x = q.front();
q.pop();
reps(x.fi) {
int y = ver[i];
if (v1[y].size() >= 2 || v1[y].count(x.se))continue;
if (v3[y] && x.se != y)v2[y] = true;
v1[y].insert(x.se), q.push({y, x.se});
}
}
}
int main() {
read();
reps(st) {
int y = ver[i];
p.pb(y), v3[y] = true, v1[y].insert(y), v1[st].insert(y);
}
bfs();
vi ans;
for (auto it:p)if (!v2[it])ans.pb(it);
pi(ans.size());
sort(ans.begin(), ans.end());
for (auto it:ans)pi(it);
return 0;
}