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.
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).
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).
4 5 9 2 3 4 5
YES 0 0 1 1
3 3 4 1 2 4
NO
思路:2-sat、注意对于不能放在集合a的,连一条边[2*i,2*i+1]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <queue> using namespace std; #define maxn 400080 int first[maxn]; int vv[maxn],nxt[maxn],S[maxn]; int e,c,n; bool vis[maxn]; int key[maxn],X[maxn]; void init() { e = 0; memset(first,-1,sizeof(first)); memset(vis,0,sizeof(vis)); } void AddEdge(int u,int v) { vv[e] = v; nxt[e] = first[u]; first[u] = e++; } bool dfs(int u)//现在要将u置1 { if(vis[u^1]) return 0; if(vis[u]) return 1; vis[u] = 1; S[c++] = u; for(int i = first[u];i != -1;i = nxt[i]) { int v = vv[i]; if(!dfs(v)) return false; } return true; } bool Judge() { for(int i = 0;i < n*2;i+=2) { if(!vis[i] && !vis[i+1]) { c = 0; if(!dfs(i)) { while(c > 0) vis[S[--c]] = 0; if(!dfs(i+1)) return false; } } } return true; } int main() { //freopen("in.txt","r",stdin); int a,b; while(scanf("%d%d%d",&n,&a,&b)==3) { for(int i = 0;i < n;i++) scanf("%d",&key[i]); for(int i = 0;i < n;i++) X[i] = key[i]; init(); sort(key,key+n); for(int i = 0;i < n;i++) { int fucka = a - key[i]; int fuckb = b - key[i]; int posa = lower_bound(key,key+n,fucka) - key; int posb = lower_bound(key,key+n,fuckb) - key; if(posa >= 0 && posa < n && fucka == key[posa]) { AddEdge(i*2,posa*2); AddEdge(i*2+1,posa*2+1); } if(posa >= n || fucka != key[posa]) { AddEdge(i*2,i*2+1); } if(posb >= 0 && posb < n && fuckb == key[posb]) { AddEdge(i*2+1,posb*2+1); AddEdge(i*2,posb*2); } if(posb >= n || fuckb != key[posb]) { AddEdge(i*2+1,2*i); } } if(!Judge()) { printf("NO\n"); } else { printf("YES\n"); bool flag = false; for(int i = 0;i < n;i++) { if(flag) cout << " "; flag = 1; int pos = lower_bound(key,key+n,X[i])-key; if(vis[pos<<1]) cout << 0; else cout << 1; } cout << endl; } } return 0; }