목차

주둔

ps
링크acmicpc.net/…
출처BOJ
문제 번호31414
문제명주둔
레벨플래티넘 5
분류

그래프

시간복잡도O(n)
인풋사이즈n<100,000
사용한 언어Python 3.11
제출기록41844KB / 92ms
최고기록92ms
해결날짜2024/02/21
출처

제3회 보라매컵 본선 Open Contest - F

풀이

코드

"""Solution code for "BOJ 31414. 주둔".

- Problem link: https://www.acmicpc.net/problem/31414
- Solution link: http://www.teferi.net/ps/problems/boj/31414

Tags: [graph]
"""


def main():
    N = int(input())
    d = [int(x) - 1 for x in input().split()]

    answer = N
    comp_no = [None] * N
    dist = [None] * N
    for source in range(N):
        if comp_no[source] is not None:
            continue
        cur = source
        for cur_dist in range(N + 1):
            if comp_no[cur] is not None:
                break
            comp_no[cur], dist[cur] = source, cur_dist
            cur = d[cur]
        if comp_no[cur] == source:
            length = cur_dist - dist[cur]
            if length == 2 or length % 2 == 1:
                answer -= 1

    print(answer)


if __name__ == '__main__':
    main()