zoukankan      html  css  js  c++  java
  • Codeforces Round #383 (Div. 1) C. 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 a**i-th chair, and his girlfriend, sitting on the b**i-th chair. The chairs were numbered 1through 2n in clockwise direction. There was exactly one person sitting on each chair.

    img

    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 a**i and b**i (1  ≤ a**i, b**i ≤  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

    Copy

    3
    1 4
    2 5
    3 6
    

    output

    Copy

    1 2
    2 1
    1 2
    

    二分图染色问题。首先很容易看出来情侣之间要连边,关键在于相邻的三个人怎么连。可以发现相邻三个人一定会有某两个人之间连,所以进行如下构造:第2 * i个人和第2 * i- 1个人之间连边,即相邻点对隔一对连一条。至于为什么要这么连,是因为最终形成的都是简单环。而且可以发现这样连图中没有奇环,因为一个点的度数最多为2(为1的情况是相邻的是情侣),环里加进去的一定是一对一对的。最终进行染色即可。

    #include <iostream>
    #include <vector>
    using namespace std;
    int n, tot = 0, head[200005], ver[400005], Next[400005], food[200005] = { 0 };
    void add(int x, int y)
    {
        ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
    }
    bool flag = 1;
    struct Pair
    {
        int x, y;
    };
    vector<Pair> v;
    void dfs(int now, int f)
    {
        food[now] = f;
        for(int i = head[now]; i; i = Next[i])
        {
            int y = ver[i];
            if(food[y]) 
            {
                if(food[y] == food[now]) 
                {
                    flag = 0;
                    return;
                }
                continue;
            }
            dfs(y, 3 - f);
        }
    }
    int main()
    {
        freopen("d.txt", "r", stdin);
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            int b, g;
            cin >> b >> g;
            add(b, g);
            add(g, b);
            Pair tmp = {b, g};
            v.push_back(tmp);
        }
        for(int i = 1; i <= n; i++)
        {
            add(2 * i, 2 * i - 1);
            add(2 * i - 1, 2 * i);
        }
        //dfs(1, 1);
        for(int i = 1; i <= 2 * n; i++)
        {
            if(!food[i])
            {
                if(i & 1) dfs(i, 1);
                else dfs(i, 2);
            }
        }
        if(!flag)
            cout << -1;
        else
        {
            for (int i = 0; i < v.size(); i++)
            {
                cout << food[v[i].x] << ' ' << food[v[i].y] << endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    操作~拷贝clone()
    属性/css~储存
    属性/css~位置.offset()&offsetParent()&position()&scrollTop()&scrollLeft()
    属性/css~尺寸.height()
    设置,获取,删除cookie方法
    Cookie
    属性/css~css.css()
    Python井字游戏
    Asp.net vNext 学习之路(三)
    使用C#发送Http 请求实现模拟登陆(以博客园为例)
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/14077208.html
Copyright © 2011-2022 走看看