题意: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. 关于SQLite3 编译及交叉编译的一些问题

    from : http://blog.sina.com.cn/s/blog_5f2e119b0101ibwn.html SQLite3 (http://www.sqlite.org)是一个非常强大的小 ...

  2. MySQL登录问题1045 (28000)处理步骤【原创】

    MySQL登录问题1045 (28000)  俩台服务器主从复制,从的同步账号无法远程登录主服务器.报错ERROR 1045 (28000): Access denied for user 'root ...

  3. 017 CPU冲高定位方法

    1.通过top命令查看cpu占用高的进程ID; 2.通过top -Hp 进程ID 查看该进程下所有线程占用cpu的情况,拿出占用cpu最高的线程ID,换算成十六进制; 3.通过 jstack 进程ID ...

  4. C基础 time.h 简单思路扩展

    前言 - time 简单需求 时间业务相关代码. 基本属于框架的最底层. 涉及的变动都很小. 以前参与游戏研发时候, 这方面需求不少, 各种被策划花式吊打. 转行开发互联网服务之后很少遇到这方面需求. ...

  5. untiy3d学习笔记

    Unity3d 记录 1.63讲 主要讲了menicam 从3D软件里面导出过后,注意如果是人物模型命名一定要非常清晰并且对称.选择到模型后等到到humanoid后可以使用menicam.然后使用me ...

  6. centos6.5 使用 rpm 安装 mysql

    从mysql网站下载mysql rpm安装包(包括server.client) 1.安装server rpm -ivh MySQL-server-5.6.19-1.el6.x86_64.rpm 强制安 ...

  7. 机顶盒 gettimeofday()获取毫秒溢出

    最近在写代码的时候遇见了一个bug,在获取当前时间戳的毫秒时,我自己测试的时候总是OK的,但是测试那边总是测不对,之前一直以为是因为我存储的类型的不对,从long long类型从lld改成llu,然后 ...

  8. 以太坊go-ethereum客户端JSON-RPC API调用(一)

    前几篇博客主要介绍了go-ethereum客户端不同环境的搭建,今天这篇博客是建立在前几排博客的基础上.当搭建完成之后,我们可以通过各种方式与节点进行交互(JavaScript Console.JSO ...

  9. WP SMTP插件为啥我一直设置的不对?

    我也是摸索好久才搞定的,如果你是万网空间先去修改一下参数在万网后台设置PHP.ini参数设置,因为万网阿里云免费虚拟主机禁用了WordPress默认使用的PHP mail()发信函数,而 stream ...

  10. Mysql锁的类型与简析

    数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重要数据结构. 根据加锁的范围,MySQL 里面的锁大致 ...