CF755C PolandBall and Forest 题解】的更多相关文章

Content 给定无向图的 \(n\) 个点的父亲节点,求无向图的联通块个数. 数据范围:\(1\leqslant n\leqslant 10^4\). Solution 并查集模板题. 我们将在当前节点和它的父亲节点连在一起,然后看不同的祖先节点的个数即可. 没学过并查集的同学建议先去做 P3367 [模板]并查集. Code #include <cstdio> #include <algorithm> #include <cstring> #include <…
C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graph…
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 e…
PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 edges, where k is some integer. Note that one vertex is a valid tree. There is exactly one relative living…
题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include &…
http://codeforces.com/problemset/problem/755/C 题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相等则p[i]是 id 较小的点. 思路:一开始没什么想法,画几分钟图发现不到什么东西,后来想着 i 和 p[i] 有关系,那么就代表 i 和 p[i] 是属于同一棵树,那么不就是并查集了嘛.抱着试一试的心态搞了一下居然过了. #include <cstdio> #include <cstri…
题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因此连通块的个数就是树的个数.并查集即可. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib>…
1.PolandBall and Hypothesis 题面在这里! 大意就是让你找一个m使得n*m+1是一个合数. 首先对于1和2可以特判,是1输出3,是2输出4. 然后对于其他所有的n,我们都可以非常快的找到一个最小的与它互质的质数p(考虑反证法),并且满足p<n. 这样就相当与解一个同余方程 n*m = p-1 (mod p) , 解出的m可以保证 n*m+1 是 p 的倍数,也就是合数了. 又因为gcd(p,n)==1,所以这个方程肯定有解,直接求一个 n 在mod p意义下的逆元然后乘…
Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O(1). HashMap(std::unordered_map).HashSet(std::unordered_set)的原理与Hash Table一样,它们的用途广泛.用法灵活,接下来侧重于介绍它们的应用. 相关LeetCode题: 706. Design HashMap  题解  705. Des…
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以用递归.堆栈(stack)实现DFS过程. 关于广度优先搜索(BFS)详见:算法与数据结构基础 - 广度优先搜索(BFS) 关于递归(Recursion)详见:算法与数据结构基础 - 递归(Recursion) 树的遍历 DFS常用于二叉树的遍历,关于二叉树详见: 算法与数据结构基础 - 二叉查找树…