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.
InputThe 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).
OutputIf there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n
If it‘s impossible, print "NO" (without the quotes).
Examples Input4 5 9Output
2 3 4 5
YESInput
0 0 1 1
3 3 4Output
1 2 4
NONote
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