題目大意:有$n$個點,黑白染色,且要求同一行同一列兩種顏色個數最多差$1$,輸出方案ios
題解:考慮把每一個點橫座標和縱座標連一條邊,若要求兩種顏色個數相同,能夠至關於入度與出度相同,就是一個歐拉回路。這裏能夠相差一,能夠把奇點向$0$點連一條邊,就能夠解決這個問題。spa
卡點:無blog
C++ Code:ci
#include <cstdio> #include <algorithm> #include <iostream> const int maxn = 4e5 + 10; int n, color[maxn]; int head[maxn], deg[maxn], cnt = 1; struct Edge { int to, nxt; } e[maxn << 2]; void addedge(int a, int b) { e[++cnt] = (Edge) { b, head[a] }; head[a] = cnt; e[++cnt] = (Edge) { a, head[b] }; head[b] = cnt; ++deg[a], ++deg[b]; } void dfs(int u) { for (int &i = head[u]; i; i = e[i].nxt) if (!color[i >> 1]) color[i >> 1] = 1 + (u <= 200000), dfs(e[i].to); } int main() { std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0); std::cin >> n; for (int i = 1, a, b; i <= n; ++i) { std::cin >> a >> b; addedge(a, b + 200000); } for (int i = 1; i <= 400000; ++i) if (deg[i] & 1) addedge(0, i); for (int i = 1; i <= 200000; ++i) dfs(i); for (int i = 1; i <= n; ++i) std::cout << "br"[color[i] - 1]; std::cout.put('\n'); return 0; }