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 in each vertex of each tree, they have unique ids from 1 to n. For each Ball i we know the id of its most distant relative living on the same tree. If there are several such vertices, we only know the value of the one with smallest id among those.

How many trees are there in the forest?

Input

The first line contains single integer n (1 ≤ n ≤ 104) — the number of Balls living in the forest.

The second line contains a sequence p1, p2, ..., pn of length n, where (1 ≤ pi ≤ n) holds and pi denotes the most distant from Ball i relative living on the same tree. If there are several most distant relatives living on the same tree, pi is the id of one with the smallest id.

It's guaranteed that the sequence p corresponds to some valid forest.

Hacking: To hack someone, you should provide a correct forest as a test. The sequence p will be calculated according to the forest and given to the solution you try to hack as input. Use the following format:

In the first line, output the integer n (1 ≤ n ≤ 104) — the number of Balls and the integer m (0 ≤ m < n) — the total number of edges in the forest. Then m lines should follow. The i-th of them should contain two integers ai and bi and represent an edge between vertices in which relatives ai and bi live. For example, the first sample is written as follows:

5 3
1 2
3 4
4 5
Output

You should output the number of trees in the forest where PolandBall lives.

Interaction

From the technical side, this problem is interactive. However, it should not affect you (except hacking) since there is no interaction.

Examples
input

Copy
5
2 1 5 3 3
output

Copy
2
input

Copy
1
1
output

Copy
1
Note

In the first sample testcase, possible forest is: 1-2 3-4-5.

There are 2 trees overall.

In the second sample testcase, the only possible graph is one vertex and no edges. Therefore, there is only one tree.

并查集模版?

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
//#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[][] = { {,},{,-},{-,},{,} };
const long long inf = 0x7f7f7f7f7f7f7f7f;
const int INT = 0x3f3f3f3f;
int n;
vector<int> a;
int finds(int x)
{
return x == a[x] ? x : finds(a[x]);
}
int main()
{
int n;
cin >> n;
a.resize(n + );
for (int i = ; i <= n; i++)
{
a[i] = i;
}
for (int i = ; i <= n; i++)
{
int m, t1, t2;
cin >> m;
t1 = finds(i);
t2 = finds(m);
if (t2 != t1)
a[t2] = t1;
}
int ans = ;
for (int i = ; i <= n; i++)
{
if (i == a[i])
ans++;
}
cout << ans;
return ;
}

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. CodeForces 755C PolandBall and Forest (并查集)

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

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

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

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

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

  6. CF755C PolandBall and Forest 题解

    Content 给定无向图的 \(n\) 个点的父亲节点,求无向图的联通块个数. 数据范围:\(1\leqslant n\leqslant 10^4\). Solution 并查集模板题. 我们将在当 ...

  7. 某5道CF水题

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

  8. 基于netty轻量的高性能分布式RPC服务框架forest<下篇>

    基于netty轻量的高性能分布式RPC服务框架forest<上篇> 文章已经简单介绍了forest的快速入门,本文旨在介绍forest用户指南. 基本介绍 Forest是一套基于java开 ...

  9. 基于netty轻量的高性能分布式RPC服务框架forest<上篇>

    工作几年,用过不不少RPC框架,也算是读过一些RPC源码.之前也撸过几次RPC框架,但是不断的被自己否定,最近终于又撸了一个,希望能够不断迭代出自己喜欢的样子. 顺便也记录一下撸RPC的过程,一来作为 ...

随机推荐

  1. Travase Objects and four method of array

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Vue组件库读取自定义配置文件

    有这样一个场景,在组件库中新增一个配置文件,后续只修改一下配置文件中的配置项就可以实现不同的需求,下面就让我们I一起来实现吧. (一)在在项目的根目录(package.json所在目录)中创建一个vu ...

  3. C#简单的LogHelper

    适用于不想使用log4net等第三方的Log工具的LogHelper.正规的还是要使用<C# 工具类LogHelper>的这种做法. using System; using System. ...

  4. “石家庄铁道大学软件工程系学生学籍管理系统2019版”java小程序制作分享

    首先附上完整代码: import java.util.Scanner; public class SocreInformation { public SocreInformation(){}; pub ...

  5. Tensor的合并与分割

    先来看一下有哪些接口用来进行张量的合并与分割: tf.concat用来进行张量的拼接,tf.stack用来进行张量的堆叠,tf.split用来进行张量的分割,tf.unstack是tf.split的一 ...

  6. 网络共享服务(一)之FTP

    网络共享服务:ftp,nfs,samba比较 从跨平台角度说, samba, ftp差不多, 而nfs不支持windows平台 从挂载角度说, samba, nfs可以把远程目录挂载到本地目录上, 对 ...

  7. [PAT] A1018 Public Bike Management

    [思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...

  8. 线段树(lazy标记)-- 模板

    ], lazy[MAXN << ]; void PushUp(int rt) { ans[rt] = ans[rt << ] + ans[rt << | ]; } ...

  9. 第一个,net core项目,一起入门 !!!

    最近项目上开始使用.net core,新的项目,熟悉的东西比较多,现在花点时间来梳理一下,重头开始搭建一个.net core项目.哈哈,这个相对老手来说,估计会觉得小儿科,没事,也就当一次分享总结罢了 ...

  10. 梯度下降算法&线性回归算法

    **机器学习的过程说白了就是让我们编写一个函数使得costfunction最小,并且此时的参数值就是最佳参数值. 定义 假设存在一个代价函数 fun:\(J\left(\theta_{0}, \the ...