Similar Arrays
Topic:
The topic is to construct two sequences
Each number in the A sequence is different, and only two identical numbers in the B sequence
For the M group relationship i, j means ai> aj or ai <aj
At the same time, sequence B has the same relationship as sequence A. If ai> aj, then bi> bj
Ideas:
First consider the situation that cannot be constructed. If any pair of numbers in the A sequence are related, then it is impossible to have two identical numbers in the B sequence
即 m >= (n - 1) * n / 2
If m <(n-1) * n / 2, find two unrelated points, sequence A is assigned n and n-1, sequence B is assigned n and n
The remaining A and B sequences are the same.
1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 #include<cmath> 5 #include<iostream> 6 #include<vector> 7 #include<queue> 8 #include<set> 9 #include<map> 10 #include<cctype> 11 #define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) 12 #define mem(a,x) memset(a,x,sizeof(a)) 13 #define lson rt<<1,l,mid 14 #define rson rt<<1|1,mid + 1,r 15 #define P pair<int,int> 16 #define ull unsigned long long 17 using namespace std; 18 typedef long long ll; 19 const int maxn = 2e5 + 10; 20 const ll mod = 998244353; 21 const int inf = 0x3f3f3f3f; 22 const long long INF = 0x3f3f3f3f3f3f3f3f; 23 const double eps = 1e-7; 24 inline ll read() 25 { 26 ll X = 0, w = 0; char ch = 0; 27 while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } 28 while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); 29 return w ? -X : X; 30 } 31 ll n, m , a , b; 32 vector<int>vec[maxn], v; 33 ll cnt[maxn], arr1[maxn] , arr2[maxn] , cnt1[maxn]; 34 35 36 37 int main() 38 { 39 n = read(), m = read(); 40 for (int i = 1; i <= m; ++i) 41 { 42 int u = read(), v = read(); 43 vec[u].push_back(v); 44 vec[v].push_back(u); 45 } 46 if (2 * m >= n * (n - 1)) 47 { 48 cout << "NO" << endl; 49 return 0; 50 } 51 int anspos; 52 for (int i = 1; i <= n; ++i) 53 { 54 if (vec[i].size() == n - 1) continue; 55 else 56 { 57 anspos = i; 58 for (int j = 0; j < vec[i].size(); ++j) cnt[vec[i][j]] = 1; 59 break; 60 } 61 } 62 for (int i = 1; i <= n; ++i) 63 { 64 if (cnt[i] == 0 && i != anspos) 65 { 66 arr2[anspos] = arr2[i] = n; 67 arr1[anspos] = n, arr1[i] = n - 1; 68 break; 69 } 70 } 71 int res = 0; 72 for (int i = 1; i <= n; ++i) 73 { 74 if (arr1[i] == 0) arr1[i] = ++res; 75 } 76 res = 0; 77 for (int i = 1; i <= n; ++i) 78 { 79 if (arr2[i] == 0) arr2[i] = ++res; 80 } 81 cout << "YES" << endl; 82 for (int i = 1; i <= n; ++i) 83 { 84 cout << arr1[i]; 85 if (i == n) cout << endl; 86 else cout << " "; 87 } 88 for (int i = 1; i <= n; ++i) 89 { 90 cout << arr2[i]; 91 if (i == n) cout << endl; 92 cout << " "; 93 } 94 95 } 96 97 98 /* 99 4 5 100 11 12 13 14 15 101 0 4 10 15 20 102 2 10 30 32 40 103 20 21 22 23 24 104 */