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. MyEclipse中JDK运行环境和编译环境的设置

    一.设置myEclipse中新项目使用的JDK 1.运行环境   [Window]->[Preferences]->[Java]->[Installed JREs] 步骤:Add-- ...

  2. Python+Flash+NodeJS 接口自动化平台

    一.前端安装步骤# manager-web(1)下载项目 git clone https://github.com/t880216t/manager-web.git (2) 安装依赖 cnpm ins ...

  3. php 数值类型

    一.整形 1. 常见的整形 echo 1234; // 十进制数 echo -123; // 负数 echo 0123; // 八进制数 (等于十进制 83) echo 0x1A; // 十六进制数 ...

  4. 谈谈javascript数组排序方法sort()的使用,重点介绍参数使用及内部机制?

    语法:arrayObject.sort(sortby) 参数sortby可选,规定排序顺序,必须是函数: 注:如果调用该方法时没有使用参数,将按字符编码的顺序进行排序,要实现这一点,首先应把数组的元素 ...

  5. 基于jQuery实现的Ajax 验证用户名唯一性

    基于jQuery实现的Ajax 验证用户名唯一性 前端jsp页面代码 <tr> <th><span class="requiredField"> ...

  6. python-面向对象-01_面向对象(OOP)基本概念

    面向对象(OOP)基本概念 面向对象编程 —— Object Oriented Programming 简写 OOP 目标 了解 面向对象 基本概念 01. 面向对象基本概念 我们之前学习的编程方式就 ...

  7. RN九宫格

    九宫格可以用两种方式来做,一种使用SectionList,是我的另外一篇博客,还有一种的纯代码计算,下面是效果图 代码如下: var Dimensions = require('Dimensions' ...

  8. WINDOWS SERVER 2008 R2安装指南

    说明:适用于以下几种操作系统: 1.Windows Server 2008 Standard Endition R2 2.Windows Server 2008 Enterprise Endition ...

  9. 在golang中使用 cgo,如何让被嵌入的c语言代码调用golang

    https://golang.org/misc/cgo/test/callback.go // Copyright 2011 The Go Authors. All rights reserved. ...

  10. save

    docker-compose-orderer.yaml # Copyright IBM Corp. All Rights Reserved. # # SPDX-License-Identifier: ...