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), ...
随机推荐
- bzoj1036 [ZJOI2008]树的统计Count(树链剖分)
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- Android Log详解(Log.v,Log.d,Log.i,Log.w,Log.e)
1.Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("",""); 2.Log.d的输出颜色是蓝 ...
- 解题:TJOI 2015 组合数学
题面 通过这个题理解了一下反链的概念,更新在图论知识点里了 每个点向右和下连边可以建出一张图,这个题事实上是让我们求图的最小链覆盖.Dilworth定理告诉我们,最小链覆盖等于最长反链(反链:DAG中 ...
- MySQL 第四篇:数据操作
一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...
- SSH框架测试
1.struts的测试:目的是能否正确显示页面. 流程如图: 2.spring测试:目的是能否得到bean 3.Hibernate测试:目的是能否跟数据库操作,测试事务 事务测试: 这个要向数据库中保 ...
- 约瑟夫环问题算法(M)
http://blog.csdn.net/zhuimengzh/article/details/6727221 用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出.写出C程序 ...
- linux join命令
http://note.youdao.com/noteshare?id=151c4844cac74e9b08c5dc954a1a4967
- 跟上Java8 - 日期和时间实用技巧
原文出处:王爵nice 当你开始使用Java操作日期和时间的时候,会有一些棘手.你也许会通过System.currentTimeMillis() 来返回1970年1月1日到今天的毫秒数.或者使用Dat ...
- 笔记 jquery 的一个bug解决方法积累
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处 当id或匹配条件中包含特殊字符时,浏览器控制台会报缺少")"的异常,解决办法目前有两个: ...
- redis sentinel集群
ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...