[codeforces741C] Arpa’s overnight party and Mehrdad’s silent entering

time limit per test : 1 second
memory limit per test : 256 megabytes

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 n n pairs of friends sitting around a table. i i i-th pair consisted of a boy, sitting on the a i a_i ai-th chair, and his girlfriend, sitting on the b i b_i bi-th chair. The chairs were numbered 1 1 1 through 2 n 2n 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 2 n 2n 2n and 1 1 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     ≤     1 0 5 ) n (1  ≤  n  ≤  10^5) n(1n105) — the number of pairs of guests.

The i i i-th of the next n lines contains a pair of integers a i a_i ai and b i ( 1     ≤   a i ,   b i   ≤     2 n ) b_i (1  ≤ a_i, b_i ≤  2n) bi(1ai,bi2n) — the number of chair on which the boy in the i i 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 -1 1.

Otherwise print n n n lines, the i i i-th of them should contain two integers which represent the type of food for the i i 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 1 1, otherwise print 2 2 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

题意:
给定n个关系(ai,bi),表示(ai,bi)之间是情侣关系,情侣的颜色不能相同,相邻的三个人的颜色不能相同,1和2*n是相邻的。你要将所有人染色(一共两种颜色),求一组染色方案。

题解:
首先,如果按照12121212…这样染色的话,在没有情侣关系的情况下肯定是可以的。然后如果两个点之间有情侣关系,则他们之间连一条边。然后将所有的 2 ∗ i 2*i 2i 2 ∗ i + 1 2*i+1 2i+1连边,然后跑一次图染色就行。

#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int ne,h[200004],cl[200004];
int u[200004],v[200004],n;
struct edge{
    int to,nt;
}e[400004];
void add(int u,int v){
     e[++ne].to=v;e[ne].nt=h[u];
     h[u]=ne;
}
int dfs(int x,int c){
    cl[x]=c;
    for(int i=h[x];i;i=e[i].nt){
        if(!cl[e[i].to]){
            bool t=dfs(e[i].to,3-c);
            if(!t)return 0;
        }
        else if(cl[e[i].to]==cl[x]){
            return 0;
        }
    }
    return 1;
}
int LiangJiaJun(){
    ne=0;
    memset(cl,0,sizeof(cl));
    memset(h,0,sizeof(h));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&u[i],&v[i]);
        add(u[i],v[i]);
        add(v[i],u[i]);
    }
    for(int i=1;i<=n;i++){
        add(i*2,i*2-1);
        add(i*2-1,i*2);
    }
    for(int i=1;i<=2*n;i++){
        if(!cl[i]){
            if(!dfs(i,1))return puts("-1"),0;
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d %d\n",cl[u[i]],cl[v[i]]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值