题目链接:https://codeforces.com/contest/1090/problem/D

Vasya had an array of n integers, each element of the array was from 1 to n. He chose m pairs of different positions and wrote them down to a sheet of paper. Then Vasya compared the elements at these positions, and wrote down the results of the comparisons to another sheet of paper. For each pair he wrote either "greater", "less", or "equal".

After several years, he has found the first sheet of paper, but he couldn't find the second one. Also he doesn't remember the array he had. In particular, he doesn't remember if the array had equal elements. He has told this sad story to his informatics teacher Dr Helen.

She told him that it could be the case that even if Vasya finds his second sheet, he would still not be able to find out whether the array had two equal elements.

Now Vasya wants to find two arrays of integers, each of length n. All elements of the first array must be distinct, and there must be two equal elements in the second array. For each pair of positions Vasya wrote at the first sheet of paper, the result of the comparison must be the same for the corresponding elements of the first array, and the corresponding elements of the second array.

Help Vasya find two such arrays of length n, or find out that there are no such arrays for his sets of pairs.

Input
The first line of input contains two integers n, m — the number of elements in the array and number of comparisons made by Vasya (1≤n≤100000, 0≤m≤100000).

Each of the following m lines contains two integers ai, bi — the positions of the i-th comparison (1≤ai,bi≤n; ai≠bi). It's guaranteed that any unordered pair is given in the input at most once.

Output
The first line of output must contain "YES" if there exist two arrays, such that the results of comparisons would be the same, and all numbers in the first one are distinct, and the second one contains two equal numbers. Otherwise it must contain "NO".

If the arrays exist, the second line must contain the array of distinct integers, the third line must contain the array, that contains at least one pair of equal elements. Elements of the arrays must be integers from 1 to n.

Examples
input
1 0
output
NO
input
3 1
1 2
output
YES
1 3 2
1 3 1
input
4 3
1 2
1 3
2 4
output
YES
1 3 4 2
1 3 4 1

题意:

给定 $n,m$,给定 $m$ 个无序对 $(a,b)$ 代表位置 $a$ 上的数字和位置 $b$ 上的数字进行比较。且这 $m$ 个无序对无重复。

让你寻找两个序列,第一个序列由 $1 \sim n$ 组成,元素互不相同且唯一。第二个序列,要满足和第一个序列对于 $m$ 个比较的结果相同,且某一个数字要出现两次,其余则皆属于 $[1,n]$ 且互不相同且唯一。

题解:

可以想见,如果有 $\frac{n(n-1)}{2}$ 次比较,那么就可以唯一确定该序列,则要输出 "NO" 。

一旦少那么一次,就代表有两个位置上的数字没有比较,不妨令这两个位置上的数字原本是最大和次大,现在全变成最大,这样不会改变 $m$ 次比较的结果。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e5+;
int n,m;
pii p[maxn];
pii findpos()
{
int a,b,c=;
pii res;
for(a=;a<=n;a++) for(b=a+;b<=n;b++) if((res=make_pair(a,b))!=p[++c]) return res;
}
int main()
{
cin>>n>>m;
for(int i=,a,b;i<=m;i++)
{
scanf("%d%d",&a,&b);
p[i]=make_pair(min(a,b),max(a,b));
}
if((ll)n*(n-)/ <= (ll)m) {printf("NO\n");return ;} printf("YES\n");
sort(p+,p++m);
pii pos=findpos();
for(int i=,cnt=;i<=n;i++)
{
if(i==pos.first) printf("%d ",n-);
else if(i==pos.second) printf("%d ",n);
else printf("%d ",++cnt);
}
printf("\n");
for(int i=,cnt=;i<=n;i++)
{
if(i==pos.first) printf("%d ",n);
else if(i==pos.second) printf("%d ",n);
else printf("%d ",++cnt);
}
printf("\n");
}

Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]的更多相关文章

  1. Codeforces 1090A - Company Merging - [签到水题][2018-2019 Russia Open High School Programming Contest Problem A]

    题目链接:https://codeforces.com/contest/1090/problem/A A conglomerate consists of n companies. To make m ...

  2. Codeforces 1090M - The Pleasant Walk - [签到水题][2018-2019 Russia Open High School Programming Contest Problem M]

    题目链接:https://codeforces.com/contest/1090/problem/M There are n houses along the road where Anya live ...

  3. Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]

    题目链接:https://codeforces.com/contest/1090/problem/B Examplesstandard input The most famous characters ...

  4. Codeforces 1491G - Switch and Flip(构造题)

    Codeforces 题目传送门 & 洛谷题目传送门 obviously,难度高一点的构造题对我来说都是不可做题 首先考虑将排列拆成一个个置换环,也就是 \(\forall i\) 连边 \( ...

  5. Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)

    传送门 Description Vitya is studying in the third grade. During the last math lesson all the pupils wro ...

  6. Codeforces 362D Fools and Foolproof Roads 构造题

    题目链接:点击打开链接 题意: 给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q 问是否可行,不可行输出NO 可行输出YES,并输出加入的p条边. set走起.. #incl ...

  7. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  8. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  9. Codeforces Gym100952 D. Time to go back-杨辉三角处理组合数 (2015 HIAST Collegiate Programming Contest)

    D. Time to go back   time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. 连接postgres特别消耗cpu资源而引发的PostgreSQL性能优化考虑

    由于是开发阶段,所以并没有配置postgres的参数,都是使用安装时的默认配置,以前运行也不见得有什么不正常,可是前几天我的cpu资源占用突然升高.查看进程,发现有一个postgres的进程占用CPU ...

  2. masstree Seastar

    masstree  Seastar 线程锁竞争和切换的开销几乎为0,代码也不用考虑多线程竞争,逻辑大大减化:此外Niagara是一个全异步执行引擎,采用了基于future,promise和contin ...

  3. 【iOS】ARC-MRC下的单例及其应用

    单例的应用十分普遍,单例模式使一个类仅仅有一个实例. *易于供外界訪问. *方便控制实例个数,节约系统资源. *OC中的常见单例: 如:UIApplication,  NSNotificationCe ...

  4. [转]epoll详解

    什么是epollepoll是什么?按照man手册的说法:是为处理大批量句柄而作了改进的poll.当然,这不是2.6内核才有的,它是在2.5.44内核中被引进的(epoll(4) is a new AP ...

  5. logrus日志使用详解

    1.logrus特点 golang标准库的日志框架很简单,logrus框架的特点: 1)完全兼容标准日志库 六种日志级别:debug, info, warn, error, fatal, panic ...

  6. Thymeleaf模版--子页面单独引入CSS、JS文件

    https://blog.csdn.net/u010392801/article/details/80465800 ****************************************** ...

  7. MySQL字符集详解

    Reference:  https://www.cnblogs.com/wcwen1990/p/6917109.html MySQL字符集详解   一.字符集和校验规则 字符集是一套符合和编码,校验规 ...

  8. JQuery DataTables Selected Row

    获取单行选中的值 $('#MonitoringTypeTable tbody').on('click', 'tr', function () { if ($(this).hasClass('selec ...

  9. linux下启动和关闭tomcat服务的方式

    Linux下tomcat服务的启动.关闭与错误跟踪,通常通过以下几种方式启动关闭tomcat服务: 切换到tomcat主目录下的bin目录 启动tomcat服务 生产模式: 方式一:直接启动 ./st ...

  10. android手机测试中如何查看内存泄露

    (一) 生成.hprof文件生成.hprof 文件的方法有很多,而且Android 的不同版本中生成.hprof 的方式也稍有差别,我使用的版本的是2.1,各个版本中生成.prof 文件的方法请参考: ...