Codeforces 468B sets and set deformation

Source: Internet
Author: User
Tags printf

Title Link: Http://codeforces.com/problemset/problem/468/B

The main topic: give you n different integers, and then give two integers, a, B, ask whether the n number can be divided into two parts, so that in a this set of any number of AI, there are A-ai also in a this set, the same with the B set. If there is no reasonable division method, output no, otherwise output yes, and lose to a reasonable grouping.

Problem Solving Ideas:

First we assume that there is a reasonable scheme, then there is one for either number x, which is definitely in the A/b collection. At the same time, if the scheme is reasonable, the final result is no intersection of the elements in the set.

1. For a number x, if the a-x does not exist, then x must be in Group B (assuming the group is formed), then the X and B are grouped together, if at the same time the b-x also does not exist, then the X and a are grouped together, at this time A and B are grouped together, obviously this scheme is unreasonable.

2. For a number x, if the a-x exists, then x and a-x corresponding subscript together, it is not possible to say that x belongs to set a, because there may be n=2,a=8,b=20,4,16, such data, only if the b-x does not exist, only the X and a subscript together.

3, if the b-x also exist, the X and B-x also the subscript also up, at this time if X. =a-x. =b-x, it is also unreasonable to note that A and B sets are common x, that is, the scheme A and B are combined. At this point if x==a-x. =b-x, obviously should be grouped into B, of course, the exception is a and b are equal, at this time any grouping can be.

The overall situation should be these kinds.

For the code, it feels good to have the following points:

1,map mapping makes it easier to find pairs of numbers.

2, virtual out two A, a set of root nodes n+1 and n+2;

3, and check the special treatment of the set;

The code is as follows:

#include <stdio.h> #include <cstring> #include <map> #include <algorithm> using namespace std;
#define N 100005 int f[n],num[n];
Map <int,int> MP;
    int find (int x) {return x!=f[x]?f[x]=find (F[x]): f[x];} void merge (int x,int y) {int fx=find (x);
    int Fy=find (y);
    if (fx!=fy) f[fx]=fy;
Return
    } int main () {int n,a,b;
    scanf ("%d%d%d", &n,&a,&b);
    for (int i=1;i<=n+2;i++) f[i]=i;
        for (int i=1;i<=n;i++) {scanf ("%d", &num[i]);
    Mp[num[i]]=i;
        } for (int i=1;i<=n;i++) {if (Mp[a-num[i]) merge (I,mp[a-num[i]);
        else merge (i,n+2);
        if (Mp[b-num[i]]) merge (I,mp[b-num[i]]);
    else merge (i,n+1);
    } if (Find (n+1) ==find (n+2)) printf ("no\n");
        else {printf ("yes\n");
            for (int i=1;i<=n;i++) {if (Find (i) ==find (n+1)) printf ("0");
        else printf ("1");
    } printf ("\ n"); }
    return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.