Similar Arrays CF-1090D(构造)
题意:
给定 $n,m$,给定 $m$ 个无序对 $(a,b)$ 代表位置 $a$ 上的数字和位置 $b$ 上的数字进行比较。且这 $m$ 个无序对无重复。
让你寻找两个序列:
第一个序列由 $1 \sim n$ 组成,元素互不相同且唯一。
第二个序列,要满足和第一个序列对于 $m$ 个比较的结果相同,且某一个数字要出现两次,其余则皆属于 $[1,n]$ 且互不相同且唯一。
思路:
只需要找出两个没有直接关系的数字分别赋值成最大和次大。
代码:
1 //#include<bits/stdc++.h> 2 #include <set> 3 #include <map> 4 #include <stack> 5 #include <cmath> 6 #include <queue> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstring> 11 #include <iostream> 12 #include <algorithm> 13 14 #define ll long long 15 #define pll pair<ll,ll> 16 #define pii pair<int,int> 17 #define bug printf("*********\n") 18 #define FIN freopen("input.txt","r",stdin); 19 #define FON freopen("output.txt","w+",stdout); 20 #define IO ios::sync_with_stdio(false),cin.tie(0) 21 #define ls root<<1 22 #define rs root<<1|1 23 #define pb push_back 24 25 using namespace std; 26 const int inf = 2e9 + 7; 27 const ll Inf = 1e18 + 7; 28 const int maxn = 2e5 + 5; 29 const int mod = 1e9 + 7; 30 31 int n, m; 32 33 map<int, int>mp[maxn]; 34 int ans[maxn]; 35 36 int main() 37 { 38 scanf("%d %d", &n, &m); 39 while (m--) 40 { 41 int x, y; 42 scanf("%d %d", &x, &y); 43 mp[x][y] = 1; 44 mp[y][x] = 1; 45 } 46 for (int i = 1; i <= n; ++i) 47 { 48 for (int j = i + 1; j <= n; ++j) 49 { 50 if (!mp[i][j]) 51 { 52 cout << "YES" << endl; 53 int sta = 1; 54 ans[i] = n; 55 ans[j] = n - 1; 56 for (int k = 1; k <= n; ++k) 57 { 58 if (ans[k]) continue; 59 ans[k] = sta++; 60 } 61 for (int k = 1; k <= n; ++k) 62 { 63 if (k != 1) cout << " "; 64 cout << ans[k]; 65 } 66 cout << endl; 67 ans[j] = n; 68 for (int k = 1; k <= n; ++k) 69 { 70 if (k != 1) cout << " "; 71 cout << ans[k]; 72 } 73 cout << endl; 74 return 0; 75 } 76 } 77 } 78 cout << "NO" << endl; 79 }