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), ...
随机推荐
- POJ3281_Dining
有一些饮料和食物,每种一个,每个客人喜欢一些饮料和一些食物,每个客人可以选择一种饮料和一种食物,问最多能够同时满足多少个客人同时拥有饮料和食物. 这样的,源点连接饮料,汇点连接食物,中间人分别连接饮料 ...
- Java参数引用传递之例外:null
今天写链表的时候写了一个函数,实参是一个空链表,应该是按引用传参,但是在函数内修改了链表,外部的链表没有变化. 原来是null作为参数传递的时候,就不是引用传参了. 引自:http://blog.cs ...
- CF739E Gosha is hunting 【WQS二分 + 期望】
题目链接 CF739E 题解 抓住个数的期望即为概率之和 使用\(A\)的期望为\(p[i]\) 使用\(B\)的期望为\(u[i]\) 都使用的期望为\(p[i] + u[i] - u[i]p[i] ...
- BZOJ3836 [Poi2014]Tourism 【树形dp +状压dp】
题目链接 BZOJ3836 题解 显然这是个\(NP\)完全问题,此题的解决全仗任意两点间不存在节点数超过10的简单路径的性质 这意味着什么呢? \(dfs\)树深度不超过\(10\) \(10\)很 ...
- 洛谷 P3235 [HNOI2014]江南乐 解题报告
P3235 [HNOI2014]江南乐 Description 两人进行 T 轮游戏,给定参数 F ,每轮给出 N 堆石子,先手和后手轮流选择石子数大于等于 F 的一堆,将其分成任意(大于1)堆,使得 ...
- 【poj1743】 Musical Theme
http://poj.org/problem?id=1743 (题目链接) 题意 给出n个音符的乐谱,求其中不重叠的重复最长连续段,重复连续段的定义为两段音符起伏相同. Solution 论文题,相邻 ...
- rsync命令比对文件及增量同步
A fast,versatile,remote (and local) file-copying tool. rsync基于ssh协议实现高效率远程或本地文件复制,传输速度比scp快.复制文件时会比对 ...
- Android 蓝牙模块基础操作
之前没怎么接触过蓝牙模块,在学习的过程中借鉴了很多前辈的经验.本次主要包含以下功能: 1.检测是否存在蓝牙模块 2.蓝牙的开启与关闭 3.与本机已配对的蓝牙设备 4.本机蓝牙可见性设置 5.扫描周围蓝 ...
- The POSIX API/nss/nscd
https://code.google.com/p/nsscache/wiki/BackgroundOnNameServiceSwitch The POSIX API POSIX is a stand ...
- Java入门:基础算法之线性搜索
本程序使用线性搜索算法从n个数中查找一个数. /* Program: 线性搜索示例 * @author: 理工云课堂 * Input: 元素个数,每个元素值,待查找数据的值 * Output:待查找数 ...