Codeforces Round 319 (Div. 1)
B
Invariance of Tree
Codeforces
2100
原题
← AC →
翻译中…

A tree of size $*n*$ is an undirected connected graph consisting of $*n*$ vertices without cycles.

翻译中…

Consider some tree with $*n*$ vertices. We call a tree invariant relative to permutation $*p* = *p*_{1}*p*_{2}... *p*_{*n*}$, if for any two vertices of the tree $*u*$ and $*v*$ the condition holds: "vertices $*u*$ and $*v*$ are connected by an edge if and only if vertices $*p*_{*u*}$ and $*p*_{*v*}$ are connected by an edge".

翻译中…

You are given permutation $*p*$ of size $*n*$. Find some tree size $*n*$, invariant relative to the given permutation.

翻译中…

Input

翻译中…

The first line contains number $*n*$ ($1 ≤ *n* ≤ 10^{5}$) — the size of the permutation (also equal to the size of the sought tree).

翻译中…

The second line contains permutation $*p*_{*i*}$ ($1 ≤ *p*_{*i*} ≤ *n*$).

翻译中…

Output

翻译中…

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

翻译中…

Otherwise, print "YES", and then print $*n* - 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.

翻译中…

Note

翻译中…

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.

翻译中…

样例

Input #1
4
4 3 2 1
Output #1
YES
4 1
4 2
1 3
InputCopy OutputCopy
Input #2
3
3 1 2
Output #2
NO
InputCopy OutputCopy
题目信息
题号
576B
时间限制
1 s
内存限制
256 MB
标签
构造
DFS 及类似
贪心
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。