Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define fore(i, l, r) for(int i = int(l); i < int(r); i++)
  4. #define forn(i, n) fore(i, 0, n)
  5.  
  6. using namespace std;
  7.  
  8. #define mp make_pair
  9. #define pb push_back
  10. #define sz(a) int((a).size())
  11. #define all(a) (a).begin(), (a).end()
  12.  
  13. #define x first
  14. #define y second
  15.  
  16. typedef long long li;
  17. typedef long double ld;
  18. typedef pair<int, int> pt;
  19.  
  20. mt19937 rnd(time(NULL));
  21.  
  22. template<class A, class B> ostream& operator <<(ostream &out, const pair<A, B> &p) {
  23. return out << "(" << p.x << ", " << p.y << ")";
  24. }
  25.  
  26. template<class A> ostream& operator <<(ostream &out, const vector<A> &v) {
  27. out << "[";
  28. forn(i, sz(v)) {
  29. if(i) out << ", ";
  30. out << v[i];
  31. }
  32. return out << "]";
  33. }
  34.  
  35. const int INF = int(1e9);
  36. const li INF64 = li(1e18);
  37. const ld EPS = 1e-9;
  38.  
  39. const int N = 100043;
  40.  
  41. int color[N];
  42. set<int> g[N];
  43. int n;
  44.  
  45. const int C1 = -1;
  46. const int C2 = -2;
  47.  
  48. bool step(queue<pair<int, set<int>::iterator> >& a, vector<int>& cur, int c)
  49. {
  50. if(a.empty())
  51. return false;
  52. auto k = a.front();
  53. a.pop();
  54. if(k.y == g[k.x].end())
  55. return true;
  56. int to = *k.y;
  57. k.y++;
  58. a.push(mp(k.x, k.y));
  59. if(color[to] != c)
  60. {
  61. color[to] = c;
  62. cur.pb(to);
  63. a.push(mp(to, g[to].begin()));
  64. }
  65. return true;
  66. }
  67.  
  68. inline bool read() {
  69. scanf("%d", &n);
  70. return true;
  71. }
  72.  
  73. char buf[5];
  74.  
  75. void ask(int x, int y)
  76. {
  77. if(color[x] == color[y])
  78. puts("YES");
  79. else
  80. puts("NO");
  81. fflush(stdout);
  82. }
  83.  
  84. int last_color = 100043;
  85.  
  86. void jfs(int x, int y, bool m)
  87. {
  88. queue<pair<int, set<int>::iterator> > q1, q2;
  89. vector<int> cur1, cur2;
  90. q1.push(mp(x, g[x].begin()));
  91. int c1 = color[x];
  92. color[x] = C1;
  93. q2.push(mp(y, g[y].begin()));
  94. int c2 = color[y];
  95. color[y] = C2;
  96. cur1.pb(x);
  97. cur2.pb(y);
  98. bool ft = true;
  99. while(true)
  100. {
  101. if(!step(q1, cur1, C1))
  102. {
  103. ft = true;
  104. break;
  105. }
  106. if(!step(q2, cur2, C2))
  107. {
  108. ft = false;
  109. break;
  110. }
  111. }
  112. if(ft)
  113. {
  114. if(m)
  115. {
  116. for(auto x : cur2) color[x] = c2;
  117. for(auto x : cur1) color[x] = c2;
  118. }
  119. else
  120. {
  121. int cc = last_color++;
  122. for(auto x : cur2) color[x] = c2;
  123. for(auto x : cur1) color[x] = cc;
  124. }
  125. }
  126. else
  127. {
  128. if(m)
  129. {
  130. for(auto x : cur2) color[x] = c1;
  131. for(auto x : cur1) color[x] = c1;
  132. }
  133. else
  134. {
  135. int cc = last_color++;
  136. for(auto x : cur2) color[x] = cc;
  137. for(auto x : cur1) color[x] = c1;
  138. }
  139. }
  140. }
  141.  
  142. void merge(int x, int y)
  143. {
  144. jfs(x, y, true);
  145. g[x].insert(y);
  146. g[y].insert(x);
  147. }
  148.  
  149. void split(int x, int y)
  150. {
  151. g[x].erase(y);
  152. g[y].erase(x);
  153. jfs(x, y, false);
  154. }
  155.  
  156. inline void solve() {
  157. forn(i, n) color[i] = i;
  158. while(true)
  159. {
  160. int x, y;
  161. scanf("%s", buf);
  162. string s = buf;
  163. if(s == "C")
  164. {
  165. scanf("%d %d", &x, &y);
  166. --x;
  167. --y;
  168. merge(x, y);
  169. }
  170. if(s == "D")
  171. {
  172. scanf("%d %d", &x, &y);
  173. --x;
  174. --y;
  175. split(x, y);
  176. }
  177. if(s == "E")
  178. {
  179. return;
  180. }
  181. if(s == "T")
  182. {
  183. scanf("%d %d", &x, &y);
  184. --x;
  185. --y;
  186. ask(x, y);
  187. }
  188. }
  189. }
  190.  
  191. int main() {
  192. #ifdef _DEBUG
  193. // freopen("input.txt", "r", stdin);
  194. int tt = clock();
  195. #endif
  196. cout << fixed << setprecision(12);
  197. int tc;
  198. tc = 1;
  199. while(tc--){
  200. read();
  201. solve();
  202.  
  203. #ifdef _DEBUG
  204. cerr << "TIME = " << clock() - tt << endl;
  205. tt = clock();
  206. #endif
  207. }
  208. return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement