题目
题目描述
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 nn distinct points in the plane. ii -th point is point (x_{i},y_{i})(x
i
,y
i
) . 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.
输入格式
The first line of input contains integer nn ( 1<=n<=2×10^{5}1<=n<=2×10
5
).
The next nn lines contain the information about the points, ii -th line contains two integers x_{i}x
i
and y_{i}y
i
( 1<=x_{i},y_{i}<=2×10^{5}1<=x
i
,y
i
<=2×10
5
), the ii -th point coordinates.
It is guaranteed that there is at least one valid answer.
输出格式
Print the answer as a sequence of nn characters ‘r’ (for red) or ‘b’ (for blue) where ii -th character denotes the color of the fish in the ii -th point.
题意翻译
给定 nn 个整点。
你要给每个点染成红色或蓝色。
要求同一水平线或垂直线上两种颜色的数量最多相差 11。
n,x_i, y_i \le 2 \times 10^5n,x
i
,y
i
≤2×10
5
。
输入输出样例
输入 #1复制
4
1 1
1 2
2 1
2 2
输出 #1复制
brrb
输入 #2复制
3
1 1
1 2
2 1
输出 #2复制
brr
思路
考虑将横纵坐标之间连一条边。
容易发现,如果把每个点的横纵坐标连一条边,那么就会有一个二分图(xx坐标和yy坐标)。接下来,我们每一条边就表示一个点。那么怎么判断是红还是蓝呢?
其实不难。既然是二分图,我们就对它跑一遍欧拉回路定向,入边为蓝,出边为红即可。因为每条边连接都是x坐标和y坐标之间的,所以对边定向就确定了一个点的颜色。
代码
#include<bits/stdc++.h>
using namespace std;
const int N=500010;
vector<int>p[N];
int vis[N],ans[N][2];
void dfs(int x,int y)
{
if(vis[x])
return;
else
vis[x]=y;
int sz=p[x].size();
for(int i=0;i<sz;i++)
dfs(p[x][i],1^y);
}
int main()
{
int n,x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
if(ans[x][0])
{
p[i].push_back(ans[x][0]);
p[ans[x][0]].push_back(i);
ans[x][0]=0;
}
else
ans[x][0]=i;
if(ans[y][1])
{
p[i].push_back(ans[y][1]);
p[ans[y][1]].push_back(i);
ans[y][1]=0;
}
else
ans[y][1]=i;
}
for(int i=1;i<=n;i++)
{
dfs(i,0);
if(vis[i])
putchar('r');
else
putchar('b');
}
return 0;
}