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. [STL][C++]LIST

    参考:http://blog.csdn.net/whz_zb/article/details/6831817 list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素.在STL中,list ...

  2. 【Golang 接口自动化01】使用标准库net/http发送Get请求

    发送Get请求 使用Golang发送get请求很容易,我们还是使用http://httpbin.org作为服务端来进行演示. package main import ( "bytes&quo ...

  3. Java读取xml

    首先将xml的格式展示出来,如下 <?xml version="1.0"?> <configuration> <connectionStrings n ...

  4. 雷林鹏分享:C# 集合(Collection)

    C# 集合(Collection) 集合(Collection)类是专门用于数据存储和检索的类.这些类提供了对栈(stack).队列(queue).列表(list)和哈希表(hash table)的支 ...

  5. 使用mothur进行OTU聚类

    微生物16S的OTU聚类工具有很多,最常用的就是 usearch.cdhit-OTU.mothur. 这些工具大多都是针对二代测序平台的,usearch的64bit版本是收费的. 如果要跑PacBio ...

  6. LeetCode--112--路径总和

    问题描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例:  给定如下二叉树,以及目标和 s ...

  7. Java 主要特性

    Java 有下面的一些主要特性. 面向对象 在 Java 中,所有的都是对象.正式因为 Java 基于对象模型,所以 Java 更加容易进行扩展. Java语言提供类.接口和继承等面向对象的特性,为了 ...

  8. ns-3

    二.NS-3C++脚本的编写如前所述,NS-3的脚本使用C++语言(也支持python),使用四种类型的网络构件(Node.NetDevice.Channel.Application).一个简单的脚本 ...

  9. h1026 BFS(打印x与路径)

    题意: Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. UVA-1579 Matryoshka (区间DP)

    题目大意:n个俄罗斯套娃,都有相应的编号,每次可将两个相邻的套娃组合成一组,每次合成只能小的放到大的里面,并且是逐层嵌套.问将这n个套娃分成若干个组,并且每组都是编号从1开始的连续序列,最少需要几步. ...