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. 2434: [Noi2011]阿狸的打字机

    ac自动机,bit,dfs序. 本文所有的stl都是因为自己懒得实现.   首先x在y里面出现,就意味y节点可以顺着fail回去. 反向建出一个fail数,然后搞出dfs序列.找出x对应的区间有多少个 ...

  2. 8.3-8.7 usaco

    summary:38 vijos1002:青蛙跳河. dp+压缩.距离大于100可以直接%100.然后数据范围小了很多可以dp了. #include<cstdio> #include< ...

  3. 网站资料收集 主要查看js的学习部分

    1.Asp.Net MVC3.0基本的简单的可能都会用,更深入的使用还需加深研究,之后希望对MVC4.0和5.0进行对比学习,暂时看到@葡萄城控件技术团队博客的MVC5系列正在继续http://www ...

  4. 实现sqrt()函数

    求一个正数N的开方, 并且可以指定精度, 要求不能用库函数sqrt 方法一:如下所示,先求sqrt(N)的整数部分,再求小数点后1位,2位 ... ... 方法二:牛顿迭代法,根据公式 Ai+1 = ...

  5. (2015年郑州轻工业学院ACM校赛题) J 堆

    判断是否是一个堆,把树构造好遍历一遍就OK了 #include<stdio.h> #include<iostream> #include<stack> #inclu ...

  6. 提示29. 怎样避免延迟加载或Load()阅读器问题

    提示29. 怎样避免延迟加载或Load()阅读器问题 如果你有如下这样的代码: 1 var results = from c in ctx.Customers 2 where c.SalesPerso ...

  7. Gen_fsm行为实践与分析

    1.简介 Gen_fsm是一个通用的有限状态机,它描述了这样的一组关系: State(S) x Event(E) -> Actions(A),State(S') 这个关系意味着:如果在S状态下发 ...

  8. JMX学习一

    JMX        即 Java Management Extensions   Java管理扩展MBean   即 managed beans                         被管 ...

  9. 28、activity之间传递数据&批量传递数据

    import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...

  10. 【Tcpcopy】离线回放功能

    最近因调试问题,需要一直进行tcpcopy,拿有问题的包进行测试.决定使用tcpcopy对录制脚本进行回放,以下为我操作的具体步骤.主要是三块 1 下载安装具有离线回放功能的tcpcopy 2 使用t ...