# 并查集做题总结

CF 469B

//CodeForces 469D
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
typedef long long ll;
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 200010;
int ans, n ,x, y, a[N], b[N], f[N];
map<int,int> mp;
int find(int k){
if (f[k]==k)
return k;
else
return find(f[k]);
}
void merge(int x,int y){
int fx, fy;
fx = find(x);
fy = find(y);
if (fx < fy)
f[fy] = fx;
else
f[fx] = fy;
}
int main() {
cin >> n >> x >> y;
rep (i, 1, n){
scanf("%d",&a[i]);
mp[a[i]] = i;
}
rep (i,1,n+2){
f[i] = i;
}
rep (i, 1, n){
if (mp.count(x-a[i])==1){//如果存在都在A集合，那么这两个节点合并
merge(i,mp[x-a[i]]);
}else{//否则合并到n+1(B)集合里
merge(i,n+1);
}
if (mp.count(y-a[i])==1){//都在B集合
merge(i,mp[y-a[i]]);
}else{
merge(i,n+2);//合并到n+2（A）集合里
}
}
if (find(n+1) == find(n+2)){//如果成立则这两个点应该不会冲突
puts("NO");
return 0;
}
puts("YES");
int anc = find(n+1);
rep (i,1,n){
//cout << find(i) << endl;
cout << (find(i)==anc) << " ";
}
puts("");
return 0;
}


posted @ 2017-06-15 16:20 ohazyi 阅读(...) 评论(...) 编辑 收藏