E. Arpa’s overnight party and Mehrdad’s silent entering

Note that girls in Arpa’s land are really attractive.

Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw n pairs of friends sitting around a table. i-th pair consisted of a boy, sitting on the ai-th chair, and his girlfriend, sitting on the bi-th chair. The chairs were numbered 1through 2n in clockwise direction. There was exactly one person sitting on each chair.

There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that:

  • Each person had exactly one type of food,
  • No boy had the same type of food as his girlfriend,
  • Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs 2n and 1 are considered consecutive.

Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.

Input

The first line contains an integer n (1  ≤  n  ≤  105) — the number of pairs of guests.

The i-th of the next n lines contains a pair of integers ai and bi (1  ≤ ai, bi ≤  2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair.

Output

If there is no solution, print -1.

Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, otherwise print 2.

If there are multiple solutions, print any of them.

Example
input
3
1 4
2 5
3 6
output
1 2
2 1
1 2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define mkr make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long LL;
const int MAXN=1e5+;
pair<int,int>p[MAXN];
vector<int>G[MAXN<<];
int color[MAXN<<],n;
bool bipartite(int u)
{
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
if(color[v]==color[u])return ;
if(!color[v])
{
color[v]=-color[u];
if(!bipartite(v))return ;
}
}
return ;
}
int main()
{
scanf("%d",&n);
for(int i=,u,v;i<=n;i++)
{
scanf("%d%d",&u,&v);
G[u].pb(v);
G[v].pb(u);
p[i]=mkr(u,v);
}
for(int i=;i<=n;i++)
{
G[i<<].pb(i<<^);
G[i<<^].pb(i<<);
}
for(int i=;i<=*n;i++)
if(!color[i])color[i]=,bipartite(i);
for(int i=;i<=n;i++)
printf("%d %d\n",color[p[i].fi],color[p[i].se]);
return ;
}

codeforces 742E (二分图着色)的更多相关文章

  1. HDU 5313 Bipartite Graph (二分图着色,dp)

    题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...

  2. HDU 5285 wyh2000 and pupil (二分图着色)

    题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...

  3. Edge coloring of bipartite graph CodeForces - 600F (二分图染色)

    大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0 ...

  4. Vasya and Endless Credits CodeForces - 1107F (二分图完美匹配)

    大意: n中贷款, 每种只能买一次, 第$i$种给$a_i$元, 要还款$k_i$个月, 每个月底还$b_i$元. 每个月可以在月初申请一种贷. 求某一时刻能得到的最大钱数.

  5. 【题解】CF742E (二分图+构造)

    [题解]CF742E (二分图+构造) 自闭了CodeForces - 742E 给定的条件就是一个二分图的模型,但是有一些不同.不同就不同在可以出现相邻两个点颜色相同的情况. 构造常用方法之一是按奇 ...

  6. NOIP2008 双栈队列

    1.      双栈排序 (twostack.pas/c/cpp) Tom 最近在研究一个有趣的排序问题.如图所示,通过 2 个栈 S1 和 S2,Tom 希望借助 以下 4 种操作实现将输入序列升序 ...

  7. CodeForces 173B Chamber of Secrets 二分图+最短路

    题目链接: http://codeforces.com/problemset/problem/173/B 题意: 给你一个n*m的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...

  8. Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理

    https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...

  9. D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)

    D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...

随机推荐

  1. SVN show log failed

    Q: SVN 不能显示日志 能正常update, commit,但是show log的时候报错 A:可能原因是服务器权限配置问题 修改配置文件svnserve.conf 和 authz, 修改前请先备 ...

  2. Birt导出Excel图片

    有一段时间没有使用Birt了,最近突然之间发现新版的Birt可以支持导出Excel附带图片.我目前下载的是Birt 4.3版本的,导出图片的也只能在Excel 2007下面能够实现,2003的xls格 ...

  3. C++11引用临时变量的终极解析

    工作中遇到一个引用临时变量的问题,经过两天的学习,私以为:不仅弄明白了这个问题,还有些自己的独到见解. 这里使用一个简单的例子来把自己的学习过程和理解献给大家,如果有什么问题请不吝指正.   **** ...

  4. Java class file format specfication

    Java class file format spec Link: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html Her ...

  5. XML文档

    XML(Extensible Markuo Language)可标记扩展语言.它是一种以简单文本格式存储数据的方式,可以被任何计算机读取. XML文档里包含的元素都是可以自定义的. 1.XML文档声明 ...

  6. vertica 8.0 新特性

    前言: <<line>> 表明在vertica 8.0文档中的title   正文: 1-支持平台 1.1-操作系统 <<Vertica Server and Ve ...

  7. Jenkins自动部署Tomcat项目

    Jenkins自动部署Tomcat项目 1.安装jenkins 插件 启动Jenkins,进入系统管理-插件管理: 选择Deploy to container Plugin 插件安装:

  8. sort详解2

    linux sort 命令详解 sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比 ...

  9. Samba安装配置

    Samba简介 Samba官网:http://www.samba.orgSMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不 ...

  10. Spark 2.6.1 源代码在 eclipse 的配置

    本文地址:http://www.cnblogs.com/jying/p/3671767.html 这么个问题又耗费了偶一天时间,真是羞愧.. 上午从官网svn地址下载最新的 spark 包,总是下载失 ...