程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces Round #268 (Div. 1)B(dfs)

Codeforces Round #268 (Div. 1)B(dfs)

編輯:C++入門知識

Codeforces Round #268 (Div. 1)B(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的
    如果有自環,則一定可以,將那個點拿出來,剩下的偶數個點兩兩匹配,否則一定不可以
    然後注意一下細節就好了~

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved