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 ...
随机推荐
- windows python文件拷贝到linux上执行问题
之前在Windows下写好了一个Python脚本,运行没问题,今天在Linux下,脚本开头的注释行已经指明了解释器的路径,也用chmod给了执行权限,但就是不能直接运行脚本. 1 问题1: 报错:: ...
- 关于ckeditor ajax提交到后台 问题
ckeditor 提交时 如果有带有html时是提交不了的 解决办法就是 你在提交的时候 将ckeditor获取的只编码(encodeURI) 然后在传到后台提交的时候 在解码 就ok了 ckname ...
- python中的浅拷贝和深拷贝
1.赋值语句 a = 'abc' b = a print id(a) print id(b) # id(a):29283464 # id(b):29283464 通过简单的复制,我们可以看到,a b其 ...
- Hsqldb安装与使用
HSQLDB是一个轻量级的纯Java开发的开放源代码的关系数据库系统,其体积小,占用空间小,使用简单,支持内存运行方式等特点.可以在http://sourceforge.net/projects/hs ...
- Java三大框架的配置
1. 首先是spring,右键项目-myeclipse-capabilitise-install spring etc.类似的就好,生成applicationContext.xml和spring一些类 ...
- Programming Entity Framework 翻译
刚开始接触.net,c#语法应该说还没掌握好.学习实践的旅程就从linq和EF开始吧.感觉相比之前的开发方式,linq和EF方便好多. linq入门用了好久,因为c#不行,补习了2.0的泛型,3.0和 ...
- Fisher vector for image classification
http://files.cnblogs.com/files/sylar120/fisher_vector.rar 拿各个参数上的偏导作为特征
- UEditor演变的迷你版编辑器
建立一个demo.html文件,首先在需要添加编辑器的地方加入以下代码,使用style可以设置编辑器的宽度和高度. <script type="text/plain" id= ...
- linux c学习笔记08--文件操作
1 . 创建int creat(const char *filename, mode_t mode);参数 mode 指定新建文件的存取权限,它同 umask 一起决定文件的最终权限( mode&am ...
- oracle删除users表空间
1.users表空间一般情况下是默认的,需将别的空间设置成默认,再删除users表空间(oracle不允许删除默认空间的). 2.删除表空间的同时会报这样的错:ORA-22868错误.原因:推断应该存 ...