#CodeForces CF741C Arpa’s overnight party and Mehrdad’s silent entering —— 二部图问题(构造、染色)

原题:

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 1 through 2n in clockwise direction. There was exactly one person sitting on each chair.

 #CodeForces CF741C Arpa’s overnight party and Mehrdad’s silent entering ——  二部图问题(构造、染色)

 

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.

#CodeForces CF741C Arpa’s overnight party and Mehrdad’s silent entering ——  二部图问题(构造、染色)

 

 粗糙翻译(觉得解释不清楚的还是去看一下原题吧):

  一共有n对男男女女,所以一共有2n个人,这2n个人分布在一个圆桌上,圆桌一共有2n个座位,现有不同类型的食物A与B,要求一对情侣不能吃同样类型的食物,围坐在桌旁的连续三个人也至少应该有两人吃的是不一样类型的食物,请你写出满足这两个条件的一种方法

输入内容:第一行是情侣的对数,下面是情侣分坐的位置;

输出内容:分别输出这些情侣该吃哪种类型的食物,用1对应食物A,2对应食物B

 

思想:

      出题人手下留情了,写对应的一种方案即可,所以对于这两个条件只要取一个相对更小一点的范围(特例)即可。如果我们用一根连线来表示两人吃的是不同类型的食物,那么对于“情侣吃不同”这个条件我们只需要将所有的情侣都连好线,然后我们再讨论条件“三人不尽相同”,对于这个条件就像我说的,可以取他的一个特例,我们来分析:

这三个人的可能情况是AAB ABB ABA(请他情况和这仨类似就不一一列举了),出现的情况就是”每个结点至少有一根线与其他结点连接——结点度数至少为1“,也就说明”每个结点与他的相邻结点间至少存在一根无向线“。【稍微解释一下,AAB中间的A是和B连线了(一条),ABB中间的B是和A连线了(一条),ABA中间的B是和两边的A都连线了(两条)。】根据这个条件我们可以推一个相对特例的条件:既然我们让三个不尽连线,那么我们不妨2i和2i-1不连线,其他都连,这样可以保证是一个无向图的极大匹配(每个点度数都为1),这样就能保证三个点不尽连线。

这时候会有一个问题:这两个条件(男女连线,三人不尽相同)都是独立完成的条件,他们是否会有影响?比如因为有了第二个条件成立导致第一个条件产生矛盾不成立呢?好问题,但是答案是不会。

首先这是一个二部图问题,也可以说是0-1黑白染色的问题,能保证我们连接之后的无向图的每一个顶点的度数都是2,保证了都是简单环。如何保证条件二和条件一不相悖?因为一定没有奇环:我连第二个条件的边的时候,如果我连的这对点之前已经相连,那一定是通过 k 条情侣边和 k-1 条第二个条件边,也就是说加上我即将连的第二个条件边之后,这肯定是个偶环。

 

题解:

        

 

 

上一篇:Vue3 composition API - (Why 、How & When)


下一篇:Day 1 计算机硬件组成 Computer hardware composition