Content

给定无向图的 \(n\) 个点的父亲节点,求无向图的联通块个数。

数据范围:\(1\leqslant n\leqslant 10^4\)。

Solution

并查集模板题。

我们将在当前节点和它的父亲节点连在一起,然后看不同的祖先节点的个数即可。

没学过并查集的同学建议先去做 P3367 【模板】并查集

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std; int n, a[10007], f[10007], vis[10007], ans; int getfa(int x) {
return (x == f[x]) ? x : f[x] = getfa(f[x]);
}
void unionn(int x, int y) {
x = getfa(x), y = getfa(y);
if(x != y) f[x] = y;
} int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) f[i] = i;
for(int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
unionn(x, i);
}
for(int i = 1; i <= n; ++i)
if(!vis[getfa(i)]) ans++, vis[getfa(i)] = 1;
printf("%d", ans);
return 0;
}

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

  1. codeforces 755C. PolandBall and Forest

    C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. 【codeforces 755C】PolandBall and Forest

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. PolandBall and Forest

    PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirect ...

  4. CodeForces 755C PolandBall and Forest (并查集)

    题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...

  5. Codeforces 755C:PolandBall and Forest(并查集)

    http://codeforces.com/problemset/problem/755/C 题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相 ...

  6. CodeForces - 755C PolandBall and Forest (并查集)

    题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因 ...

  7. 某5道CF水题

    1.PolandBall and Hypothesis 题面在这里! 大意就是让你找一个m使得n*m+1是一个合数. 首先对于1和2可以特判,是1输出3,是2输出4. 然后对于其他所有的n,我们都可以 ...

  8. 算法与数据结构基础 - 哈希表(Hash Table)

    Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...

  9. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

随机推荐

  1. javascript-初级-day04js数据类型

    day01-js数据类型 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type& ...

  2. ASP .Net Core 在 CentOS8 ARM 下连接 SQL Server 2008 R2(Hypervisor)

    本文主要记录在 ARM 系统下无法连接SQL Server 2008 R2 的解决过程. 解决方案是使用 ODBC 的方式连接数据库,进行操作. 手上有公司的华为鲲鹏云计算 ARM 架构的 CentO ...

  3. 【POJ1961 Period】【KMP】

    题面 一个字符串的前缀是从第一个字符开始的连续若干个字符,例如"abaab"共有5个前缀,分别是a, ab, aba, abaa, abaab. 我们希望知道一个N位字符串S的前缀 ...

  4. Codeforces Round #717 (Div.2) 题解

    我 AK 的第二场(?)的 Div.2,还捡了个 rk4(虽然我 div2 only 的最高记录是 rk2)祭之( A 这题我竟然 WA 了两发,丢人( 直接贪心,对于 \(i=1,2,\cdots, ...

  5. python3 excel读、写、修改操作

    python3上Excel文件操作的库比较多,新手一开始不知道如何选择合适的库,故整理如下: xlwt: 只能写不能读,只支持python2.3到python2.7版本,只支持xls文件. xlrd ...

  6. nginx_location

    Nginx location 配置语法 1. location [ = | ~ | ~* | ^~ ] uri { ... } 2. location @name { ... } location 配 ...

  7. Linux— file命令 用于辨识文件类型

    Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...

  8. Excel-姓名列中同一个人汇总金额列,得出总金额

    8.姓名列中同一个人求和金额列,得出总金额. 方法一: P2处公式=SUMPRODUCT(($M$2:$M$20=$M2)*($N$2:$N$20)) 解释函数: 引用:https://zhinan. ...

  9. adblock plus-看下图你就懂

  10. 一劳永逸,使用 PicGo + GitHub 搭建个人图床工具

    原文链接: 一劳永逸,使用 PicGo + GitHub 搭建个人图床工具 经常写博客的同学都知道,有一个稳定又好用的图床是多么重要.我之前用过七牛云 + Mpic 和微博图床,但总感觉配置起来比较麻 ...