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

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

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

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int T,a[1000006],fa[1000006],f[1000006],p[1000006],ans,n,t,notPrime[1000006],prime[1000006],num;
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
void un(int x,int y)
{
int Y=get(y);int X=get(x);
if(X!=Y)
{
fa[X]=Y;
}
}
char ch;
int temp;
int read()
{
while(ch=getchar(),ch<'0'||ch>'9');
temp=ch-'0';
while(ch=getchar(),ch<='9'&&ch>='0')
temp=temp*10+ch-'0';
return temp;
}
int main()
{
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
for(int i=2;i<=1000000;++i)
{
if(!notPrime[i])
{
prime[++num]=i;
}
for(int j=1;j<=num&&prime[j]*i<=1000000;++j)
{
notPrime[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
scanf("%d",&T);
int maxi=1000000;
for(int tt=1;tt<=T;++tt)
{
n=read();
for(int i=1;i<=maxi;++i)
{
fa[i]=i;
f[i]=0;
a[i]=0;
}
maxi=0;
ans=0;
for(int i=1;i<=n;++i)
{
t=read();
if(t==1) ans++;
maxi=max(maxi,t);
a[t]=1;
}
for(int i=1;i<=num;++i)
{
int now=prime[i];
int tail=0;
for(int j=1;now*j<=maxi;++j)
{
if(a[now*j])
p[++tail]=now*j;
}
for(int j=1;j<tail;++j)
{
un(p[j],p[j+1]);
}
}
for(int i=2;i<=maxi;++i)
if(a[i])
{
if(!f[get(i)]) ans++;
f[get(i)]=1;
}
printf("Case %d: %d\n",tt,ans);
} }

【并查集】【枚举倍数】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. JS 检测客户端断网情况

    常用方法 1 navigator.onLine 2 window.addEventListener() 3 获取网络资源 4 ajax请求 1. navigator.onLine 只会在机器未连上路由 ...

  2. 使用vscode实现git同步

    用了git最方便的就是项目同步管理,回到家打开vscode只需要点击一下pull就能全部同步过来.是不是很方便....毕竟之前我都是拿u盘拷贝回家或者存到云盘再下载下来..   我这里之前用的是国内的 ...

  3. callee与caller

    1.callee arguments.callee表示当前函数,使用于递归 function factorial(num){ if(num<=1){ return 1; }else{ retur ...

  4. AngularJs 文件上传(实现Multipart/form-data 文件的上传)

    <!-- 上传yml文件 --> <div class="blackBoard" ng-show="vm.showUpop==true"> ...

  5. Linux进程调度原理【转】

    转自:http://www.cnblogs.com/zhaoyl/archive/2012/09/04/2671156.html Linux进程调度的目标 1.高效性:高效意味着在相同的时间下要完成更 ...

  6. i8042 键盘控制器-------详细介绍

    [转]http://shanzy.bokee.com/834368.html ps/2 键盘硬件概述 对于驱动来说,和键盘相关的最重要的硬件是两个芯片.一个是 intel 8042 芯片,位于主板上, ...

  7. linux kernel make构建分析

    前言 之前对uboot的构建进行了分析,现在再对linux kernel的构建进行分析.几年前的确也分析过,但是只是停留在笔记层面,没有转为文章,这次下定决定来完善它. 环境 同样,采用的还是zynq ...

  8. Denoise Autoencoder简单理解

    自编码器通过学习隐含特征来表达原始数据,那什么是denoise autoencoder呢? 关于Autoencoder参考:http://blog.csdn.net/on2way/article/de ...

  9. TCP可靠传输和拥塞控制

    1.TCP的可靠传输 tcp的可靠传输主要靠 来自接收方的确认报文 和 超时重传. 发出报文,计时器开始计时,在规定超时时间内未收到确认报文则重新发送. 注意:发送报文都留一个副本,如果收到确认报文就 ...

  10. ajax刷新输出实时数据

    setInterval('shuaxin()',3000); function shuaxin(){ $.ajax({//股票 url:"http://apimarkets.wallstre ...