题意: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. 组合数+逆元 A - Chat Group Gym - 101775A

    题目链接:https://cn.vjudge.net/contest/274151#problem/A 具体思路:我们可以先把所有的情况算出来,为2^n.然后不合法的情况减去就可以了.注意除法的时候要 ...

  2. spring boot 自定义属性覆盖application文件属性

    参考 Spring boot源码分析-ApplicationListener应用环境: https://blog.csdn.net/jamet/article/details/78042486 加载a ...

  3. Mysql储存过程6: in / out / inout

    in 为向函数传送进去的值 out 为函数向外返回的值 intout 传送进去的值, 并且还返回这个值 )) begin then select 'true'; else select 'false' ...

  4. PhantomJS、Selenium、Chrome驱动的mac版安装和配置

    PhantomJS(爬取动态页面需要用到) 基于webkit的javaScript API.提供了css选择器,提供了处理文件的I/O操作,支持Web标准.DOM操作.JSON.HTML5.Canva ...

  5. hashCode()与equals()区别

    这两个方法均是超类Object自带的成员方法.Object类是所有Java类的祖先.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法.在不明确给出超类的情况下,Java会自 ...

  6. [ python ] 各种推导式

    各种推导式,主要使用示例演示用法 列表生成式 示例1:求0-9每个数的平方 li = [x*x for x in range(10)] print(li) # 执行结果: # [0, 1, 4, 9, ...

  7. HTML表格的基本操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  8. 关于Android不同系统版本的市场占比情况详解

    一,google官方统计的不同Android版本市场的占比强开 google统计的数据情况 这个是google官方对于不同版本的市场占比情况.这个是针对全世界所有的Android手机占比情况. 二,友 ...

  9. 495. Teemo Attacking

    In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  10. day2、购物商城

    作业:购物商城 商品展示,价格 买,加入购物车 付款,钱不够 代码如下: import codecs #登录接口,用户名密码都正确登录成功,否则失败 def login(your_name,your_ ...