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的

            如果有自环,则一定可以,将那个点拿出来,剩下的偶数个点两两匹配,否则一定不可以

            然后注意一下细节就好了~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
         #include 
         
           using namespace std; const int MAXN = 100010; int p[MAXN]; map 
          
            mp; int isrin[MAXN]; int ans[MAXN]; int vis[MAXN]; int last; int sz; int a,b; vector 
           
             q[MAXN]; void dfs(int u,int p) { if(vis[u])return; vis[u]=1; sz++; last=u; for(int i=0;i 
             
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值