PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000017. PolandBall và khu rừng (PolandBall and Forest)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
PolandBall và khu rừng (PolandBall and Forest)
PolandBall and Forest
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A forest consists of several disjoint trees with a total of vertices numbered from to . For every vertex , you are given the vertex farthest from in the same tree. If several vertices attain the maximum distance, is the one with the smallest index.
Determine the number of trees from the sequence .
Input
The first line contains an integer . The next line contains integers . It is guaranteed that at least one forest satisfies the given information.
Output
Print the number of trees in the forest.
Subtasks
- Subtask 1 (20 points): ; for all ; the sequence describes a valid forest.
- Subtask 2 (30 points): ; for all ; the sequence describes a valid forest.
- Subtask 3 (50 points): ; for all ; the sequence describes a valid forest.
Examples
Input
5
2 1 5 3 3
Output
2
Explanation
The farthest-vertex array is consistent with components {1,2} and {3,4,5}, so there are two trees.