Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]
题目链接: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]的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces 1491G - Switch and Flip(构造题)
Codeforces 题目传送门 & 洛谷题目传送门 obviously,难度高一点的构造题对我来说都是不可做题 首先考虑将排列拆成一个个置换环,也就是 \(\forall i\) 连边 \( ...
- 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 ...
- Codeforces 362D Fools and Foolproof Roads 构造题
题目链接:点击打开链接 题意: 给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q 问是否可行,不可行输出NO 可行输出YES,并输出加入的p条边. set走起.. #incl ...
- 集训第四周(高效算法设计)P题 (构造题)
Description There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...
- 集训第四周(高效算法设计)O题 (构造题)
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...
- 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 ...
随机推荐
- 转发:CentOS下tar压缩排除某个文件夹或文件及解压
一.压缩 一般直接用tar命令打包很简单,直接使用 tar -zcvf test.tar.gz test 即可. 在很多时候,我们要对某一个目录打包,而这个目录下有几十个子目录和子文件,我们需要在打 ...
- Faiss安装
一.上策:使用现成的faiss 找到别人(同事或同学)的python目录,找到faiss文件夹,复制到本地,并将其添加到PYTHONPATH下. 二.中策:使用anaconda anaconda上面的 ...
- OSI(Open System Interconnect) 网络七层协议
你作为用户想发个快递,你叫来了顺丰,顺丰快递员从你手里拿走了快递,又装进一个盒子,然后把一个快递单子贴在了上面. 快递员回到集散中心,将快递往那一扔不管了,分拣员把快递按投递的省市分开,发往同一地区的 ...
- 【原创 Hadoop&Spark 动手实践 9】Spark SQL 程序设计基础与动手实践(上)
[原创 Hadoop&Spark 动手实践 9]SparkSQL程序设计基础与动手实践(上) 目标: 1. 理解Spark SQL最基础的原理 2. 可以使用Spark SQL完成一些简单的数 ...
- Kafka Docker集群搭建
1. Zookeeper下载 http://apache.org/dist/zookeeper/ http://mirrors.hust.edu.cn/apache/zookeeper/zookeep ...
- [微信小程序] 认识微信小程序及开发环境搭建
微信公众平台首页 https://mp.weixin.qq.com 微信公众平台测试帐号系统 https://open.weixin.qq.com/connect/qrconnect?appid=wx ...
- Linux连接redis客户端出现Could not connect to Redis at 127.0.0.1:6379: Connection refused
打开两个窗口,一个执行 命令,另外一个窗口执行
- [springBoot系列]--springBoot注解大全[转]
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- js上传
有时候需要显示进度,这时候就需要做一些切割,具体上传代码如下: <!DOCTYPE HTML> <html lang="en-US"> <head&g ...
- xdebug php 运行效率分析工具
Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况. 官网:https://xdebug.org/index.php 安装方法: ht ...