codeforces 742E (二分图着色)
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.
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.
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.
3
1 4
2 5
3 6
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 (二分图着色)的更多相关文章
- HDU 5313 Bipartite Graph (二分图着色,dp)
题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...
- HDU 5285 wyh2000 and pupil (二分图着色)
题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...
- Edge coloring of bipartite graph CodeForces - 600F (二分图染色)
大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0 ...
- Vasya and Endless Credits CodeForces - 1107F (二分图完美匹配)
大意: n中贷款, 每种只能买一次, 第$i$种给$a_i$元, 要还款$k_i$个月, 每个月底还$b_i$元. 每个月可以在月初申请一种贷. 求某一时刻能得到的最大钱数.
- 【题解】CF742E (二分图+构造)
[题解]CF742E (二分图+构造) 自闭了CodeForces - 742E 给定的条件就是一个二分图的模型,但是有一些不同.不同就不同在可以出现相邻两个点颜色相同的情况. 构造常用方法之一是按奇 ...
- NOIP2008 双栈队列
1. 双栈排序 (twostack.pas/c/cpp) Tom 最近在研究一个有趣的排序问题.如图所示,通过 2 个栈 S1 和 S2,Tom 希望借助 以下 4 种操作实现将输入序列升序 ...
- CodeForces 173B Chamber of Secrets 二分图+最短路
题目链接: http://codeforces.com/problemset/problem/173/B 题意: 给你一个n*m的地图,现在有一束激光从左上角往左边射出,每遇到‘#’,你可以选择光线往 ...
- Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理
https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...
- D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...
随机推荐
- 虚拟机中CentOS 7下PHP环境配置
为了简单起见,虚拟机网卡设置为桥接模式 1.yum install httpd php 2.yum install mariadb 3.启动mariadb systemctl start maria ...
- Redis安装手册
转载请注明出处:http://www.cnblogs.com/robinjava77/p/5465146.html (Robin) 1)下载redis:wget http://download.red ...
- What is SPI?
原文地址:http://www.fpga4fun.com/SPI1.html SPI is a simple interface that allows one chip to communicate ...
- H5全屏滚动专题页最佳实践
1.slip.js + rem.js 主要插件: slip.js github: https://github.com/binnng/slip.js rem.js 插件为阿里淘宝的 rem 实现的基础 ...
- AOP和IOC的作用
IOC:控制反转,是一种设计模式.一层含义是控制权的转移:由传统的在程序中控制依赖转移到由容器来控制:第二层是依赖注入:将相互依赖的对象分离,在spring配置文件中描述他们的依赖关系.他们的依赖关系 ...
- Json的序列化与反序列化
对于Json的序列化和反序列化,如果自己编写源代码来实现的话,很复杂很烦,所以我采用的是使用别人已经写好的引用文件.这类文件网上有很多,我用的是LitJson,当然Newtonsoft也可以,但后者需 ...
- Lua 5.2 编译 For Windows
body { font-family: 微软雅黑; font-size: 11pt; line-height: 1.5; } html, body { color: #000000; backgrou ...
- [delphi]极域学生端解除键盘鼠标锁定退出全屏广播-强制窗口化-源代码
v2.0 2015-07-11 更新了V2.0 版本 发布在吾爱破解论坛 欢迎下载使用 http://www.52pojie.cn/thread-382769-1-1.html ---------- ...
- 今天使用bootstrap中的tagsinput控件,碰到个小问题
我直接使用时,后台一直报Uncaught Can't add objects when itemValue option is not set错误, 代码如下: <div> <inp ...
- 《IT蓝豹》挑战独立开发项目能力
做了5年的android开发,今天没事写写刚入行不久的时候第一次独立开发项目的心得体会, 当时我刚工作8个月,由于公司运营不善倒闭了,在2011年3月份我开始准备跳槽, 看了一周andro ...