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 ...
随机推荐
- 在Ubuntu18.04下配置hadoop集群
服务器准备 启动hadoop最小集群的典型配置是3台服务器, 一台作为Master, NameNode, 两台作为Slave, DataNode. 操作系统使用的Ubuntu18.04 Server, ...
- (原)visual studio 2015中添加dll路径
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9922033.html 使用vs2015调用opencv 3.4时,除了需要在“VC++目录”中”包含 ...
- CoreGraphics之CGContextSaveGState与UIGraphicsPushContext
转至:https://www.jianshu.com/p/be38212c0f79 CoreGraphics与UIKit 这边从iOS绘图教程 提取一些重要的内容. Core Graphics Fra ...
- redis学习 (key)键,Python操作redis 键 (二)
# -*- coding: utf-8 -*- import redis #这个redis 连接不能用,请根据自己的需要修改 r =redis.Redis(host=") 1. delete ...
- Spring Boot系列——日志配置
日志,通常不会在需求阶段作为一个功能单独提出来,也不会在产品方案中看到它的细节.但是,这丝毫不影响它在任何一个系统中的重要的地位. 为了保证服务的高可用,发现问题一定要即使,解决问题一定要迅速,所以生 ...
- CSS-2
day 39 学习链接:https://www.cnblogs.com/yuanchenqi/articles/5977825.html 4 文本属性 font-size: 10px; text-a ...
- XSS跨站脚本小结(转)
原文链接:http://www.cnblogs.com/xiaozi/p/5588099.html#undefined XSS漏洞验证经常遇到一些过滤,如何进行有效验证和绕过过滤呢,这里小结一下常见的 ...
- 通过JS页面唤醒app(安卓+ios)
var browser = { versions: function () { var u = navigator.userAgent, app = navigator.appVersion; ret ...
- linux下以‘-’开头的文件名
linux下以‘-’开头的文件名,cp.mv.rm.ls等对他都是无效的: [root@ha131 ~]# ll -plat.py ls:无效选项 -- . 请尝试执行"ls --help& ...
- 【WPF】ImageMagick调节图片的颜色
需求:打开一张图片后,自由调节图片的颜色(色调). 思路:读取显示一张图片后,用ColorPicker取色器选择一种颜色,之后将图片的色调调节为该颜色. 工具: 1.图像工具 ImageMagick( ...