CF755C.PolandBall and Forest

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

PolandBall 和他的家人住在一片森林里。森林中有一些树。树是无向无环图,包含 kk 个顶点和 k−1k-1 条边,其中 kk 是某个整数。注意:单个顶点也构成一棵合法的树。

每棵树的每个顶点上恰好住着一位亲属,他们的编号从 11 到 nn,且互不相同。对于每个 Ball ii,我们已知与其同在一棵树上、距离它最远的那位亲属的编号;若存在多个这样的最远顶点,则我们仅知道其中编号最小的那个顶点的编号。

请问:这片森林中一共有多少棵树?

输入格式

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

The second line contains a sequence _p_1, _p_2, ..., p__n of length n, where (1 ≤ p__i ≤ n) holds and p__i 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, p__i 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 a__i and b__i and represent an edge between vertices in which relatives a__i and b__i live. For example, the first sample is written as follows:

5 3
1 2
3 4
4 5

第一行包含一个整数 nn(1≤n≤1041 \leq n \leq 10^4)——表示森林中 Ball 的数量。

第二行包含一个长度为 nn 的序列 p1,p2,…,pnp_1, p_2, \dots, p_n,其中满足 1≤pi≤n1 \leq p_i \leq n,且 pip_i 表示与 Ball ii 同在一棵树上、距离其最远的亲属。若存在多个与 Ball ii 同在一棵树上且距离其最远的亲属,则 pip_i 为其中编号最小的那个亲属的编号。

保证该序列 pp 对应某个合法的森林。

Hack 方法:要 hack 某个提交,你需要提供一个合法的森林作为测试用例。系统将根据该森林计算出序列 pp,并将其作为输入提供给被 hack 的程序。请使用如下格式:

第一行输出整数 nn(1≤n≤1041 \leq n \leq 10^4)——表示 Ball 的数量,以及整数 mm(0≤m<n0 \leq m < n)——表示该森林中边的总数。随后输出 mm 行,第 ii 行包含两个整数 aia_i 和 bib_i,表示亲属 aia_i 与 bib_i 所在顶点之间存在一条边。例如,第一个样例可表示为如下形式:

5 3
1 2
3 4
4 5

输出格式

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

你应该输出波兰球所生活的森林中树的数量。

输入输出样例

  • 输入#1

    5
    2 1 5 3 3

    输出#1

    2
  • 输入#2

    1
    1

    输出#2

    1

说明/提示

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.

在第一个样例测试用例中,可能的森林为:1-2 和 3-4-5。

总共有 2 棵树。

在第二个样例测试用例中,唯一可能的图是一个顶点且没有边。因此,只有一棵树。

输入解题思路,AI测评打分。不知道怎么写?

首页