B. Invariance of Tree

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/576/problem/B

Description

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 = p1p2... pn, if for any two vertices of the tree u andv the condition holds: "vertices u and v are connected by an edge if and only if vertices pu and pv 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 ≤ 105) — the size of the permutation (also equal to the size of the sought tree).

The second line contains permutation pi (1 ≤ pi ≤ 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.

Sample Input

4
4 3 2 1

Sample Output

YES
4 1
4 2
1 3

HINT

题意

给你一些点,以及p[i],表示如果你i和j相连的话,那么p[i]和p[j]也必须相连

然后问你能不能构成一棵树~

如果能的话,请输出这棵树的样子

题解:

如果有i=p[i]的话,那么直接让其他所有点都连接这个点就好了

如果不存在二元环的话,那就直接输出NO,因为你随便连接一个环内的边,就会使得整个环都连边,就会错误

如果存在二元环,如果存在奇元环,那就直接输出NO,否则就让偶元环依次连过去就好了

大概第三类讨论画画图就能很轻松的证明了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000500
#define mod 1001
#define eps 1e-9
#define pi 3.1415926
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int p[maxn];
int vis[maxn];
int deal(int x,int c)
{
if(vis[x])
return c;
vis[x]=;
deal(p[x],c+);
}
void solve(int x,int c,int a,int b)
{
if(vis[x])return;
vis[x]=;
if(c%==)
printf("%d %d\n",x,a);
else
printf("%d %d\n",x,b);
solve(p[x],c+,a,b);
}
int main()
{
int n=read();
int flag = ;
for(int i=;i<=n;i++)
{
p[i]=read();
if(p[i]==i)flag=i;
}
if(flag)
{
printf("YES\n");
for(int i=;i<=n;i++)if(flag!=i)printf("%d %d\n",i,flag);
return ;
}
int flag1=;
for(int i=;i<=n;i++)
{
if(!vis[i])
{
int tmp = deal(i,);
if(tmp==)flag1=i;
if(tmp%==){printf("NO\n");return ;}
}
}
if(!flag1){printf("NO\n");return ;}
printf("YES\n");
memset(vis,,sizeof(vis));
vis[p[flag1]]=;
for(int i=;i<=n;i++)
if(!vis[i])
solve(i,,flag1,p[flag1]);
return ;
}

Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造的更多相关文章

  1. Codeforces Round #319 (Div. 2) D - Invariance of Tree

    Invariance of Tree 题目大意:给你一个有1-n组成的序列p,让你构造一棵树,如果节点a和b之间有一条边,则p[a]和p[b]之间也有一条边. 思路:没啥思路,看了题解菜爆. 我们可以 ...

  2. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  3. Codeforces Round 319 # div.1 & 2 解题报告

    Div. 2 Multiplication Table (577A) 题意: 给定n行n列的方阵,第i行第j列的数就是i*j,问有多少个格子上的数恰为x. 1<=n<=10^5, 1< ...

  4. Codeforces Round #319 (Div. 1) C. Points on Plane 分块

    C. Points on Plane Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/pro ...

  5. Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学

    C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  6. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  7. Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题

    A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...

  8. 构造+分块思想 Codeforces Round #319 (Div. 1) C

    http://codeforces.com/contest/576/problem/C 题目大意: 给你一个曼哈顿距离的图,然后要求你找到一个链,链穿了所有的点 然后要求这链的长度<=25*10 ...

  9. Codeforces Round #319 (Div. 2) E - Points on Plane

    题目大意:在一个平面里有n个点,点坐标的值在1-1e6之间,让你给出一个遍历所有点的顺序,要求每个点走一次,且 曼哈顿距离之和小于25*1e8. 思路:想了一会就有了思路,我们可以把1e6的x,y坐标 ...

随机推荐

  1. js不能执行,IE处理方法

    一.如果你的ie不能打开js脚本(连系统里所有的js文件都不运行,网页上的js广告或好多页面都显示不了),请按一下步骤进行排查与解决: 1.查看是否IE的安全里面禁止了JS的运行: 将工具=>i ...

  2. OK335xS Qt network hacking

    /********************************************************************** * OK335xS Qt network hacking ...

  3. RPi 2B GPIO 测试

    /************************************************************************************** * RPi 2B GPI ...

  4. YQL

    YQL,(Yahoo! Query Language)是一种支持对互联网上的数据进行查询.过滤.连接.类似SQL语法的简单语言.用YQL官方的话:有了YQL,开发人员只需要使用一种 简单的查询语言即可 ...

  5. CentOS 7 安装 mariaDB

    1.安装数据库 [root@localhost ~]# yum -y  install mariadb-server mariadb mariadb-devel 2.启动数据库[root@localh ...

  6. ClassLoader工作机制

    阅读目录 一.ClassLoader概念 二.JVM平台提供三层classLoader 三.JVM加载class文件到内存有两种方式 四.ClassLoader加载类的过程 五.自定义类加载器 六.实 ...

  7. JS保留两位小数 [转]

    js保留2位小数toFixed(xxxx) var a = 9.39393; alert(a.toFixed()); alert(Number.toFixed(9.39393)); 返回的是9. 对于 ...

  8. STL序列式容器

    1.vector      空间运用的灵活性.      实现技术——关键是对大小的控制以及重新配置时的数据移动效率.      配置新空间.数据移动.释还旧空间      erase(int pos ...

  9. Java中线程顺序执行

    现有线程threadone.threadtwo和threadthree,想要的运行顺序为threadone->threadtwo->threadthree,应该如何处理?这里需要用到一个简 ...

  10. 【idea】移动下载站

    硬件: 1.Mac或 Linux台 2.300M 无线TP-LINK TL-WR802N AP无密码,与 Mac 同一个网段,Mac开 rails 应用 扫一扫页面,手机下载.OK Mac 搭建ROR ...