Codeforces Round #319 (Div. 2) D
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的更多相关文章
- 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) B. Invariance of Tree 构造
B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...
- 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坐标 ...
- Codeforces Round #319 (Div. 2) D - Invariance of Tree
Invariance of Tree 题目大意:给你一个有1-n组成的序列p,让你构造一棵树,如果节点a和b之间有一条边,则p[a]和p[b]之间也有一条边. 思路:没啥思路,看了题解菜爆. 我们可以 ...
随机推荐
- CentOS安装HBase
1.下载HBASE http://www.apache.org/dyn/closer.cgi/hbase/ 2.解压文件到安装目录 #mkdir hbase #cd hbase #tar -zxvf ...
- caffe 测试时间报错 Aborted at unix time
今天测试时间报错,具体如下图: 在网上查了一下,大概的原因是由于程序中使用了随机函数造成的,后来检查了一下prototxt中有可能含有随机数的地方,去掉之后就可以了,包括shuffle:true,以及 ...
- wps去广告
彻底解决WPS弹出热点广告.WPS购物图标的办法 方法一:(一定有效) https://www.cnblogs.com/ytaozhao/p/5654149.html 一直用WPS,但一直有一个问题迟 ...
- python-面向对象-12_模块和包
模块和包 目标 模块 包 发布模块 01. 模块 1.1 模块的概念 模块是 Python 程序架构的一个核心概念 每一个以扩展名 py 结尾的 Python 源代码文件都是一个 模块 模块名 同样也 ...
- (1.16)mysql server优化之buffer pool
(1.16)mysql server优化之buffer pool 1.innodb buffer pool 查看 show status like 'Innodb_buffer_pool_%'; 该 ...
- 前端 HTML form表单标签 textarea标签 多行文本
<textarea></textarea>作用:允许用户录入多行数据到表单控件中 <!DOCTYPE html> <html lang="en&qu ...
- MySQL数据库查询操作进阶——多表查询
多表查询 在大部分情况下,我们用到的表都是彼此相关联的,所以我们会有相当大的需求用到跨表的查询,这个时候我们就需要将相关联的表连起来做多表查询. 多表查询分为连表查询和子查询,连表查询即将相关联的表连 ...
- Cglib动态代理实现原理
Cglib动态代理实现方式 我们先通过一个demo看一下Cglib是如何实现动态代理的. 首先定义个服务类,有两个方法并且其中一个方法用final来修饰. public class PersonSer ...
- Lua 随机数生成问题
原文链接:http://blog.csdn.net/zhangxaochen/article/details/8095007 Lua 生成随机数需要用到两个函数: math.randomseed(xx ...
- 【Java】-NO.16.EBook.4.Java.1.012-【疯狂Java讲义第3版 李刚】- Swing
1.0.0 Summary Tittle:[Java]-NO.16.EBook.4.Java.1.011-[疯狂Java讲义第3版 李刚]- Swing Style:EBook Series:Jav ...