Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. // O(n log n) offline solution for dynamic connectivity problem.
  2. // Query types:
  3. // {1, {a, b}} add edge. If edge already exists nothing happns.
  4. // {2, {a, b}} remove edge. If no edge exists nothing happens.
  5. // {3, {0, 0}} count number of connected components.
  6. // Uses 1-indexing
  7. #include <bits/stdc++.h>
  8. #define F first
  9. #define S second
  10. using namespace std;
  11.  
  12. struct DynamicConnectivity {
  13. struct edge{
  14. int a,b,l,r;
  15. };
  16. vector<int> ret,tq,id,is;
  17. vector<vector<int> > g;
  18. int dfs(int x, int c) {
  19. id[x]=c;
  20. int r=is[x];
  21. for (int nx:g[x]) {
  22. if (!id[nx]) r|=dfs(nx, c);
  23. }
  24. return r;
  25. }
  26. void go(int l, int r, int n, int out, vector<edge> es) {
  27. vector<edge> nes;
  28. for (int i=1;i<=n;i++) {
  29. g[i].clear();
  30. id[i]=0;
  31. is[i]=0;
  32. }
  33. for (auto e:es) {
  34. if (e.l>r||e.r<l||e.a==e.b) continue;
  35. if (e.l<=l&&r<=e.r) {
  36. g[e.a].push_back(e.b);
  37. g[e.b].push_back(e.a);
  38. }
  39. else {
  40. nes.push_back(e);
  41. is[e.a]=1;
  42. is[e.b]=1;
  43. }
  44. }
  45. int i2=1;
  46. for (int i=1;i<=n;i++) {
  47. if ((int)g[i].size()>0||is[i]) {
  48. if (!id[i]) {
  49. int a=dfs(i, i2);
  50. if (!a) out++;
  51. else i2++;
  52. }
  53. }
  54. else {
  55. out++;
  56. }
  57. }
  58. for (auto&e:nes) {
  59. e.a=id[e.a];
  60. e.b=id[e.b];
  61. }
  62. if (l==r) {
  63. if (tq[l]) ret[tq[l]-1]=out+i2-1;
  64. }
  65. else {
  66. int m=(l+r)/2;
  67. go(l, m, i2-1, out, nes);
  68. go(m+1, r, i2-1, out, nes);
  69. }
  70. }
  71. vector<int> solve(int n, vector<pair<int, pair<int, int> > > queries) {
  72. map<pair<int, int>, int> ae;
  73. tq.resize(queries.size());
  74. id.resize(n+1);
  75. is.resize(n+1);
  76. g.resize(n+1);
  77. int qs=0;
  78. vector<edge> es;
  79. for (int i=0;i<(int)queries.size();i++) {
  80. auto q=queries[i];
  81. if (q.S.F>q.S.S) swap(q.S.F, q.S.S);
  82. if (q.F==1) {
  83. if (ae[q.S]==0) ae[q.S]=i+1;
  84. }
  85. else if(q.F==2) {
  86. if (ae[q.S]) {
  87. es.push_back({q.S.F, q.S.S, ae[q.S]-1, i});
  88. ae[q.S]=0;
  89. }
  90. }
  91. else if (q.F==3) {
  92. tq[i]=1+qs++;
  93. }
  94. }
  95. for (auto e:ae){
  96. if (e.S) es.push_back({e.F.F, e.F.S, e.S-1, (int)queries.size()});
  97. }
  98. ret.resize(qs);
  99. if ((int)queries.size()>0) go(0, (int)queries.size()-1, n, 0, es);
  100. return ret;
  101. }
  102. };
  103.  
  104. int main(){
  105. freopen("connect.in", "r", stdin);
  106. freopen("connect.out", "w" ,stdout);
  107. ios_base::sync_with_stdio(0);
  108. cin.tie(0);
  109. int n,k;
  110. cin>>n>>k;
  111. vector<pair<int, pair<int, int> > > qs;
  112. for (int i=0;i<k;i++){
  113. char c;
  114. cin>>c;
  115. if (c=='?') {
  116. qs.push_back({3, {0, 0}});
  117. }
  118. else if(c=='+'){
  119. int a,b;
  120. cin>>a>>b;
  121. qs.push_back({1, {a, b}});
  122. }
  123. else if(c=='-'){
  124. int a,b;
  125. cin>>a>>b;
  126. qs.push_back({2, {a, b}});
  127. }
  128. }
  129. DynamicConnectivity t;
  130. vector<int> v=t.solve(n, qs);
  131. for (int vv:v){
  132. cout<<vv<<'\n';
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement