[关闭]
@lawk97 2017-05-27T02:49:44.000000Z 字数 2244 阅读 955

寒假专题训练--并查集/生成树

并查集 生成树


地址

A - How Many Tables

[[HDU - 1213]

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7. int n,m,t,ans;
  8. int set[1005],size[1005],flag[1005];
  9. void build(){
  10. for (int i = 1; i <= n; ++i)
  11. {
  12. set[i]=i;
  13. size[i]=1;
  14. }
  15. }
  16. int f(int x){
  17. if (x==set[x])
  18. {
  19. return x;
  20. }else{
  21. set[x]=f(set[x]);
  22. return set[x];
  23. }
  24. }
  25. void connect(int x,int y){
  26. int fx,fy;
  27. fx=f(x);
  28. fy=f(y);
  29. if (fx==fy)
  30. {
  31. return;
  32. }
  33. if (size[fx]>=size[fy])
  34. {
  35. size[fx]+=size[fy];
  36. set[fy]=fx;
  37. }else{
  38. size[fy]+=size[fx];
  39. set[fx]=fy;
  40. }
  41. }
  42. int main(){
  43. scanf("%d",&t);
  44. while(t--){
  45. scanf("%d%d",&n,&m);
  46. build();
  47. ans=0;
  48. for (int i = 1; i <= n; ++i)
  49. {
  50. flag[i]=0;
  51. }
  52. for (int i = 1; i <= m; ++i)
  53. {
  54. int a,b;
  55. scanf("%d%d",&a,&b);
  56. connect(a,b);
  57. }
  58. for (int i = 1; i <= n; ++i)
  59. {
  60. if (flag[f(i)]==0)
  61. {
  62. flag[set[i]]=1;
  63. ans++;
  64. }
  65. }
  66. printf("%d\n",ans );
  67. }
  68. return 0;
  69. }

B - Corporative Network

[POJ - 1962]


D - 食物链

[POJ - 1182]

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7. int n,k,ans,set[150005],size[150005];
  8. struct word{
  9. int d,x,y;
  10. }w[100005];
  11. void build(){
  12. for (int i = 1; i <= 3*n; ++i)
  13. {
  14. set[i]=i;
  15. size[i]=1;
  16. }
  17. }
  18. int f(int x){
  19. if (x==set[x])
  20. {
  21. return x;
  22. }else{
  23. set[x]=f(set[x]);
  24. return set[x];
  25. }
  26. }
  27. void connect(int x,int y){
  28. int fx,fy;
  29. fx=f(x);
  30. fy=f(y);
  31. if (fx==fy)
  32. {
  33. return;
  34. }
  35. if (size[fx]>=size[fy])
  36. {
  37. size[fx]+=size[fy];
  38. set[fy]=fx;
  39. }else{
  40. size[fy]+=size[fx];
  41. set[fx]=fy;
  42. }
  43. }
  44. int main(){
  45. ans=0;
  46. scanf("%d%d",&n,&k);
  47. for (int i = 0; i < k; ++i)
  48. {
  49. scanf("%d%d%d",&w[i].d,&w[i].x,&w[i].y);
  50. }
  51. build();
  52. for (int i = 0; i < k; ++i)
  53. {
  54. int d,x,y;
  55. d=w[i].d;
  56. x=w[i].x;
  57. y=w[i].y;
  58. if (x>n||y>n)
  59. {
  60. ans++;
  61. continue;
  62. }
  63. if (d==2&&x==y)
  64. {
  65. ans++;
  66. continue;
  67. }
  68. if (d==1)
  69. {
  70. if (f(3*x-2)==f(3*y-1)||f(3*x-1)==f(3*y)||f(3*x)==f(3*y-2)||
  71. f(3*y-2)==f(3*x-1)||f(3*y-1)==f(3*x)||f(3*y)==f(3*x-2))
  72. {
  73. ans++;
  74. continue;
  75. }
  76. connect(3*x-2,3*y-2);
  77. connect(3*x-1,3*y-1);
  78. connect(3*x,3*y);
  79. }else{
  80. if (f(3*x-2)==f(3*y-2)||f(3*x-1)==f(3*y-1)||f(3*x)==f(3*y))
  81. {
  82. ans++;
  83. continue;
  84. }
  85. if (f(3*y-2)==f(3*x-1)||f(3*y-1)==f(3*x)||f(3*y)==f(3*x-2))
  86. {
  87. ans++;
  88. continue;
  89. }
  90. connect(3*x-2,3*y-1);
  91. connect(3*x-1,3*y);
  92. connect(3*x,3*y-2);
  93. }
  94. }
  95. printf("%d\n",ans );
  96. return 0;
  97. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注