MAZE
题目背景:
12.10 省选模拟T2
分析:结论 + 最小字典序匹配
我们考虑选择方式,如果有一个妹子,她是其中某些人的最喜欢的妹子,但是她匹配上的人不是最喜欢她的人中的一个,那么可以通过调整让其中一个最喜欢她的人X匹配她,然后这个妹子原来匹配的人Y也去匹配他最喜欢的妹子,然后把Y最喜欢的妹子原来匹配的人的妹子改成X原来匹配的妹子,这样一定能够获得一个更多人满意的方案,那么现在就比较明显了,如果一个妹子有人最喜欢,那么她一定会被匹配到一个最喜欢她的人,所以,一个人要么匹配他最喜欢的妹子,要么匹配他第一个没有人最喜欢的妹子,直接对于每个人建边,跑最小匹配就可以了,如果没有匹配,就说明没有和谐方案。
Source:
/*created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>inline char read() {static const int IN_LEN = 1024 * 1024;static char buf[IN_LEN], *s, *t;if (s == t) {t = (s = buf) + fread(buf, 1, IN_LEN, stdin);if (s == t) return -1;}return *s++;
}///*
template<class T>
inline void R(T &x) {static char c;static bool iosig;for (c = read(), iosig = false; !isdigit(c); c = read()) {if (c == -1) return ;if (c == '-') iosig = true; }for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;
}
//*/const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;*oh++ = c;
}template<class T>
inline void W(T x) {static int buf[30], cnt;if (x == 0) write_char('0');else {if (x < 0) write_char('-'), x = -x;for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;while (cnt) write_char(buf[cnt--]);}
}inline void flush() {fwrite(obuf, 1, oh - obuf, stdout);
}/*
template<class T>
inline void R(T &x) {static char c;static bool iosig;for (c = getchar(), iosig = false; !isdigit(c); c = getchar())if (c == '-') iosig = true; for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;
}
//*/const int MAXN = 50 + 10;int n;
int p[MAXN][MAXN], match[MAXN << 1];
bool vis[MAXN], in[MAXN];
std::vector<int> edge[MAXN];inline void read_in() {R(n);for (int i = 1; i <= n; ++i)for (int j = 1; j <= n; ++j) R(p[i][j]);
}inline bool dfs(int cur) {for (int p = 0; p < edge[cur].size(); ++p) {int v = edge[cur][p], temp = match[n + v];if (in[v]) continue ;match[n + v] = cur, match[cur] = v, in[v] = true;if (!temp || dfs(temp)) return true;match[n + v] = temp, match[temp] = v;}return false;
}inline void solve() {for (int i = 1; i <= n; ++i) vis[p[i][1]] = true;for (int i = 1; i <= n; ++i) {edge[i].push_back(p[i][1]);for (int j = 2; j <= n; ++j)if (!vis[p[i][j]]) {edge[i].push_back(p[i][j]);break ;}std::sort(edge[i].begin(), edge[i].end());}for (int i = n; i >= 1; --i) {memset(in, 0, sizeof(in));if (!dfs(i)) std::cout << "Impossible", exit(0);
// for (int i = 1; i <= n; ++i) W(match[i]), write_char(' ');}for (int i = 1; i <= n; ++i) W(match[i]), write_char(' ');
}int main() {freopen("maze.in", "r", stdin);freopen("maze.out", "w", stdout);read_in();solve();flush();return 0;
}