题意:n个点,每个点有一个点权。两个点之间有边相连的充要条件是它们的点权不互素,问你这张图的连通块数。

从小到大枚举每个素数,然后枚举每个素数的倍数,只要这个素数的某个倍数存在,就用并查集在这些倍数之间都连上边。然后输出最后的集合数量即可。

注意,点权为1的点都会自成一个连通块。

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. int T,a[1000006],fa[1000006],f[1000006],p[1000006],ans,n,t,notPrime[1000006],prime[1000006],num;
  8. int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
  9. void un(int x,int y)
  10. {
  11. int Y=get(y);int X=get(x);
  12. if(X!=Y)
  13. {
  14. fa[X]=Y;
  15. }
  16. }
  17. char ch;
  18. int temp;
  19. int read()
  20. {
  21. while(ch=getchar(),ch<'0'||ch>'9');
  22. temp=ch-'0';
  23. while(ch=getchar(),ch<='9'&&ch>='0')
  24. temp=temp*10+ch-'0';
  25. return temp;
  26. }
  27. int main()
  28. {
  29. // freopen("1.in","r",stdin);
  30. // freopen("1.out","w",stdout);
  31. for(int i=2;i<=1000000;++i)
  32. {
  33. if(!notPrime[i])
  34. {
  35. prime[++num]=i;
  36. }
  37. for(int j=1;j<=num&&prime[j]*i<=1000000;++j)
  38. {
  39. notPrime[i*prime[j]]=1;
  40. if(i%prime[j]==0) break;
  41. }
  42. }
  43. scanf("%d",&T);
  44. int maxi=1000000;
  45. for(int tt=1;tt<=T;++tt)
  46. {
  47. n=read();
  48. for(int i=1;i<=maxi;++i)
  49. {
  50. fa[i]=i;
  51. f[i]=0;
  52. a[i]=0;
  53. }
  54. maxi=0;
  55. ans=0;
  56. for(int i=1;i<=n;++i)
  57. {
  58. t=read();
  59. if(t==1) ans++;
  60. maxi=max(maxi,t);
  61. a[t]=1;
  62. }
  63. for(int i=1;i<=num;++i)
  64. {
  65. int now=prime[i];
  66. int tail=0;
  67. for(int j=1;now*j<=maxi;++j)
  68. {
  69. if(a[now*j])
  70. p[++tail]=now*j;
  71. }
  72. for(int j=1;j<tail;++j)
  73. {
  74. un(p[j],p[j+1]);
  75. }
  76. }
  77. for(int i=2;i<=maxi;++i)
  78. if(a[i])
  79. {
  80. if(!f[get(i)]) ans++;
  81. f[get(i)]=1;
  82. }
  83. printf("Case %d: %d\n",tt,ans);
  84. }
  85.  
  86. }

【并查集】【枚举倍数】UVALive - 7638 - Number of Connected Components的更多相关文章

  1. uva live 7638 Number of Connected Components (并查集)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集

    [抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...

  3. Number of Connected Components in an Undirected Graph -- LeetCode

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  5. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  6. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  7. [Locked] Number of Connected Components in an Undirected Graph

    Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...

  8. [Swift]LeetCode323. 无向图中的连通区域的个数 $ Number of Connected Components in an Undirected Graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  9. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

随机推荐

  1. 你不知道的Static

    Static静态字段,静态方法,静态代码块 壹  简介 一些场景下会要求一个类的多个实例共享一个成员变量:有时候想定义一些不和具体对象关联.不需要new就调用的方法 举例:Console类的Write ...

  2. 持续集成工具Jenkins安装、部署、使用

    本文介绍jenkins,利用其做项目发布与持续集成交付工具. 一.Jenkins是什么? Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布 ...

  3. 使用inline-block,使前面img,后面空div居中显示在一行后,导致当div中有内容时,div下移问题

    .pro_li img,.pro_sm{display: inline-block; *display:inline;*zoom:1;vertical-align: middle ;} 解决方法:使用 ...

  4. 2017ACM暑期多校联合训练 - Team 5 1006 HDU 5205 Rikka with Graph (找规律)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  5. java检验银行卡号

    /* 校验过程: 1.从卡号最后一位数字开始,逆向将奇数位(1.3.5等等)相加. 2.从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,将个位十位数字相加,即将其减去9),再求和 ...

  6. 购物车中的input输入框只能输入数字和输入为0的时候默认为1

    <input type="text" value="1" onkeyup="value=(parseInt((value=value.repla ...

  7. win32的回调函数

    [转]http://blog.csdn.net/w419675647/article/details/6599070 众所周知,win32的回调函数WndProc()是操作系统调用的函数,win32用 ...

  8. elk系列4之kibana图形化操作【转】

    preface 我们都搭建了ELK系统,且日志也能够正常收集的时候,那么就配置下kibana.我们可以通过kibana配置柱状图,趋势图,统计图,圆饼图等等各类图.下面就拿配置统计图和柱状图为例,结合 ...

  9. Petrozavodsk Summer Training Camp 2017 Day 9

    Petrozavodsk Summer Training Camp 2017 Day 9 Problem A. Building 题目描述:给出一棵树,在树上取出一条简单路径,使得该路径的最长上升子序 ...

  10. JavaScript 跳转 页面

    * window.location.href , self.location, window.location 出现问题不能跳转 Chome 不能本页跳转, IE 有时可以