CodeForces - 547D: Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)

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.

CodeForces - 547D:  Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)

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 ;
}
上一篇:'InitializeCulture' is not a member of 'XXXX'


下一篇:「CF547D」 Mike and Fish