E

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.

题意说的是给了一个数列p1p2... pn 组成的数是1到n。然后让你构造一棵N个点的树要保证树中 u和v存在路径, 那么在这颗树种pu,和pv也必须存在路径

想法:   如果pv pu 有连线 那么P[pv] P[pu]也要有联系,我们发现这样会是一个循环,这样我们就可以知道,在同一个循环内除了 长度为1 或者2的可以自己和自己连接,其他都必须和1 或者2连接

如果最小的一个循环节大小为1的循环节,那么就有解,你可以让他去连接除了他自己之外的任意一个循环节,这样算算边完全是n-1条

如果最小的一个循环节为2的那么其他的存在循环节的话必须为2的倍数,你可以画一下他们只要不是倍数关系,可定乱套了。

如果最小的一个循环节大于2肯定无解,因为他要和自己连都已经形成环了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
const int maxn=;
struct edg{
int a,b;
edg(int ca=,int cb=){
if(ca>cb)swap(ca,cb);
a=ca; b=cb;
}
bool operator == (const edg &rhs)const{
return a==rhs.a&&b==rhs.b;
}
bool operator <(const edg &rhs)const {
return a<rhs.a||(a==rhs.a&&b<rhs.b);
}
};
vector<int>G[maxn];
int A[maxn],n;
bool use[maxn];
set<edg>Q;
void bfs(int root, int to)
{
while(true){
edg e=edg(root,to);
if(Q.count(e))return ;
else Q.insert(e);
root=A[root];
to=A[to];
}
}
void solve1(){
int root=G[][];
for(int i=; i<G[].size(); i++)
{
edg a=edg(root,G[][i]);
Q.insert(a);
}
for(int i=; i<=n; i++)
{
int siz=G[i].size();
for(int j=; j<siz; j++)
{
int to=G[i][j];
bfs(root,to);
}
}
}
void solve2()
{
int root1=G[][];
int root2=A[root1];
edg e=edg(root1,root2);
Q.insert(e);
for(int i=; i<G[].size(); i++)
bfs(root1,G[][i]);
for(int i=; i<=n; i++)
{
int siz=G[i].size();
for(int j=; j<siz; j++)
{
int to=G[i][j];
bfs(root1,to);
}
}
}
int main()
{ scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&A[i]);
for(int i=; i<=n; i++)
{
if(use[i])continue;
int siz=,L=A[i];
while(use[L]==false){
use[L]=true;
siz++;
L=A[L];
}
G[siz].push_back(i);
}
if(G[].size()==&&G[].size()==){
puts("NO"); return ;
}
if(G[].size())solve1();
else {
for(int i=; i<=n; i++)if(G[i].size()){
if(i%){
puts("NO");return ;
}
}
solve2();
}
puts("YES");
set<edg>::iterator it;
for(it = Q.begin() ; it!=Q.end(); ++it)
{
edg e = *it;
printf("%d %d\n",e.a,e.b);
}
return ;
}

Codeforces Round #319 (Div. 2) D的更多相关文章

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

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

  2. 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/ ...

  3. 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 ...

  4. 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/ ...

  5. 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/ ...

  6. 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 ...

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

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

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

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

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

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

随机推荐

  1. 转:servlet的url-pattern匹配规则详细描述

    原文地址:servlet的url-pattern匹配规则详细描述   原文写的很详细 另外可以参考一下:Web.xml中设置Servlet和Filter时的url-pattern匹配规则 一.概述 在 ...

  2. 20165336 2017-2018-2 《Java程序设计》第5周学习总结

    20165336 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 内部类的类体中不可以声明类变量和类方法. 内部类仅供他的外嵌类使用,其他类不可以用某个类的内 ...

  3. 洛谷P3354 Riv河流 [IOI2005] 树型dp

    正解:树型dp 解题报告: 传送门! 简要题意:有棵树,每个节点有个权值w,要求选k个节点,最大化∑dis*w,其中如果某个节点到根的路径上选了别的节点,dis指的是到达那个节点的距离 首先这个一看就 ...

  4. Linux内核如何装载和启动一个可执行程序(转)

    原文:http://www.cnblogs.com/petede/p/5351696.html 实验七:Linux内核如何装载和启动一个可执行程序 姓名:李冬辉 学号:20133201 注: 原创作品 ...

  5. Github上Laravel开源排行榜Star数61-90名

    Github上Laravel开源排行榜Star数61-90名,罗列所有 Laravel 开源扩展包,含 Github Star 数量,下载数量和项目简介.默认排序是按Star数量从多到少来排 61.c ...

  6. what's the 回抽

    出自 MBA智库百科(https://wiki.mbalib.com/) 什么是回抽 由于近年来证券投资领域中技术分析的不断普及,许多投资者都认识到了股价(汇价)一旦突破颈线压力,后市看好.但是还有一 ...

  7. 002-pro ant design-Unexpected end of JSON input while parsing near '...错误解决方案

    解决方法:先清除缓存,再重新安装 清除缓存 npm cache clean --force 重新安装 npm install

  8. vue中根据当前时间进行排序

    computed: { newdataList: function() { return this.sortKey(this.dataList, "addtime"); } }, ...

  9. Flappy Bird

    在网上学习了下“65行 JavaScript 代码实现 Flappy Bird 游戏”(http://blog.jobbole.com/61842/),main.js 如下: // Initializ ...

  10. [OpenCV]代码整理

    开发环境:Windows7, VS2010, OpenCV2.4.10 1.图像特征匹配 // AxFeatureExtract.cpp : 定义控制台应用程序的入口点. // #include &q ...