A10024 | Invariance of Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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$ ).
The second line contains permutation $p_{i}$ ( $1<=p_{i}<=n$ ).
输出格式
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.
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.
输入输出样例
输入 #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.
It can be shown that in the second sample test no tree satisfies the given condition.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted