F - Number of Connected Components UVALive - 7638 (并查集 + 思维)
题目链接:https://cn.vjudge.net/contest/275589#problem/F
题目大意:就是给你n个数,如果说两个数之间的gcd!=1,那么就将这两个点连起来,问你最终这些点能形成几块
具体思路:首先,我们可以讲所有数的倍数给标记出来,然后如果有一个数是 6,我们就把2 3 6 全部指向6,这样的话,每当我们找到一个数,我们就把这个数和他的素因子连起来(并查集),往小的地方连,然后最后看一下输入的n个数是不是标记的自己,如果是自己那么这肯定是一个块,如果不是标记的自己,就证明他所在的块已经被算过了(这也太暴力了...)
AC代码:
#include<bits/stdc++.h>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = +;
int a[maxn],vis[maxn],father[maxn];
vector<int>q[maxn];
void init()
{
for(int i=; i<=maxn; i++)
{
for(int j=i*; j<=maxn; j+=i)
{
q[j].push_back(i);
}
}
}
int Find(int t)
{
return t==father[t]?t:father[t]=Find(father[t]);
}
void cal(int t1,int t2)
{
int x1=Find(t1);
int y1=Find(t2);
if(x1!=y1)
{
if(x1>y1)father[x1]=y1;
else father[y1]=x1;
}
}
int main()
{
init();
int T;
scanf("%d",&T);
int Case=;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=; i<=maxn; i++)
{
father[i]=i;
}
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
int len=q[a[i]].size();
for(int j=; j<len; j++)
{
cal(a[i],q[a[i]][j]);
}
}
ll ans=;
for(int i=; i<=n; i++)
{
if(a[i]==)
{
ans++;
continue;
}
int t=Find(a[i]);
if(vis[t])continue;
vis[t]=;
ans++;
}
printf("Case %d: %lld\n",++Case,ans);
}
return ;
}
F - Number of Connected Components UVALive - 7638 (并查集 + 思维)的更多相关文章
- [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), ...
- 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 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
随机推荐
- Mysql innodb 间隙锁 (转)
MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面. 间隙锁(Gap Lock):锁加在不存在的空闲空间,可以是两个索引记录之间,也可能是第一个索引记 ...
- 通过ClientDataSet复制表的结构及数据
1. 需要2个ClientDataSet组件: 2. clientDataSet1连接目标表,clientDataSet2连接源表,如果无法直接连接,使用DataSetProvider进行桥接: ...
- 多态在编译器是无法确定引用类型的是哪个子类 可以用 instanceof 在运行期判断
- 【Java并发编程】之一:可重入内置锁
每个Java对象都可以用做一个实现同步的锁,这些锁被称为内置锁或监视器锁.线程在进入同步代码块之前会自动获取锁,并且在退出同步代码块时会自动释放锁.获得内置锁的唯一途径就是进入由这个锁保护的同步代码块 ...
- Eclipse报错Project configuration is not up-to-date with pom.xml
1.问题 Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. S ...
- 再谈MySql索引
一.索引简介 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 索引分单列索引(主键索引.唯一索引.普通索引)和组合索引.单列索引,即一个索引只包含单个列 ...
- MT【140】是否存在常数$\textbf{C}$
\(\textbf{天下事有难易乎?为之,则难者亦易矣 不为,则易者亦难矣------<为学>}\) (中国第59届国际数学奥林匹克国家集训队2018.3.20日测试题) 证明:存在常数\ ...
- 一步步创建第一个Docker App —— 2. 创建 Docker化 主机
原文:https://docs.docker.com/engine/getstarted-voting-app/node-setup/ 部署voting app的第一步,是为集群节点(swarm no ...
- BZOJ3601 一个人的数论 【数论 + 高斯消元】
题目链接 BZOJ3601 题解 挺神的 首先有 \[ \begin{aligned} f(n) &= \sum\limits_{x = 1}^{n} x^{d} [(x,n) = 1] \\ ...
- UOJ #126 【NOI2013】 快餐店
题目链接:快餐店 震惊!某ZZ选手此题调了一天竟是因为……>>点击查看 一般碰到这种基环树的题都要先想想树上怎么做.这道题如果是在树上的话……好像求一遍直径就做完了?答案就是直径长度的一半 ...