Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define For(i, n) for(int i = 0; i < n; ++i)
- #define all(x) (x).begin(),(x).end()
- #define rall(x) (x).rbegin(),(x).rend()
- #define ld long double
- #define pii pair<int, int>
- #define vt vector
- #define ll long long
- // #define endl '\n'
- const int MAX = 1e9;
- const int MOD = 1e9 + 7;
- const ll INF = 1e18;
- const ld PI = 3.14159265358979323846;
- template<typename T>
- void read(vt<T> & a) {
- For(i, a.size()) cin >> a[i];
- }
- template<typename T>
- void print(vt<T> & a) {
- For(i, a.size()) cout << a[i] << " ";
- cout << endl;
- }
- template<typename T>
- void print2(vt<vt<T> > & a) {
- For(i, a.size()) {
- For(j, a[i].size()) cout << a[i][j] << " ";
- cout << endl;
- }
- }
- template<typename T>
- void read2(vt<vt<T> > & a) {
- For(i, a.size()) For(j, a[i].size()) cin >> a[i][j];
- }
- const int N = 2000;
- vt<int> PAR(N);
- int rec(int p, int cond, vt<vt<int> > & a) {
- if (a[0].size() == 1) {
- PAR[a[0][0]] = p;
- return 1;
- }
- if (a[0].size() % 2 != 1) {
- return 0;
- }
- int k = a.size();
- int n = a[0].size();
- map<int, set<int> > mp;
- for (int i = 0; i < k; i++) {
- for (int j = 0; j < n; j++) {
- int x = a[i][j];
- mp[x].insert(j);
- mp[x].insert(n - 1 - j);
- }
- }
- int mn = MAX;
- for (auto x : mp) {
- int pos = x.second.size();
- if (pos < mn) {
- mn = pos;
- }
- }
- int cnt_mns = 0;
- for (auto x : mp) {
- int pos = x.second.size();
- if (pos == mn) {
- cnt_mns++;
- }
- }
- int mid = -1;
- if (cnt_mns % 2 == 1) {
- int cur_mn = 0;
- for (int j = 0; j < n; j++) {
- int x = a[0][j];
- int sz = mp[x].size();
- if (sz == mn) {
- if (cur_mn == cnt_mns / 2) {
- mid = x;
- break;
- }
- cur_mn++;
- }
- }
- } else {
- int mx_diff = MAX;
- int cnt = 0;
- for (auto x : mp) {
- int pos = x.second.size();
- if (pos == mn) {
- if (cnt == cond) {
- mid = x.first;
- break;
- }
- cnt++;
- }
- }
- }
- int first_half_contains = a[0][0];
- vt<vt<vt<int> > > halfs(2, vt<vt<int> > (k));
- for (int i = 0; i < k; i++) {
- bool swp = false;
- for (int j = 0; j < n; j++) {
- int x = a[i][j];
- if (x == first_half_contains) {
- swp = true;
- break;
- }
- if (x == mid) {
- break;
- }
- }
- int cur = 0;
- for (int j = 0; j < n; j++) {
- int x = a[i][j];
- if (x == mid) {
- cur++;
- continue;
- }
- halfs[cur][i].push_back(x);
- }
- if (swp) {
- swap(halfs[0][i], halfs[1][i]);
- }
- }
- for (int cur = 0; cur < 2; cur++) {
- int sz = halfs[cur][0].size();
- for (int i = 0; i < k; i++) {
- if (halfs[cur][i].size() != sz) {
- return 0;
- }
- }
- }
- PAR[mid] = p;
- int res = 1;
- if (rec(mid, 0, halfs[0]) != 1) {
- res &= rec(mid, 1, halfs[0]);
- }
- if (rec(mid, 0, halfs[1]) != 1) {
- res &= rec(mid, 1, halfs[1]);
- }
- return res;
- }
- void solve()
- {
- int n, k; cin >> n >> k;
- vt<vt<int> > a(k, vt<int> (n));
- read2(a);
- int res = rec(-1, 0, a);
- if (res == 0) {
- res = rec(-1, 1, a);
- }
- assert(res == 1);
- for (int i = 1; i <= n; i++) {
- cout << PAR[i] << " ";
- }
- cout << endl;
- }
- int main()
- {
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- // #define FILE
- #ifdef FILE
- freopen("output.txt", "r", stdin);
- // freopen("input.txt", "r", stdin);
- #endif
- cout << setprecision(20) << fixed;
- int T = 1;
- For(t, T) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement