fork(1) download
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4. #define gc getchar_unlocked
  5. #define pc putchar_unlocked
  6. inline int scan(){register int n=0,c=gc();while(c<'0'||c>'9')c=gc();while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=gc();return n;}
  7.  
  8. bool findPath(int X, int Y, bool V[],int T[],int N){
  9. if(T[X]==Y) {
  10. //cout<<X<<Y<<"\n";
  11. return true;
  12. }
  13. else if(V[X]==0 && T[X]!=Y && T[X]<N ){
  14. //cout << "temp="<<X<<"-"<<T[X]<<"\n";
  15. V[X]=1;
  16. return findPath(V[X],Y,V,T,N);
  17. }
  18. else return false;
  19. }
  20. int findPathUtil(int X, int Y,int A[], int B[],int N){
  21. bool VA[200]={0};
  22. bool VB[200]={0};
  23. bool ans = false;
  24. //char a='a',b='b';
  25. ans = ans || findPath(X,Y,VA,A,N) || findPath(X,Y,VB,B,N);
  26. return ans;
  27. }
  28.  
  29. int main()
  30. {
  31. int t,N,M,Q,nn,mm,X,Y;
  32. t=scan();
  33. int A[200],B[200];
  34. while(t--){
  35. N=scan();
  36. M=scan();
  37. //int A[N],B[N];
  38. mm=M;
  39. while(M--){
  40. int t1=scan();
  41. int t2=scan();
  42. A[t1]=t2;
  43. B[t2]=t1;
  44. //added all entries in A,B array
  45. }
  46. }
  47. /*for(int i=0;i<mm;i++){
  48.   cout << i << "=" <<A[i]<<"\n";
  49.   }*/
  50.  
  51. Q=scan();
  52. while(Q--){
  53. X=scan();
  54. Y=scan();
  55. if(X<N && Y<N){
  56. if(findPathUtil(X,Y,A,B,N)==true)cout<<"YES\n";
  57. else cout <<"NO\n";
  58. }
  59. else cout << "NO\n";
  60. }
  61. return 0;
  62. }
Success #stdin #stdout 0s 3304KB
stdin
1
4 2
0 1
1 2
3
0 2
0 3
2 1
stdout
YES
NO
YES