cf468B Two Sets

Posted zhber

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf468B Two Sets相关的知识,希望对你有一定的参考价值。

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).

Examples
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.

 

如果A中有了一个x,那么A中也要有a-x,说明(逆否命题)如果A中没有a-x,也就没有x。即如果B中有a-x,就有x。因为不在A就在B咯

所以这个A还是B无所谓的,重要的是x和a-x一定在同一集合,x和b-x一定在同一集合

因此裸并查集,只要被并起来的数字有一个不能在A,那么这一群都不能在A

技术分享
 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 LL n,a,b;
 5 inline LL read()
 6 {
 7     LL x=0,f=1;char ch=getchar();
 8     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
 9     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
10     return x*f;
11 }
12 struct po{LL x;int rnk;}p[100010];
13 bool operator <(po a,po b){return a.x<b.x;}
14 int fa[100010];
15 int mrk[100010];
16 inline int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
17 map<LL,LL>mp;
18 int main()
19 {
20     n=read();a=read();b=read();
21     for (int i=1;i<=n;i++)
22     {
23         p[i].x=read();
24         p[i].rnk=i;
25     }
26     sort(p+1,p+n+1);
27     for(int i=1;i<=n;i++)mp[p[i].x]=p[i].rnk,fa[i]=i,mrk[i]=3;
28     for(int i=1;i<=n;i++)
29     {
30         if (mp[a-p[i].x])
31         {
32             int pos=getfa(mp[a-p[i].x]),pos2=getfa(p[i].rnk);
33             if (pos!=pos2)fa[pos2]=pos;
34         }
35         if (mp[b-p[i].x])
36         {
37             int pos=getfa(mp[b-p[i].x]),pos2=getfa(p[i].rnk);
38             if (pos!=pos2)fa[pos2]=pos;
39         }
40     }
41     for (int i=1;i<=n;i++)
42     {
43         int ff=getfa(p[i].rnk);
44         if (!mp[a-p[i].x])mrk[ff]&=1;
45         if (!mp[b-p[i].x])mrk[ff]&=2;
46     }
47     for (int i=1;i<=n;i++)
48         if (mrk[getfa(i)]==0){puts("NO");return 0;}
49         else if (mrk[getfa(i)]==3)mrk[getfa(i)]=1;
50     puts("YES");
51     for (int i=1;i<=n;i++)printf("%d ",mrk[getfa(i)]==2?0:1);
52     puts("");
53 }
cf468B

 




以上是关于cf468B Two Sets的主要内容,如果未能解决你的问题,请参考以下文章

Two Sets CodeForces - 468B

CodeForces468B Two Sets 解题报告

codeforces 468B two set(并查集)

codeforces 468B two set(并查集)

CF 1042F Leaf Sets

Coursera Algorithms week2 基础排序 Interview Questions: 1 Intersection of two sets