题意:给定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>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int fa[MAXN];
int a[MAXN];
set<int> s;
int Find(int v){
return fa[v] = (fa[v] == v) ? v : Find(fa[v]);
}
int main(){
for(int i = ; i < MAXN; ++i){
fa[i] = i;
}
int n;
scanf("%d", &n);
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
int tx = Find(i);
int ty = Find(a[i]);
if(tx < ty) fa[ty] = tx;
else fa[tx] = ty;
}
for(int i = ; i <= n; ++i){
if(Find(i) == i) s.insert(i);
if(Find(a[i]) == a[i]) s.insert(a[i]);
}
printf("%d\n", s.size());
return ;
}

CodeForces - 755C PolandBall and Forest (并查集)的更多相关文章

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

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

  2. codeforces 755C. PolandBall and Forest

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

  3. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  4. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  5. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  6. Codeforces 1027F Session in BSU - 并查集

    题目传送门 传送门I 传送门II 传送门III 题目大意 有$n​$门科目有考试,第$i​$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)​$,要求每门科目至少参加 ...

  7. CodeForces - 455C Civilization (dfs+并查集)

    http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...

  8. Codeforces 859E Desk Disorder:并查集【两个属性二选一】

    题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的 ...

  9. Codeforces 651E Table Compression【并查集】

    题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...

随机推荐

  1. vmware 因误删Linux 虚拟机磁盘,无法启动处理方法

    有可能我们在做了以下误操作,导致Linux系统无法启动: 1). 磁盘损坏或虚拟机磁盘被我们删除了,而fstab文件没有更新: 2). 由于误操作或其它原因使动态库错误. 1. 首先准备好系统安装盘, ...

  2. 实用类-<Math类常用>

    Math.random() //取0~1之间的随机数(不包括1) Math.max(数字1,数字2) //取两个数中最大的一个 Math.min(数字1,数字2) //取两个数中最小的一个 Math. ...

  3. [Android实例] Android网络收音机项目(内含源码)

    ======================帖子内容===================================最近喜欢听广播,但是搜索了一下,苦于网上没有Android的网络收音机项目的例 ...

  4. Guava LoadingCache不能缓存null值

    测试的时候发现项目中的LoadingCache没有刷新,但是明明调用了refresh方法了.后来发现LoadingCache是不支持缓存null值的,如果load回调方法返回null,则在get的时候 ...

  5. MyEclipse JSP页面中文字符不能保存

      问题: 就是写HTML+JSP代码时有些中文,保存时提示sava could not be completed. Reason: some characters cannot be mapped ...

  6. pytorch神经网络解决回归问题(非常易懂)

    对于pytorch的深度学习框架,在建立人工神经网络时整体的步骤主要有以下四步: 1.载入原始数据 2.构建具体神经网络 3.进行数据的训练 4.数据测试和验证 pytorch神经网络的数据载入,以M ...

  7. Laradock 下安装Beast扩展

    laradock/php-fpm/Dockfile ########################################################################## ...

  8. POJ 1166:The Clocks

    The Clocks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15357   Accepted: 6230 Descr ...

  9. emWin 模拟器环境搭建

    转载http://www.nxpic.org/module/forum/thread-609329-1-1.html 这个模拟器工程在Segger官网下载:https://www.segger.com ...

  10. Unity3d fbx纹理不显示 原因

    Unity3d 导入fbx文件后纹理不显示(3ds Max中显示正常) 原因: 1.纹理图片没有导入fbx同一文件夹中 2.纹理图片没有在fbx文件之前导入(现导入纹理图片,再导入fbx文件)