CF576B.Invariance of Tree

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

A tree of size nn is an undirected connected graph consisting of nn vertices without cycles.

Consider some tree with nn vertices. We call a tree invariant relative to permutation p=p1p2... pnp=p_{1}p_{2}...\ p_{n} , if for any two vertices of the tree uu and vv the condition holds: "vertices uu and vv are connected by an edge if and only if vertices pup_{u} and pvp_{v} are connected by an edge".

You are given permutation pp of size nn . Find some tree size nn , invariant relative to the given permutation.

输入格式

The first line contains number nn ( 1<=n<=1051<=n<=10^{5} ) — the size of the permutation (also equal to the size of the sought tree).

The second line contains permutation pip_{i} ( 1<=pi<=n1<=p_{i}<=n ).

输出格式

If the sought tree does not exist, print "NO" (without the quotes).

Otherwise, print "YES", and then print n1n-1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter.

If there are multiple solutions, output any of them.

输入输出样例

  • 输入#1

    4
    4 3 2 1
    

    输出#1

    YES
    4 1
    4 2
    1 3
    
  • 输入#2

    3
    3 1 2
    

    输出#2

    NO
    

说明/提示

In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.

It can be shown that in the second sample test no tree satisfies the given condition.

首页