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.

Examples

Input
4
1 1
1 2
2 1
2 2
Output
brrb
Input
3
1 1
1 2
2 1
Output
brr

题意:给定二维坐标,求一种合法的染色情况,使得每一行的颜色差异<=1;每一列也是。

思路:想不到。(不过不是第一次见了,多校也见过)。把点转化为边,那么就是每个点的两种颜色存疑最多为1,把两种颜色看成入边和出边,则和欧拉路,欧拉回路有些联系。  把度数为1的加“虚边”,染色即可。

自己的版本:离散化; 一个连通块,最多两个奇点,如果有,找出来加边,然后dfs。

(dfs的之后需要加地址符,防止多次访问无效边,会T31

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],cnt,vis[maxn],ans[maxn];
int x[maxn],y[maxn],a[maxn],tot1,tot2,ind[maxn],used[maxn];
int q[maxn],top,opt;
void add(int u,int v)
{
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void dfs(int u)
{
if(used[u]) return ;
if(ind[u]&) q[++top]=u; used[u]=;
for(int i=Laxt[u];i;i=Next[i]) dfs(To[i]);
}
void dfs1(int u,int f)
{
for(int &i=Laxt[u];i;i=Next[i]){ //加个地址符,防止被稠密图卡。
if(!vis[i]){
ans[i/]=opt; vis[i]=vis[i^]=;
dfs1(To[i],i);
}
}
ans[f/]=opt; opt^=;
}
int main()
{
int N; scanf("%d",&N);
rep(i,,N) scanf("%d%d",&x[i],&y[i]);
rep(i,,N) a[++tot1]=x[i];
sort(a+,a+tot1+);tot1=unique(a+,a+tot1+)-(a+);
rep(i,,N) x[i]=lower_bound(a+,a+tot1+,x[i])-a;
rep(i,,N) a[++tot2]=y[i];
sort(a+,a+tot2+); tot2=unique(a+,a+tot2+)-(a+);
rep(i,,N) y[i]=lower_bound(a+,a+tot2+,y[i])-a;
cnt=;
rep(i,,N) {
add(x[i],y[i]+tot1);
add(y[i]+tot1,x[i]);
ind[x[i]]++; ind[y[i]+tot1]++;
}
rep(i,,tot1+tot2) {
if(!used[i]){
top=; dfs(i);
rep(i,,top){
add(q[i],q[i+]); add(q[i+],q[i]); i++;
}
opt=;
if(top) dfs1(q[],); else dfs1(i,);
}
}
rep(i,,N) putchar(ans[i]?'b':'r');
return ;
}

看了std:不离散化;而且任意的奇点偶可以加边,效果不变。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],cnt=,vis[maxn],ans[maxn];
int x[maxn],y[maxn],a[maxn],tot1,tot2,ind[maxn];
int q[maxn],top,opt;
inline char gc(){
static char buf[<<],*S,*T;
if(S==T){T=(S=buf)+fread(buf,,<<,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=,f=;char ch=gc();
while(ch<''||ch>''){if(ch=='-') f=-;ch=gc();}
while(ch>=''&&ch<='') x=x*+ch-'',ch=gc();
return x*f;
}
void add(int u,int v)
{
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void dfs(int u)
{
for(int &i=Laxt[u];i;i=Next[i]){
if(!vis[i]&&!vis[i^]){
vis[i]=; dfs(To[i]);
}
}
}
int main()
{
int N; scanf("%d",&N);
rep(i,,N) x[i]=read(),y[i]=read()+2e5;
rep(i,,N) {
add(x[i],y[i]);
add(y[i],x[i]);
ind[x[i]]++; ind[y[i]]++;
}
int lst=;
rep(i,,4e5){
if(ind[i]&){
if(lst) add(lst,i),add(i,lst),lst=;
else lst=i;
}
}
rep(i,,4e5) if(ind[i])dfs(i);
rep(i,,N) putchar(vis[i<<]?'b':'r');
return ;
}

CodeForces - 547D: Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)的更多相关文章

  1. Codeforces 547D - Mike and Fish(欧拉回路)

    Codeforces 题目传送门 & 洛谷题目传送门 首先考虑将题目中的条件转化为图论的语言.看到"行""列",我们很自然地想到二分图中行.列转点,点转 ...

  2. Codeforces.547D.Mike and Fish(思路 欧拉回路)

    题目链接 \(Description\) 给定平面上n个点,将这些点染成红or蓝色,要求每行.每列红色点与蓝色点数量的差的绝对值<=1.输出方案(保证有解). \(Solution\) 参考这 ...

  3. Codeforces 547D Mike and Fish

    Description 题面 题目大意:有一个的网格图,给出其中的 \(n\) 个点,要你给这些点染蓝色或红色,满足对于每一行每一列都有红蓝数量的绝对值之差不超过1 Solution 首先建立二分图, ...

  4. CodeForces 547D Mike and Fish 思维

    题意: 二维平面上给出\(n\)个点,然后对每个点进行染色:红色和蓝色,要求位于同一行或同一列的点中,红色点和蓝色点的个数相差不超过1 分析: 正解是求欧拉路径,在这篇博客中看到一个巧妙的思路: 对于 ...

  5. Codeforces 247D Mike and Fish

    Mike and Fish 我们可以把这个模型转换一下就变成有两类点,一类是X轴, 一类是Y轴, 每个点相当于对应的点之间建一条边, 如果这条边变红两点同时+1, 变蓝两点同时-1. 我们能发现这个图 ...

  6. 547D Mike and Fish

    传送门 分析 见正睿10.3笔记 代码 #include<iostream> #include<cstdio> #include<cstring> #include ...

  7. CF 547 D. Mike and Fish

    D. Mike and Fish http://codeforces.com/contest/547/problem/D 题意: 给定平面上n个点,将这些点染成红或者蓝色,要求每行.每列红色点与蓝色点 ...

  8. 「CF547D」 Mike and Fish

    「CF547D」 Mike and Fish 传送门 介绍三种做法. \(\texttt{Solution 1}\) 上下界网络流 我们将每一行.每一列看成一个点. 两种颜色的数量最多相差 \(1\) ...

  9. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

随机推荐

  1. 基于Socket的Android手机视频实时传输

    首先,简单介绍一下原理.主要是在手机客户端 (Android)通过实现Camera.PreviewCallback接口,在其onPreviewFrame重载函数里面获取摄像头当前图像数据, 然后通过S ...

  2. 难部署的taiga,式微的circus——趋势从进程管理到容器管理,简单才是美

    一直需要一个项目管理系统,一直没时间弄. taiga是github上搜project management star最多的项目,又是基于django用python写的后端,所以就用它: 但是,集中精力 ...

  3. Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件

    由于Writer嫌我文章过长,只能把上篇拆开两半了.以下是接着上篇的. 准备工作做完了,现在就要完成点击事件. 定义Expander和单击事件: 1: /// <summary> 2: / ...

  4. USACO 铂金 T1

    题意 给出一个数轴,每次可以选择停下并得到当前点的收益,或者继续随机向左右游走,走到边界游戏结束收益为0. 求从每个点出发的最大期望收益.(n<=1e5) 有一个显然的dp方程 这个方程是带环的 ...

  5. 『cs231n』作业3问题2选讲_通过代码理解LSTM网络

    LSTM神经元行为分析 LSTM 公式可以描述如下: itftotgtctht=sigmoid(Wixxt+Wihht−1+bi)=sigmoid(Wfxxt+Wfhht−1+bf)=sigmoid( ...

  6. 『Python』图像金字塔、滑动窗口和非极大值抑制实现

    图像金字塔 1.在从cv2.resize中,传入参数时先列后行的 2.使用了python中的生成器,调用时使用for i in pyramid即可 3.scaleFactor是缩放因子,需要保证缩放后 ...

  7. 基于binlog的增量备份

    1.1 增量备份简介 增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加或者被修改的文件.这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件:第二次增 ...

  8. RAC配置(启停库)

    关库顺序 :先关闭数据库 然后关闭节点资源 [root@rac1 ~]# srvctl stop   database  -d 数据库名[root@rac1 ~]# srvctl stop   ins ...

  9. Leetcode 92

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  10. 多态性&& 虚函数 && 抽象类

    http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/11/2134673.html 多态性 指相同对象收到不同消息或不同对象收到相同消息时产生不同的实现动 ...