CF469D Two Set (并查集)

时间:2023-03-09 04:32:20
CF469D Two Set (并查集)

Codeforces Round #268 (Div. 2)D

Codeforces Round #268 (Div. 1)B

CF468B

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

题意:

给出n个数和a和b,要求x在集合A的话,a-x也要在集合A;x在集合B的话,b-x也要在集合B。求出可行的集合分配,或输出无解。

题解:

并查集。

x和a-x肯定同时在A集合或者同时在B集合(x在B集合的话a-x肯定不能在A集合),也就是x和x-a会在同一集合,b-x同理。

当没有x-b时,x肯定在A集合;没有x-a时同理;两个都没有是无解的一种情况。

并查集狂撸。

细节多,我怕了。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("huzhi.txt","w",stdout)
#define mp make_pair
#define pb push_back int d[];
int c[];
int f[];
int n,ab[]; map<int,int>S; int getfather(int x){
int t=f[x]!=x?getfather(f[x]):x;
f[x]=t;
return t;
}
void comb(int x,int y){
f[getfather(y)]=getfather(x);
} int gank(int x) {
int q=d[x];
int xa=S[ab[]-q];
int xb=S[ab[]-q];
if(xa!= && xb!=){
int fx=getfather(x);
int fxa=getfather(xa);
int fxb=getfather(xb);
if(fx>n && fxa>n && fx!=fxa)return -;
if(fx!=fxa) fx>fxa?comb(fx,fxa):comb(fxa,fx);
fx=getfather(x);
if(fx>n && fxb>n && fx!=fxb)return -;
if(fx!=fxb) fx>fxb?comb(fx,fxb):comb(fxb,fx);
}else if(xa!=){
int fx=getfather(x);
int fxa=getfather(xa);
if(fx==n+ || fxa==n+)return -;
if(fx!=n+)comb(n+,fx);
if(fxa!=n+)comb(n+,fxa);
}else if(xb!=){
int fx=getfather(x);
int fxb=getfather(xb);
if(fx==n+ || fxb==n+)return -;
if(fx!=n+)comb(n+,fx);
if(fxb!=n+)comb(n+,fxb);
}else return -;
return ;
} bool farm() {
if(ab[]==ab[])ab[]=1e9*+;
int i,j;
mf1(c);
FOR(i,,n+)f[i]=i;
///n+1是setA,n+2是setB
FOR(i,,n) {
if(gank(i)==-)return ;
//printf("%d f[1]=%d\n",i,getfather(1));
}
set<int>A;
int b[],r=;
A.clear();
int cnt=;
FOR(i,,n){
int fi=getfather(i);
if(fi!=n+ && fi!=n+){
int xa=S[ab[]-d[i]];
int xb=S[ab[]-d[i]];
if(xb!= && (getfather(xb)==n+ || i==xa))comb(n+,fi);
else if(xa!= && (getfather(xa)==n+ || i==xb)) comb(n+,fi);
else return ;
}
//printf("f[%d] = %d\n",i,f[i]);
}
return ;
} int main() {
int i;
RD3(n,ab[],ab[]);
FOR(i,,n) {
RD(d[i]);
S[d[i]]=i;
}
if(farm()) {
puts("YES");
if(n>)printf("%d",getfather()-n-);
FOR(i,,n)printf(" %d",getfather(i)-n-);
} else puts("NO");
return ;
}