codeforces 742E (二分图着色)
2016-12-14 16:46
603 查看
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+5; pair<int,int>p[MAXN]; vector<int>G[MAXN<<1]; int color[MAXN<<1],n; bool bipartite(int u) { for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(color[v]==color[u])return 0; if(!color[v]) { color[v]=3-color[u]; if(!bipartite(v))return 0; } } return 1; } int main() { scanf("%d",&n); for(int i=1,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=1;i<=n;i++) { G[i<<1].pb(i<<1^1); G[i<<1^1].pb(i<<1); } for(int i=1;i<=2*n;i++) if(!color[i])color[i]=1,bipartite(i); for(int i=1;i<=n;i++) printf("%d %d\n",color[p[i].fi],color[p[i].se]); return 0; }
相关文章推荐
- [矩阵][高斯消元][二分图]Codeforces 736D.Permutations
- CodeForces 557D Vitaly and Cycle(二分图求解)
- 【CodeForces - 687A NP-Hard Problem】 二分图染色
- Codeforces Beta Round #27 (Codeforces format, Div. 2) D Ring Road 2(二分图染色版)
- codeforces 557D Vitaly and Cyclef(二分图染色)
- [csu1508 地图的四着色]二分图染色
- [二分图 图的直径] Codeforces 542E VK Cup 2015 - Round 3 E.Playing on Graph
- codeforces 862B (二分图 dfs )之 Mahmoud and Ehab and the bipartiteness
- HDU 5313 Bipartite Graph (二分图着色,dp)
- [二分图]Codeforces 19E. Fairy
- [最长路][二分图]Codeforces 542 E. Playing on Graph
- CodeForces 687A--判断二分图
- 二分图判定 (黑白二着色法)
- [扫描线 二分图最大匹配 线段树优化网络流] Codeforces 793G Tinkoff Challenge - Elimination Round G. Oleg and chess
- Codeforces 557D - Vitaly and Cycle [二分图染色]【图论】
- codeforces 387D George and Interesting Graph(二分图最大匹配)
- codeforces 688c 二分图判断
- CodeForces 688C NP-Hard Problem(二分图判断)
- NP-Hard Problemd(二分图判定着色)
- codeforces 687A NP-Hard Problem(DFS二分图判定)