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 ...
随机推荐
- php服务生成exe附件下载
<?php $realname = "zhuast.exe"; //原始文件名 $filename = "中文 文件名.exe"; //下载到浏览器的文件 ...
- SOA Integration Repository Error:Service Provider Access is not available.
在Oracle EBS Integration Repository中,打开一个Webservice,报了一个警告. 英文: Warning Service Provider Access is no ...
- 1、基于MFC的OpenGL程序
首先,使用的库是GLUT以及GLAUX,先下载两者,添加查找路径以及链接 一.单文本文件 工程openGLMFC 1.创建单文本文件 2.添加路径.链接 方法如之前篇章所示, 链接库为op ...
- 0,SFDC 开发篇 - 开发框架和APEX语法
1, 开发环境 Your Name | Developer Console | Debug | Open Execute Anonymous Window 输入hello world,并点击Excut ...
- HandlerThread和IntentService
HandlerThread 为什么要使用HandlerThread? 我们经常使用的Handler来处理消息,其中使用Looper来对消息队列进行轮询,并且默认是发生在主线程中,这可能会引起UI线程的 ...
- 如何将ASP.NET MVC所有参数均自动设置为默认
今天看到CSDN上有个问题觉得有点意思:"可不可以ASP.NET MVC所有参数均自动设置为默认" public class HomeController : Controller ...
- re正则表达式
1.matchimport rem = re.match("[0-9]{0,10}","7564565abcdefg") #match从开头开始匹配if m: ...
- 升级SSH
本例使用CentOS6.6 原SSH版本为5.3P1 一.准备好需要升级到的新版本的软件包 openssh下载地址:http://www.openssh.com/portable.html#http ...
- 如何用grunt压缩文件
grunt-cli 全局装完之后,就可以给每个项目装grunt了. 1.先把package.json和Gruntfile.js拷到项目下(PS:这两个文件是每个项目装grunt的时候必带的) 2. ...
- 00904. 00000 - "%s: invalid identifier"
SELECT * FROM table name WHERE STRING_VALUE like '%Britaney Searing%' ORDER BY COMPOSITE_INSTANCE_ID ...