fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
  4. #define FORD(i,b,a) for (int i=(b),_a=(a);i>=_a;i=i-1)
  5. #define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
  6. #define FORE(i,v) for (__typeof((v).begin()) i=(v).begin();i!=(v).end();i++)
  7. #define ALL(v) (v).begin(),(v).end()
  8. #define pb push_back
  9. #define mp make_pair
  10. #define fi first
  11. #define se second
  12. #define SZ(x) ((int)(x).size())
  13. #define double db
  14. typedef long long ll;
  15. typedef pair<int,int> PII;
  16. const ll mod=1000000007;
  17. ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
  18. const int MAXN = 1E4+3;
  19. const int oo = 1e9+3;
  20.  
  21. int n, m, s, u, v, res, bac[MAXN],adj[MAXN][MAXN];
  22. bool ok[MAXN];
  23. queue<int> q;
  24. vector<int> a[MAXN];
  25.  
  26. int main() {
  27. #ifndef ONLINE_JUDGE
  28. freopen("test.inp", "r", stdin);
  29. freopen("test.out", "w", stdout);
  30. #endif
  31. cin >> n >> m >> s;
  32. FOR(i,1,m) {
  33. cin >> u >> v;
  34. if (!adj[u][v])
  35. a[u].push_back(v);
  36. adj[u][v] = 1;
  37. }
  38. q.push(s); bac[s] = 1;
  39. while (!q.empty()) {
  40. u = q.front();
  41. q.pop();
  42. for(int i=0; i<a[u].size(); i++)
  43. {
  44. v = a[u][i];
  45. if (!bac[v])
  46. {
  47. if (ok[u]) ok[v] = 1;
  48. bac[v] = bac[u] + 1;
  49. q.push(v);
  50. }
  51. else
  52. if (bac[v] == bac[u] + 1) ok[v] = 1;
  53. }
  54. }
  55. FOR(i,1,n) if (ok[i]) res++;
  56. cout << res;
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 407232KB
stdin
Standard input is empty
stdout
Standard output is empty