uva live 7638 Number of Connected Components (并查集)
题意:
每两个点如果他们的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 (并查集)的更多相关文章
- CF-292D Connected Components 并查集 好题
D. Connected Components 题意 现在有n个点,m条编号为1-m的无向边,给出k个询问,每个询问给出区间[l,r],让输出删除标号为l-r的边后还有几个连通块? 思路 去除编号为[ ...
- 【并查集】【枚举倍数】UVALive - 7638 - Number of Connected Components
题意:n个点,每个点有一个点权.两个点之间有边相连的充要条件是它们的点权不互素,问你这张图的连通块数. 从小到大枚举每个素数,然后枚举每个素数的倍数,只要这个素数的某个倍数存在,就用并查集在这些倍数之 ...
- [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), ...
- 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 ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
随机推荐
- 为什么 Java 线程没有 Running 状态?
Java虚拟机层面所暴露给我们的状态,与操作系统底层的线程状态是两个不同层面的事.具体而言,这里说的 Java 线程状态均来自于 Thread 类下的 State 这一内部枚举类中所定义的状态: 什么 ...
- 【SSL1786】麻将游戏
题目大意: 给出一个矩阵,查询其中两个点连通线段数 正文: 看这题好眼熟... 实质和这道题是一模一样的,只不过由一条询问升级到多条询问.
- mysql5.7单机多实例安装
基于之前的mysql5.7单实例安装 修改/etc/my.cnf文件如下(这里配置4个实例,可自行修改数目) # # 多实例配置文件,可以mysqld_multi --example 查看例子 # [ ...
- php批量POST修改
这是一个thinkphp中的批量修改的案例: 如需要删除多项,或者同时修改多项记录 要点: 前端表单中name要加[],如:<input type="hidden" name ...
- tensorflow学习笔记六----------神经网络
使用mnist数据集进行神经网络的构建 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from ...
- kotlin学习(6)运算符重载和其他约定
约定 在Kotlin中,可以调用自己代码中定义的函数,来实现语言结构.这戏功能与特定的函数命名相关,例如,在你的类中定义了一个名为plus的特殊方法,那么按照约定,就可以在该类的实例上使用 + 运算符 ...
- ajax使用jsonp跨域调用webservice error错误信息"readyState":4,"status":200,"statusText":"success"
主要还是接口写有问题 至于ajax保持简洁写法即可 $.ajax({ dataType: 'jsonp', type: ‘get’, data: {}, url: '' })
- fpga配置方式 .jic固化为ps模式
FPGA不同下载方式的区别[扫盲]以及如何利用AS模式固化程序(转载) 主动配置方式(AS)和被动配置方式(PS)和最常用的(JTAG)配置方式: AS由FPGA器件引导配置操作过程,它控制着 ...
- [转载]Quartus ii 一些Warning/Eeror分析与解决
我会在此基础上继续添加 原文地址:ii 一些Warning/Eeror分析与解决">Quartus ii 一些Warning/Eeror分析与解决作者:yanppf 注:http:// ...
- poj3494Largest Submatrix of All 1’s(最大全1子矩阵)
题目链接:http://poj.org/problem?id=3494 题目大意: 出1个M*N的矩阵M1,里面的元素只有0或1,找出M1的一个子矩阵M2,M2中的元素只有1,并且M2的面积是最大的. ...