codeforces #547D Mike and Fish (二分图 欧拉回路)

博客介绍了如何帮助Mike在平面上的n个点放置红蓝鱼,使得每行每列的鱼颜色差不超过1。通过将坐标转化为二分图,并寻找欧拉回路进行交替染色,确保条件满足。解决方案包括调整奇度数节点,形成偶数度数的二分图,然后求解欧拉回路进行染色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mike and Fish CodeForces - 547D

As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange thing about him is he has an infinite number of blue and red fish.

He has marked n distinct points in the plane. i-th point is point (xi, yi). He wants to put exactly one fish in each of these points such that the difference between the number of red fish and the blue fish on each horizontal or vertical line is at most 1.

He can’t find a way to perform that! Please help him.

Input
The first line of input contains integer n (1 ≤ n ≤ 2 × 105).

The next n lines contain the information about the points, i-th line contains two integers xi and yi (1 ≤ xi, yi ≤ 2 × 105), the i-th point coordinates.

It is guaranteed that there is at least one valid answer.

Output
Print the answer as a sequence of n characters ‘r’ (for red) or ‘b’ (for blue) where i-th character denotes the color of the fish in the i-th point.

Example
Input

4
1 1
1 2
2 1
2 2

Output

brrb

Input

3
1 1
1 2
2 1

Output

brr

思路:
摘自 < %popoqq >
补充一点
题目大意:给定平面上的n个点,要求将每个点染成红色/蓝色,使得每行/每列的红色点数和蓝色点数之差≤1。

坐标系中的问题,考虑把x坐标和y坐标分成两个集合,将每一个横坐标/纵坐标看做一个点,每个点看做一条连接两个坐标的边。
现在

### Codeforces 平台上的二分图染色算法题目 #### CF1949I. Disks 的解题思路 对于给定的一组圆盘,判断是否存在一种方式使得这些圆盘能够通过改变大小来满足特定条件。如果存在某个圆既变小又变大,或者变小的圆的数量不等于变大的圆的数量,则该连通块无法实现目标状态[^5]。 为了验证这一点,可以采用二分图染色的方法: - 将每个圆视为图中的节点; - 如果两个圆之间有交集,则在这两个节点间建立一条边; - 使用两种不同的颜色尝试对整个图形进行着色,在此过程中遇到冲突则说明不存在合法方案; 这种方法不仅适用于本题描述的情况,也广泛应用于其他涉及二分性质的问题求解中。 #### 构造性算法实例 在解决一些具有构造特性的编程挑战时,比如构建IP地址这样的任务[T1 IP地址(ip)][^2],虽然这并不是典型的二分图问题,但是当涉及到如何有效地分配资源(如同一网络内的设备编号),也可以借鉴类似的思维模式——即合理规划不同部分之间的关系以达到最优配置效果。 ```cpp // C++ code snippet demonstrating bipartite graph coloring approach #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 7; int color[MAXN]; vector<int> adj[MAXN]; bool dfs(int u, int c) { color[u] = c; for (auto v : adj[u]) { if (!color[v]) { if (!dfs(v, 3 - c)) return false; // alternate colors between 1 and 2 } else if (color[v] == c) return false; // found an odd cycle or conflict } return true; } void solve() { memset(color, 0, sizeof(color)); bool isBipartite = true; for (int i = 1; i <= n && isBipartite; ++i) { if (!color[i]) isBipartite &= dfs(i, 1); } cout << (isBipartite ? "YES\n" : "NO\n"); } ``` 上述代码展示了利用深度优先搜索(DFS)来进行二分图检测的过程。这里假设输入已经准备好了一个无向图的数据结构`adj[]`表示邻接表形式存储的图以及顶点数量n。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值