• Source
    1. #include <iostream>
    2. #include <vector>
    3. using namespace std;
    4.  
    5. int N, M, u, v;
    6. vector <int> road[102];
    7. void read ()
    8. {
    9. cin>>N>>M>>u>>v;
    10. for (int i=1; i<=M; i++)
    11. {
    12. int A, B;
    13. cin>>A>>B;
    14. road[A].push_back(B);
    15. }
    16. }
    17.  
    18. int check[102], mark[102];
    19. void init ()
    20. {
    21. for (int i=1; i<=N; i++)
    22. {
    23. check[i]=0;
    24. mark[i]=1;
    25. }
    26. }
    27.  
    28. void find_similar ()
    29. {
    30. for (int i=1; i<=N; i++)
    31. {
    32. if (check[i]==1 && mark[i]==1)
    33. {
    34. mark[i]=1;
    35. }
    36. else mark[i]=0;
    37. }
    38. }
    39.  
    40. void DQ_QL (int r, int End)
    41. {
    42. if (r==End)
    43. {
    44. find_similar ();
    45. }
    46. else
    47. {
    48. for (int i=0; i<road[r].size(); i++)
    49. {
    50. int v=road[r][i];
    51. if (check[v]==0)
    52. {
    53. check[v]=1;
    54. DQ_QL (v, End);
    55. check[v]=0;
    56. }
    57. }
    58. }
    59. }
    60.  
    61. int main ()
    62. {
    63. int t;
    64. cin>>t;
    65. while (1)
    66. {
    67. if (t==0) break;
    68. t--;
    69. read ();
    70. init ();
    71. check[u]=1;
    72. DQ_QL (u, v);
    73. check[u]=0; // ^^;
    74. int count = 0;
    75. for (int i=1; i<=N; i++)
    76. {
    77. if (i!=u && i!=v && mark[i]==1) count++;
    78. }
    79. cout<<count<<endl;
    80. for (int i=1; i<=100; i++) road[i].clear();
    81. }
    82. return 0;
    83. }