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. Fiddler怎么可以抓取https的请求包

    对于https的协议是带有安全的功能,所有有一些的https的协议是无法抓到的,所以需要通过设置filler中来对,来使用filler的方式的来抓取到https的请求包,那么如何使用filler中抓取 ...

  2. LeetCode 812 Largest Triangle Area 解题报告

    题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...

  3. NPM升级

    nmp的更新可以使用自身指令即可: npm install npm -g 可以看到从3.10.10升级到了4.0.5 都说npm比node升级的快,现在比起来nodejs的更新速度更快 如果npm官方 ...

  4. c++ 如何获取多线程的返回值?(std::thread ,std::async)

    //简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...

  5. LEO原创-FMX之你不知道的ARC

    LEO原创13498714 FMX加入了ARC技术,对象创建后不用释放,FMX会帮你释放,是不是这样就不用关心对象的释放了呢,非也! 写简单的代码,这个功能也许很好用,但如果你写的是一个项目,那隐藏的 ...

  6. MongoDB pymongo模块 删除数据

    使用user集合,删除user集合的数据 import pymongo mongo_client = pymongo.MongoClient( host='192.168.0.112', port=2 ...

  7. socket 套接字总结

    简单版 服务端 import socket import struct import json import os server_dir = r'E:\Moudule_1\socket练习\serve ...

  8. 常见的local variable 'x' referenced before assignment问题

    def fun1(): x = 5 def fun2(): x *= 2 return x return fun2() 如上代码,调用fun1() 运行会出错:UnboundLocalError: l ...

  9. Mac搭建PHP+rabbitMQ环境

    RabbitMQ是一个在AMQP基础上实现的企业级消息系统.何谓消息系统,就是消息队列系统,消息队列是“”消费-生产者模型“”的一个典型的代表,一端往消息队列中不断写入消息,而另一端则可以读取或者订阅 ...

  10. Spark SQL读取Oracle的number类型的数据时精度丢失问题

    Spark SQL读取数据Oracle的数据时,发现number类型的字段在读取的时候精度丢失了,使用的spark版本是Spark2.1.0的版本,竟然最后经过排查和网上查资料发现是一个bug.在Sp ...