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. 转:Mysql float类型where 语句判断相等问题

    原文地址:https://www.2cto.com/database/201111/111983.html 原文内容如下: Mysql where 语句中有float 类型数据判断相等时,检索不出记录 ...

  2. Excel--数据透视图

    原文:https://ke.qq.com/course/289406 1.数据源注意项 2. 3.选中数据源操作 任意选中数据源表格中的单元格(有值得单元格),插入数据透视表 默认数据源区域就是整个表 ...

  3. tensorflow入门笔记(二) 滑动平均模型

    tensorflow提供的tf.train.ExponentialMovingAverage 类利用指数衰减维持变量的滑动平均. 当训练模型的时候,保持训练参数的滑动平均是非常有益的.评估时使用取平均 ...

  4. 防止atoi函数内存越界

    函数形式为: int atoi(const char *nptr);    函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非 ...

  5. WebDriver一些常见问题的解决方法

    1.Exception NoSuchElementException: 解决方法: 1)检查目标element的locator 2)如果locator是正确的,尝试在查找element之前等待页面的加 ...

  6. c语言数组应用

    #include <stdio.h> #define SIZE 5 int main(void) { int sum[3]={0},sum2[SIZE]={0},i,sum1=0; dou ...

  7. 前端 HTML body标签相关内容 常用标签 表单标签 form里面的 label标签介绍

    定义:<label> 标签为 input 元素定义标注(标记). label标签功能:关联input标签文本与表达元素,点击input标签文本时,如同点击表单元素一样. label标签是行 ...

  8. 20170724 Airflow官网资料学习

    -- 1  Apache Airflow 文档 AirFlow 对编程人员来讲就是一个平台,用于进行日程安排和监控.但是还在卵化期,严格来说,不是一个完整的成品.

  9. ASP.NET学习笔记(1)

    1.ASP.Net简介 A.ASP.Net动态网页技术.在服务器端运行.Net代码,动态生成HTML.在浏览器可以使用JavaScript.Dom完成前台工作.如存储数据.访问数据库.业务逻辑运算等可 ...

  10. 前端框架之Vue(8)-表单输入绑定

    基础用法 你可以用 v-model 指令在表单 <input> . <textarea> 及 <select> 元素上创建双向数据绑定.它会根据控件类型自动选取正确 ...