设为首页 加入收藏

TOP

Codeforces Round #268 (Div. 1)B(dfs)
2015-07-20 17:37:51 来源: 作者: 【 】 浏览:3
Tags:Codeforces Round #268 Div. dfs
B. Two Sets time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Little X has n distinct integers: p1,?p2,?...,?pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied:

  • If number x belongs to set A, then number a?-?x must also belong to set A.
  • If number x belongs to set B, then number b?-?x must also belong to set B.

    Help Little X divide the numbers into two sets or determine that it's impossible.

    Input

    The first line contains three space-separated integers n,?a,?b (1?≤?n?≤?105; 1?≤?a,?b?≤?109). The next line contains n space-separated distinct integers p1,?p2,?...,?pn (1?≤?pi?≤?109).

    Output

    If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1,?b2,?...,?bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B.

    If it's impossible, print "NO" (without the quotes).

    Sample test(s) input
    4 5 9
    2 3 4 5
    
    output
    YES
    0 0 1 1
    
    input
    3 3 4
    1 2 4
    
    output
    NO
    
    Note

    It's OK if all the numbers are in the same set, and the other one is empty.


    题意:RT
    思路:据说这题的解法有很多,说一种比较简单的方法
    如果两个数字相加等于a或b,就在它们之间连一条边
    所以对于每个点,度数最多为2
    那么每个点在图中只有两种情况,要么属于一个简单环,要么属于一条链
    现在就只判断环或链里面点的数量,如果为偶数一定可以两两匹配
    如果为奇数,则要分情况,因为可能有的点有自环,因为自己是可以和自己相加属于a或b的
    如果有自环,则一定可以,将那个点拿出来,剩下的偶数个点两两匹配,否则一定不可以
    然后注意一下细节就好了~
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU1429 胜利大逃亡(续) BFS +简.. 下一篇hdu 2072 单词数(STL set写法)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·利用python进行数据 (2025-12-25 20:49:22)
·如何使用 python 中 (2025-12-25 20:49:19)
·零基础如何学爬虫技 (2025-12-25 20:49:17)
·Java 并发工具类:提 (2025-12-25 20:25:44)
·Java面试技巧:如何 (2025-12-25 20:25:41)