codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)

来源:互联网 发布:淘宝店铺过户近亲属 编辑:程序博客网 时间:2024/04/25 20:32


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 sawn pairs of friends sitting around a table.i-th pair consisted of a boy, sitting on theai-th chair, and his girlfriend, sitting on thebi-th chair. The chairs were numbered1 through 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 chairs2n 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 andbi (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 thei-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, print1, otherwise print 2.

If there are multiple solutions, print any of them.

Example
Input
31 42 53 6
Output
1 22 11 2


题目大意:有2n个人围成一圈坐在桌子边上,每个人占据一个位子,对应这2n个人是n对情侣,要求情侣不能吃同一种食物,并且桌子上相邻的三个人的食物必须有两个人是不同的,只有两种食物(1或者是2),问一种可行分配方式。


思路:


1、相邻的三个人需要有两个人的食物是不同的,那么其实如果我们只要保证每两个人之间的食物种类是不同的即可。

那么对应我们建立无向边:

(i*2,i*2-1);

因为每对情侣我们也需要要求食物种类是不同的,那么对应我们还要建立无向边:
(输入进来的第一个编号,输入进来的第二个编号);


2、很容易发现,因为一共只有两种食物,那么整个图是一个二分图的模型,对应我们接下来只需要对每个点进行染色即可。

这里Dfs实现。


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int N = 2e5 + 100;vector<int>V[N];int a[N][2],vis[N],col[N];void dfs(int rt,int color){for(int i=0;i<V[rt].size();i++) {int son=V[rt][i];if(vis[son]) continue;vis[son]=1;col[son]=(3^color);dfs(son,3^color);}}int main(){ios::sync_with_stdio(false);int n,i,j;cin>>n;for(i=1;i<=n;i++) {cin>>a[i][0]>>a[i][1];V[a[i][0]].push_back(a[i][1]);V[a[i][1]].push_back(a[i][0]);}for(i=1;i<2*n;i=i+2) {V[i].push_back(i+1);V[i+1].push_back(i);}for(i=1;i<=2*n;i++) {if(vis[i]) continue;vis[i]=1;    col[i]=1;    dfs(i,1);}for(i=1;i<=n;i++) cout<<col[a[i][0]]<<" "<<col[a[i][1]]<<endl;return 0;}















0 0
原创粉丝点击