[CF547D]Mike and Fish

题目大意:有$n$个点,黑白染色,且要求同一行同一列两种颜色个数最多差$1$,输出方案

题解:考虑把每个点横坐标和纵坐标连一条边,若要求两种颜色个数相同,可以相当于入度与出度相同,就是一个欧拉回路。这里可以相差一,可以把奇点向$0$点连一条边,就可以解决这个问题。

卡点:

 

C++ Code:

#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;
}

  

posted @ 2019-10-31 19:15  Memory_of_winter  阅读(204)  评论(0编辑  收藏  举报