fork download
  1. /*
  2. * author : 3hmed_124M
  3. */
  4. #include <bits/stdc++.h>
  5. #include <string>
  6. using namespace std;
  7. #define ll long long
  8. #define int long long
  9. #define el '\n'
  10. #define all(v) v.begin(), v.end()
  11. const int mod = 1e9 + 7, OO = 0x3f3f3f3f;
  12. const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
  13. const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
  14. const char di[] = {'U', 'R', 'L', 'D'};
  15.  
  16. void solve() {
  17. int n, m;
  18. cin >> n >> m;
  19. vector<vector<int> > graph(n + 1);
  20. for (int i = 0; i < m; ++i) {
  21. int x, y;
  22. cin >> x >> y;
  23. graph[y].push_back(x);
  24. }
  25. vector<bool> visited(n + 1, false);
  26. int q;
  27. cin >> q;
  28. while (q--) {
  29. int x, y;
  30. cin >> x >> y;
  31. if (x == 1) {
  32. if (visited[y]) continue;
  33. queue<int> q;
  34. visited[y] = true;
  35. q.push(y);
  36. while (!q.empty()) {
  37. int u = q.front();
  38. q.pop();
  39. for (auto v: graph[u]) {
  40. if (!visited[v]) {
  41. visited[v] = true;
  42. q.push(v);
  43. }
  44. }
  45. }
  46. } else if (x == 2) {
  47. if (visited[y]) {
  48. cout << "YES" << el;
  49. } else {
  50. cout << "NO" << el;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. int32_t main() {
  57. ios_base::sync_with_stdio(false);
  58. cout.tie(nullptr), cin.tie(nullptr);
  59.  
  60. // if(fopen("in.txt", "r")){
  61. // freopen("in.txt", "r", stdin);
  62. // freopen("out.txt", "w", stdout);
  63. // }
  64.  
  65. int tt = 1;
  66. //cin >> tt;
  67. while (tt--) {
  68. solve();
  69. }
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty