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. m_Orchestrate learning system---三十四、使用重定义了$的插件的时候最容易出现的问题是什么

    m_Orchestrate learning system---三十四.使用重定义了$的插件的时候最容易出现的问题是什么 一.总结 一句话总结:如下面这段代码,定义了$的值,还是会习惯性的把$当成jQ ...

  2. CSS实现和选择器

    CSS实现和选择器 本课内容: 一.实现CSS四种方式 1,每个html标签中都有一个style样式属性,该属性的值就是css代码.(针对一个标签)2,使用style标签的方式. 一般都定义在head ...

  3. dns未设置 PHP Warning: file_get_contents():php_network_getaddresses: getaddrinfo failed:

    php通过去访问外部网站时,出现以下提示: PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed ...

  4. every day a practice —— morning

    In 25 years, Panda Express has transformed from a single restaurant in a southern California mall to ...

  5. codeforces 993c//Careful Maneuvering// Codeforces Round #488 by NEAR (Div. 1)

    题意:x轴-100和+100的有敌人飞船,纵坐标由输入数据给出,我方有2飞船在x轴0,y坐标待定.0时刻时敌人同时向我方2飞船发出光线,光线会穿透飞船打到敌人自己,问2飞船放在哪敌人损失最大? 假如- ...

  6. 介绍 Jersey 依赖

    Jersey 使用 Apache Maven 进行构造和安装.非快照(Non-snapshot)Jersey 发行版本是不会部署到 中心 Maven 仓库(Central Maven Reposito ...

  7. Jon Snow and his Favourite Number CodeForces - 768C (技巧)

    链接 题意 给定数组, 每次操作先将数组排序, 再将奇数位全部异或x, 求k次操作后数组最大值与最小值 (1 ≤ n ≤ 105, 0 ≤ k ≤ 105, 0 ≤ x ≤ 103) 题解 直接暴力模 ...

  8. 『PyTorch』第五弹_深入理解Tensor对象_中上:索引

    一.普通索引 示例 a = t.Tensor(4,5) print(a) print(a[0:1,:2]) print(a[0,:2]) # 注意和前一种索引出来的值相同,shape不同 print( ...

  9. HDU1506(真心不错的DP)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. JS水平移动图片

    横向: <div id=demo style="overflow:hidden;width:200px;border:2px solid #e0e0e0;padding:2px;&qu ...