BZOJ 3237([Ahoi2013]连通图-cdq图重构-连通性缩点)

        BZOJ 3237([Ahoi2013]连通图-cdq图重构-连通性缩点)

分类: CDQ分治 508人阅读 评论(0) 收藏 举报

目录(?)[+]

  1. Ahoi2013连通图
  2. Description
  3. Input
  4. Output
  5. Sample Input
  6. Sample Output
  7. HINT
  8. Source

3237: [Ahoi2013]连通图

Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 106 Solved: 31
[ Submit][ Status]

Description

BZOJ 3237([Ahoi2013]连通图-cdq图重构-连通性缩点)_第1张图片

Input

BZOJ 3237([Ahoi2013]连通图-cdq图重构-连通性缩点)_第2张图片

Output

BZOJ 3237([Ahoi2013]连通图-cdq图重构-连通性缩点)_第3张图片

Sample Input

4 5
1 2
2 3
3 4
4 1
2 4
3
1 5
2 2 3
2 1 2

Sample Output



Connected
Disconnected
Connected

HINT


N<=100000 M<=200000 K<=100000

Source



弱B。。的弱B题解。。。

首先我们知道,可以把提问中没问的边缩成点。

但是不影响复杂度。。。

所以我们,把它拆成2半。。

前一半缩点(不考虑后一半的询问),乱搞,后一半的不用考虑前一半的询问,乱搞。。。

于是f(q)=f(q/2)+O(qc*a(qc)) O(f(q))=O(qlogqc*α(qc))


[cpp] view plain copy print ?
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<functional>
  6. #include<iostream>
  7. #include<cmath>
  8. using namespace std;
  9. #define For(i,n) for(int i=1;i<=n;i++)
  10. #define Fork(i,k,n) for(int i=k;i<=n;i++)
  11. #define Rep(i,n) for(int i=0;i<n;i++)
  12. #define ForD(i,n) for(int i=n;i;i--)
  13. #define RepD(i,n) for(int i=n;i>=0;i--)
  14. #define Forp(x) for(int p=pre[x];p;p=next[p])
  15. #define Lson (x<<1)
  16. #define Rson ((x<<1)+1)
  17. #define MEMr(a,n,w) Rep(i,n) a[i]=w;
  18. #define MEMF(a,n,w) For(i,n) a[i]=w;
  19. #define MEM(a) memset(a,0,sizeof(a));
  20. #define MEMI(a) memset(a,127,sizeof(a));
  21. #define MEMi(a) memset(a,128,sizeof(a));
  22. #define INF (2139062143)
  23. #define F (100000007)
  24. #define MAXN (100000+10)
  25. #define MAXM (200000+10)
  26. #define MAXQ (100000+10)
  27. #define MAXC (4)
  28. long long mul(long long a,long long b){return (a*b)%F;}
  29. long long add(long long a,long long b){return (a+b)%F;}
  30. long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
  31. typedef long long ll;
  32. int n,m,q;
  33. struct comm
  34. {
  35. int n,a[4];
  36. }ask[MAXQ],back[MAXQ*30],*back_tail=back;
  37. struct E
  38. {
  39. int x,y;
  40. }e[MAXM*30],*e_tail=e;
  41. struct unionset
  42. {
  43. int father[MAXN];
  44. void init(int n){For(i,n) father[i]=i;}
  45. int getfather(int x)
  46. {
  47. if (father[x]==x) return x;
  48. return father[x]=getfather(father[x]);
  49. }
  50. bool union2(int x,int y)
  51. {
  52. if (getfather(x)==getfather(y)) return 0;
  53. father[father[x]]=father[y]; return 1;
  54. }
  55. }ufs;
  56. bool ans[MAXQ]={0};
  57. int newV[MAXN],newE[MAXM];
  58. void solve(int n,E *_e,int m,int l,int r)
  59. {
  60. e_tail+=m;
  61. E *e=e_tail;
  62. copy(_e,e_tail,e);
  63. static bool b[MAXM]={0};MEMr(b,m,0);
  64. if (l==r)
  65. {
  66. Rep(j,ask[l].n) b[ask[l].a[j]]=1;
  67. ufs.init(n);
  68. int tot=0;
  69. Rep(i,m) if (!b[i]) tot+=ufs.union2(e[i].x,e[i].y);
  70. if (tot==n-1) ans[l]=1;
  71. e_tail-=m;
  72. return;
  73. }
  74. Fork(i,l,r) Rep(j,ask[i].n) b[ask[i].a[j]]=1;
  75. ufs.init(n);
  76. Rep(i,m) if (!b[i]) ufs.union2(e[i].x,e[i].y);
  77. //Con
  78. int n2=0;
  79. For(i,n) if (ufs.getfather(i)==i) newV[i]=++n2;
  80. For(i,n) if (ufs.getfather(i)^i) newV[i]=newV[ufs.getfather(i)];
  81. Rep(i,m) e[i].x=newV[e[i].x],e[i].y=newV[e[i].y];
  82. //Red
  83. int m2=0;
  84. Rep(i,m) if (b[i]) newE[i]=m2++;
  85. Rep(i,m) if (b[i]) e[newE[i]]=e[i];
  86. Fork(i,l,r) Rep(j,ask[i].n) ask[i].a[j]=newE[ask[i].a[j]];
  87. {
  88. int m=l+r>>1,len=m-l+1;
  89. comm *back_head=back_tail;
  90. back_tail+=len;
  91. copy(ask+l,ask+m+1,back_head);
  92. solve(n2,e,m2,l,m);
  93. copy(back_head,back_head+len,ask+l);
  94. back_tail-=len;
  95. solve(n2,e,m2,m+1,r);
  96. }
  97. e_tail-=m;
  98. }
  99. int main()
  100. {
  101. // freopen("bzoj3237.in","r",stdin);
  102. scanf("%d%d",&n,&m);
  103. Rep(i,m) scanf("%d%d",&e[i].x,&e[i].y);
  104. scanf("%d",&q);
  105. Rep(i,q)
  106. {
  107. scanf("%d",&ask[i].n);
  108. Rep(j,ask[i].n) scanf("%d",&ask[i].a[j]),ask[i].a[j]--;
  109. }
  110. solve(n,e,m,0,q-1);
  111. Rep(i,q) if (ans[i]) puts("Connected");else puts("Disconnected");
  112. return 0;
  113. }  

你可能感兴趣的:(cdq分治)