链接

B. Two Sets

题意

给两个集合A B,两个数a b,n个数x,分配n个数到两个集合,要求x , a-x在同一个集合,x , b-x在同一个集合,属于A集合的数输出0,B的输出1,无解输出NO,空集也算一个集合,所以存在所有数都在一个集合。

做法

这有一个样例

input
6 5 9
1 2 3 4 5 8
output
YES
1 0 0 1 1 1

[1 4]A,[4 5]B,[1,8]B,所以只能把1 4都放进B集合

首先,能在A里面处理的就放入A,否则放入B,能在B里面处理的就放入B,否则放入A,如果x只能在B出现然而b-x在A里面则把b-x拿来B,你是不是担心把b-x从A里拿掉了,那a-(b-x)岂不是没有配对了?如果是这样的话那就是不合法的呗,不然后面还会把a-(b-x)从A中拿给B的,如上面的样例。如果两个集合里有重复元素则不合法。

代码

#include<bits/stdc++.h>
using namespace std;
#define M 111000
int fa[M], num[M];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main() {
int n, a, b;
while (cin >> n >> a >> b) {
map<int, int> mp;
for (int i = 1; i <= n; i++) {
cin >> num[i];
mp[num[i]] = i;
}
for (int i = 1; i <= n + 2; i++)
fa[i] = i;
int x;
for (int i = 1; i <= n; i++) {
x = find(i);
if (mp[a - num[i]]) fa[x] = find(mp[a - num[i]]);//如果a-num[i]存在的话,则把num[i]和a-num[i]放进a集合
else fa[x] = find(n + 1);//否则放在b集合
x = find(i);//为什么不直接用上面的x,因为合并后fa[x]已经变化了
if (mp[b - num[i]]) fa[x] = find(mp[b - num[i]]);//如果b-num[i]存在的话,则把num[i]和b-num[i]放进b集合
else fa[x] = find(n + 2);//否则放在a集合
}
if (find(n + 1) == find(n + 2))//如果两个集合有重复元素则不合法
cout << "NO\n";
else {
cout << "YES\n";
printf("%d", find(1) == find(n + 1));
for (int i = 2; i <= n; i++)
printf(" %d", find(i) == find(n + 1));
cout << endl;
}
}
return 0;
}

codeforces 468B two set(并查集)的更多相关文章

  1. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

  2. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  3. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  4. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  5. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

  6. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  7. Codeforces 650C Table Compression (并查集)

    题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...

  8. CodeForces - 893C Rumor【并查集】

    <题目链接> 题目大意: 有n个人,其中有m对朋友,现在你有一个秘密你想告诉所有人,第i个人愿意出价a[i]买你的秘密,获得秘密的人会免费告诉它的所有朋友(他朋友的朋友也会免费知道),现在 ...

  9. CodeForces 566D Restructuring Company (并查集+链表)

    题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...

随机推荐

  1. Linux挂载NAS共享文件夹

    [root@ftp:/mnt] > mount -o username=user01,password=1234567890 //192.168.31.20/share /mnt/nas [ro ...

  2. Nexus私服的搭建

    1.nexus 介绍     是开源的,用该框架架设maven私有服务器   2.nexus私服环境搭建     把nexus.war包放到tomcat的webapps下面     浏览且登录     ...

  3. ListViewItem中的图片不能动态改变的解决方法

    近期遇到了一个问题,就是我的listviewitem中有个图片,点击的时候须要变成还有一种图片.结果在getView()中设置了响应.可是能够运行.就是不起作用.在网上查了非常多资料也没有解决.最后发 ...

  4. Android ListView拉到顶/底部,像橡皮筋一样弹性回弹复位

     <Android ListView拉到顶/底部,像橡皮筋一样弹性回弹复位> Android本身的ListView拉到顶部或者底部会在顶部/底部边缘间隙出现一道"闪光&quo ...

  5. 拒绝switch,程序加速之函数指针数组

    先看一个使用switch语句的程序: #include <stdio.h> #include <time.h> //加法 int add(int a,int b) { retu ...

  6. Codeforces Round #200 (Div. 2)D. Alternating Current (堆栈)

    D. Alternating Current time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. 什么是A记录、MX记录、CNAME记录具体介绍

    什么是A记录: A (Address) 记录是用来指定主机名(或域名)相应的IP地址记录.用户能够将该域名下的站点服务器指向到自己的web server上. 同一时候也能够设置域名的子域名. 通俗来说 ...

  8. JFinal Starting scanner at interval of 5 seconds.报错

    Starting JFinal 2.0 Starting scanner at interval of 5 seconds. Starting web server on port: 80 Excep ...

  9. 【POI 2007】 山峰和山谷

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1102 [算法] 广度优先搜索 [代码] #include<bits/stdc+ ...

  10. EOJ 3031 二进制倒置

    题目描述 给定一个整数 n(0≤n≤10100).将 n 的 334 位二进制表示形式(不包括开头可能的值为 0 的位,n=0 表示为 1 位 0)前后倒置,输出倒置后的二进制数对应的整数. 例如:n ...