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), ...
随机推荐
- Dubbo学习(一) Dubbo原理浅析
一.初入Dubbo Dubbo学习文档: http://dubbo.incubator.apache.org/books/dubbo-user-book/ http://dubbo.incubator ...
- Spring、SpringMVC、MyBatis整合
项目结构: 新建web项目:File->new->Dynamic Web Project 一.准备所需jar包1. Spring框架的jar包 spring-framework-5.0.4 ...
- Spring Cloud与微服务构建:Spring Cloud简介
Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...
- BZOJ5089 最大连续子段和(分块)
假设所有操作都是对整个序列的.考虑每个子区间,区间和与其被加的值构成一次函数关系.最大子段和相当于多个子区间取最大值,答案显然就在这些一次函数构成的下凸壳上.如果预处理出凸壳,只要在凸壳上暴力跳就可以 ...
- MugLife app是一款可以将静态照片变成3D动画的手机应用
MugLife app是一款可以将静态照片变成3D动画的手机应用,如下效果图所示: 大家可以看到,这个静态图具有了类3D的动画特效,是不是很好玩? 这种算法是如何实现的呢? 这里给出一篇论文“Brin ...
- HTML的标签元素分类的区别
HTML ,即Hyper Text Markup Language 超文本标记语言: 文本:纯字符,如window中的txt文本 超文本:在纯文本中嵌入样式,图片,音频,视频,链接等内容 HTML的基 ...
- BGP的那些安全痛点(转)
0x00 BGP(RFC 1771. RFC 4271)定义 全称是Border Gateway Protocol, 对应中文是边界网关协议,最新版本是BGPv4. BGP是互联网上一个核心的互联网去 ...
- (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- 【费用流】【网络流24题】【P4013】 数字梯形问题
Description 给定一个由 \(n\) 行数字组成的数字梯形如下图所示. 梯形的第一行有 \(m\) 个数字.从梯形的顶部的 \(m\) 个数字开始,在每个数字处可以沿左下或右下方向移动,形成 ...
- svn提交失败 :“svn: E200007: Commit failed”
引言: 在切换subversion服务器地址之后,发生的无法正确提交代码的问题org.apache.subversion.javahl.ClientException: svn: E200007: C ...