zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    第一条博客
    (原创)c#学习笔记08--面向对象编程简介01--面向对象编程的含义01--对象的含义01--属性和字段
    (原创)c#学习笔记06--函数06--委托
    (原创)c#学习笔记06--函数05--函数的重载
    (原创)c#学习笔记06--函数04--结构函数
    (原创)c#学习笔记06--函数03--main函数
    (原创)c#学习笔记06--函数02--变量的作用域02--参数和返回值与全局数据
    (原创)c#学习笔记06--函数02--变量的作用域01--其他结构中变量的作用域
    (原创)c#学习笔记06--函数01--定义和使用函数02--参数
    (原创)c#学习笔记06--函数01--定义和使用函数01--返回值
  • 原文地址:https://www.cnblogs.com/zhber/p/7163830.html
Copyright © 2011-2022 走看看