Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造
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 构造的更多相关文章
- Codeforces Round #319 (Div. 2) D - Invariance of Tree
Invariance of Tree 题目大意:给你一个有1-n组成的序列p,让你构造一棵树,如果节点a和b之间有一条边,则p[a]和p[b]之间也有一条边. 思路:没啥思路,看了题解菜爆. 我们可以 ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round 319 # div.1 & 2 解题报告
Div. 2 Multiplication Table (577A) 题意: 给定n行n列的方阵,第i行第j列的数就是i*j,问有多少个格子上的数恰为x. 1<=n<=10^5, 1< ...
- 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 ...
- 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/ ...
- 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/ ...
- 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 ...
- 构造+分块思想 Codeforces Round #319 (Div. 1) C
http://codeforces.com/contest/576/problem/C 题目大意: 给你一个曼哈顿距离的图,然后要求你找到一个链,链穿了所有的点 然后要求这链的长度<=25*10 ...
- Codeforces Round #319 (Div. 2) E - Points on Plane
题目大意:在一个平面里有n个点,点坐标的值在1-1e6之间,让你给出一个遍历所有点的顺序,要求每个点走一次,且 曼哈顿距离之和小于25*1e8. 思路:想了一会就有了思路,我们可以把1e6的x,y坐标 ...
随机推荐
- bash把所有屏幕输出重定向到文件并保持屏幕输出的方法
输出到文件log中,并在屏幕上显示:#ls >&1 | tee log 追加输出到文件log中,并在屏幕上显示:#ls >&1 | tee -a log
- 1470. UFOs(三维树状数组)
1470 最简单的三维树状数组 #include <iostream> #include<cstdio> #include<cstring> #include< ...
- 3月下旬剩余poj题解
poj1700 数学推导+简单dp poj2390 水题不说什么了 poj3260 先对找的钱做完全背包,在对能付的钱做多重背包,注意这道题能付的钱数的上界 poj2516 裸的最小费用最大流了没什么 ...
- HHVM 是如何提升 PHP 性能的?
背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...
- RPi 2B apache2 mysql php5 and vsftp
/************************************************************************* * RPi 2B apache2 mysql ph ...
- HDU 2289 Cup
Cup Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 如何安装mysql-5.5.29-win32.zip
1.windows下安装mysql-5.5.29-win32.zip: 1.解压缩,比如到d:\,为了方便,改一下路径,比如mysql2.开始/运行,输入cmd,回车进入命令行d:cd mysql\b ...
- HDU 5965 Gym Class 贪心+toposort
分析:就是给一些拓补关系,然后求最大分数,所以贪心,大的越靠前越好,小的越靠后越好 剩下的就是toposort,当然由于贪心,所以使用优先队列 #include <iostream> #i ...
- 设计模式_Observer_观察者模式
形象例子: 想知道咱们公司最新MM情报吗?加入公司的MM情报邮件组就行了,tom负责搜集情报,他发现的新情报不用一个一个通知我们,直接发布给邮件组,我们作为订阅者(观察者)就可以及时收到情报啦.观察者 ...
- Colors
.custom_a1, .custom_a2, .custom_a3, .custom_a4{ width:800px; height:100px; line-height:100px; color: ...