Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. #include <algorithm>
  2. #include <array>
  3. #include <bitset>
  4. #include <cassert>
  5. #include <chrono>
  6. #include <cmath>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <functional>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <map>
  13. #include <numeric>
  14. #include <queue>
  15. #include <random>
  16. #include <set>
  17. #include <stack>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include <queue>
  22.  
  23. /* #include <ext/pb_ds/assoc_container.hpp> */
  24. /* #include <ext/pb_ds/tree_policy.hpp> */
  25. using namespace std;
  26. /* using namespace __gnu_pbds; */
  27.  
  28. #define endl '\n'
  29. #define sz(x) (int)x.size()
  30. #define all(x) begin(x), end(x)
  31. #define rall(x) (x).rbegin(), (x).rend()
  32. #define ar array
  33. #define vt vector
  34.  
  35. #define for_base(i, a, b, x) for (int i=((a)<(b))?(a):(a)-1; ((a)<(b))?i<(b):i>=(b); ((a)<(b))?i+=(x):i-=(x))
  36. #define FOR1(a) for_base(i, 0, a, 1)
  37. #define FOR2(i, a) for_base(i, 0, a, 1)
  38. #define FOR3(i, a, b) for_base(i, a, b, 1)
  39. #define FOR4(i, a, b, x) for_base(i, a, b, x)
  40. #define FIFTH(a, b, c, d, e, ...) e
  41. #define FOR(...) FIFTH(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
  42. #define EACH(a, x) for (auto& a: x)
  43. #define FIT(i, x) for (auto i=(x).begin(); i!=(x).end(); ++i)
  44. #define RIT(i, x) for (auto i=(x).rbegin(); i!=(x).rend(); ++i)
  45. #define finish(...) return void(print(__VA_ARGS__))
  46.  
  47. typedef long long ll;
  48.  
  49. template<class T> using min_queue = priority_queue<T, vector<T>, greater<T> >;
  50.  
  51. /* find_by_order(x) => returns an iterator to the element at a given position */
  52. /* order_of_key(x) => returns the position of a given element */
  53. /* template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; */
  54.  
  55. template<class T> bool umin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
  56. template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
  57.  
  58. template<class T> void read(T& x) { cin >> x; }
  59. template<class H, class T> void read(pair<H, T>& p) { cin >> p.first >> p.second; }
  60. template<class A, size_t S> void read(array<A, S>& x) { EACH(a, x) read(a); }
  61. template<class T> void read(vector<T>& v) { EACH(i, v) read(i); }
  62. template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); }
  63.  
  64. template<class H, class T> ostream &operator<<(ostream &o, pair<H, T> &p) { o << p.first << " " << p.second; return o; }
  65. template<class H, class T> ostream &operator<<(ostream &o, vector<pair<H, T> > &v) { string s; EACH(i, v) o << s << i, s = "\n"; return o; }
  66. template<class T, size_t S> ostream &operator<<(ostream &o, array<T, S> &a) { string s; EACH(i, a) o << s << i, s = " "; return o; }
  67. template<class T, size_t S> ostream &operator<<(ostream &o, vector<array<T, S> > &v) { string s; EACH(i, v) o << s << i, s = "\n"; return o; }
  68. template<class T> ostream &operator<<(ostream &o, vector<T> &v) { string s; EACH(i, v) o << s << i, s = " "; return o; }
  69. template<class T> ostream &operator<<(ostream &o, vector<vector<T> > &v) { string s; EACH(i, v) o << s << i, s = "\n"; return o; }
  70. template<class T> void write(T x) { cout << x; }
  71. template<class H, class... T> void write(const H &h, const T &...t) { write(h); write(t...); }
  72. void print() { write('\n'); }
  73. template<class H, class... T> void print(const H &h, const T &...t) { write(h); if (sizeof...(t)) write(' '); print(t...); }
  74.  
  75. void DBG() { cerr << "]" << endl; }
  76. template<class H, class... T> void DBG(H h, T... t) { cerr << h; if(sizeof...(t)) cerr << ", "; DBG(t...); }
  77. #ifdef local
  78. #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
  79. #else
  80. #define dbg(...) 0
  81. #endif
  82.  
  83. const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
  84. const int mod = 1e9 + 7;
  85.  
  86.  
  87. void dfs(int node, int n, vt<vt<int> >& adj, vt<vt<int> >& dist, vt<bool>& vis) {
  88. vis[node] = 1;
  89. if(node == n - 1) {
  90. dist[node].push_back(0);
  91. }
  92. for(int next : adj[node]) {
  93. if(!vis[next]) {
  94. dfs(next, n, adj, dist, vis);
  95. }
  96. for(int d : dist[next]) {
  97. dist[node].push_back(d + 1);
  98. }
  99. }
  100. }
  101.  
  102. void find_paths(int n, vt<vt<int> >& graph, vt<vt<int> >& dist, vt<bool>& vis) {
  103. for(int i = 0; i < n; ++i) {
  104. if(!vis[i]) {
  105. dfs(i, n, graph, dist, vis);
  106. }
  107. }
  108. }
  109.  
  110. void solve() {
  111. int n, m;
  112. read(n, m);
  113. int e1, e2;
  114. read(e1, e2);
  115. vt<vt<int> > adj1(n);
  116. vt<vt<int> > adj2(m);
  117. while(e1--) {
  118. int a, b;
  119. read(a, b);
  120. --a;
  121. --b;
  122. adj1[a].push_back(b);
  123. }
  124. while(e2--) {
  125. int a, b;
  126. read(a, b);
  127. --a;
  128. --b;
  129. adj2[a].push_back(b);
  130. }
  131. vt<bool > vis1(n);
  132. vt<bool > vis2(n);
  133. vt<vt<int> > d1(n);
  134. vt<vt<int> > d2(m);
  135. find_paths(n, adj1, d1, vis1);
  136. find_paths(m, adj2, d2, vis2);
  137.  
  138. bool exists[2001];
  139. memset(exists, 0, sizeof(exists));
  140. for(int x : d1[0]) {
  141. for(int y : d2[0]) {
  142. if(x + y <= 2000)
  143. exists[x + y] = 1;
  144. }
  145. }
  146. int q;
  147. read(q);
  148. while(q--) {
  149. int s;
  150. read(s);
  151. print(exists[s] ? "Yes" : "No");
  152. }
  153. }
  154.  
  155. int main() {
  156. ios_base::sync_with_stdio(0);
  157. cin.tie(0);
  158.  
  159. int t = 1;
  160. /* read(t); */
  161. for(int tc = 1; tc <= t; ++tc) {
  162. /* write("Case #", tc, ": "); */
  163. solve();
  164. }
  165. return 0;
  166. }
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement