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

题意:

  每两个点如果他们的gcd大于1的话就可以连一条边,问在这些数里面有多少个联通块。

题解:

  我们可以用筛法倍数。然后用并查集将他们连通起来,2 3 6 本来2 和3的gcd 为1,但是他们可以通过6使得连通。

  还有就是要注意 15 25 35 这个数据。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
int num[maxn];
int F[maxn];
vector <int> now;
int findfa(int x)
{
if(F[x]==x){
return x;
}
else return F[x] = findfa(F[x]);
}
int join(int x, int y)
{
int f1 = findfa(x);
int f2 = findfa(y);
if(f1!=f2){
if(f1>f2) swap(f1, f2);
F[f2] = f1;
}
}
void solve()
{
ms(num, );
for(int i = ;i<maxn;i++) F[i] = i;
int n, x, Max = ;
scanf("%d", &n);
for(int i = ;i<n;i++){
scanf("%d", &x);
num[x]++;
Max = max(Max, x);
}
for(int i = ;i<=Max;i++){
now.clear();
for(int j = ;i*j<=Max;j++){
if(num[i*j])
now.pb(i*j);
}
if(now.size()>=){
for(int k = ;k<now.size();k++)
join(now[], now[k]);
}
}
int ans = ;
for(int i = ;i<=maxn;i++){
if(num[i]&&i==F[i]){
ans++;
}
}
ans += num[];
printf("%d", ans);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// IOS
int t;scanf("%d", &t);
int cnt = ;
while(t--){
printf("Case %d: ", cnt++);
solve();
printf("\n");
}
return ;
}

uva live 7638 Number of Connected Components (并查集)的更多相关文章

  1. CF-292D Connected Components 并查集 好题

    D. Connected Components 题意 现在有n个点,m条编号为1-m的无向边,给出k个询问,每个询问给出区间[l,r],让输出删除标号为l-r的边后还有几个连通块? 思路 去除编号为[ ...

  2. 【并查集】【枚举倍数】UVALive - 7638 - Number of Connected Components

    题意:n个点,每个点有一个点权.两个点之间有边相连的充要条件是它们的点权不互素,问你这张图的连通块数. 从小到大枚举每个素数,然后枚举每个素数的倍数,只要这个素数的某个倍数存在,就用并查集在这些倍数之 ...

  3. [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), ...

  4. LeetCode Number of Connected Components in an Undirected Graph

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

  5. [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 ...

  6. [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), ...

  7. 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 ...

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

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

  9. 323. 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), ...

随机推荐

  1. vue视图不更新情况详解

    vue视图不更新情况详解:https://www.jb51.net/article/161371.htm

  2. [2019上海网络赛F题]Rhyme scheme

    题目链接 题意,求出合法的长度为n的字典序第k小字符串,合法的定义为除了最后一位,每一位的取值范围为'A'到'A'+pos-1,而最后一位的取值范围'A'到当前字符串最大值+1. 队友tql,Orz ...

  3. 一、JVM — Java内存区域

    Java 内存区域详解 写在前面 (常见面试题) 基本问题 拓展问题 一 概述 二 运行时数据区域 2.1 程序计数器 2.2 Java 虚拟机栈 2.3 本地方法栈 2.4 堆 2.5 方法区 2. ...

  4. SGU 521 North-East ( 二维LIS 线段树优化 )

    521. "North-East" Time limit per test: 0.5 second(s)Memory limit: 262144 kilobytes input: ...

  5. PHP MVC结构系统架构设计

    今天研究了下PHP MVC结构,所以决定自己写个简单的MVC,以待以后有空再丰富.至于什么MVC结构,其实就是三个Model,Contraller,View单词的简称,,Model,主要任务就是把数据 ...

  6. 如何让form2中的数据源,显示在form1的dataGridView控件中呢????

    定义一个static的静态变量,即可全局访问

  7. css实现斑马线效果

    文本实现斑马线效果 <style> p { font-size: 17px; line-height: 25px; background-color: antiquewhite; back ...

  8. sql中关闭自增,并插入数据

    ET IDENTITY_INSERT 允许将显式值插入表的标识列中. 语法 SET IDENTITY_INSERT [ database.[ owner.] ] { table } { ON | OF ...

  9. maven多模块tomcat启动报 NoClassDefFoundError:com/test/main/message

    maven多模块tomcat启动报 NoClassDefFoundError:com/test/main/message 扫描不到 添加子模块jar包

  10. Python 余弦相似度与皮尔逊相关系数 计算

    夹角余弦(Cosine) 也可以叫余弦相似度. 几何中夹角余弦可用来衡量两个向量方向的差异,机器学习中借用这一概念来衡量样本向量之间的差异. (1)在二维空间中向量A(x1,y1)与向量B(x2,y2 ...