【并查集】【枚举倍数】UVALive - 7638 - Number of Connected Components
题意: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的更多相关文章
- uva live 7638 Number of Connected Components (并查集)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 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 ...
- 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), ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- [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), ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [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 ...
- [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), ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
随机推荐
- JSP和Servlet面试题
1.讲下servlet的执行流程. Servlet的执行流程也就是servlet的生命周期,当服务器启动的时候生命周期开始,然后通过init()<启动顺序根据web.xml里的startup-o ...
- 巧用margin/padding的百分比值实现高度自适应
原文:https://segmentfault.com/a/1190000004231995 一个基础却又容易混淆的css知识点 本文依赖于一个基础却又容易混淆的css知识点:当margin/padd ...
- D - Keiichi Tsuchiya the Drift King Gym - 102028D (几何)
题目链接:https://cn.vjudge.net/contest/275150#problem/D 题目大意: 问你能满足那个矩形可以顺利通过的条件,然后求出最小的w 具体思路:首先,我们应该将情 ...
- TinyOS在ubuntu 14.04下安装教程
1:打开/etc/apt/sources.list 文件,在文件最底部添加安装源: deb http://tinyos.stanford.edu/tinyos/dists/ubuntu lucid m ...
- Python3 反射及常用的方法
反射就是通过字符串映射或修改程序运行时的状态.属性.方法 有四个常用方法: hasattr(obj,name_str) 判断一个obj对象是否有对应name_str的方法 getattr(obj,na ...
- 【Python学习】字符编码
先说两个基础知识. (1)计算机内部,数据是由0,1组成的: (2)计算机最小的数据单位,就是一个二进制单位即bit,接下来就是8个二进制单位表示一个字节(Byte). 1 ASCII码 ASCII码 ...
- C++之C/C++内存对齐
一.什么是字节对齐,为什么要对齐 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问,这 ...
- MGR Switch single-Primary to Muti_primary
MGR single_primary 切换 Muti-Primary 模式 root@localhost [(none)]>select * from performance_schema.re ...
- Nginx实现404页面的几种方法【转】
一个网站项目,肯定是避免不了404页面的,通常使用Nginx作为Web服务器时,有以下集中配置方式,一起来看看. 第一种:Nginx自己的错误页面 Nginx访问一个静态的html 页面,当这个页面没 ...
- 深入解析Mysql 主从同步延迟原理及解决方案
MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响主服务器服务;③当主服务器出现问题时,可以 ...