CF469D Two Set (并查集)
Codeforces Round #268 (Div. 2)D
Codeforces Round #268 (Div. 1)B
CF468B
|
D. Two Sets
time limit per test
1 second memory limit per test
256 megabytes input
standard input output
standard output 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:
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). Sample test(s)
input
4 5 9 output
YES input
3 3 4 output
NO Note
It's OK if all the numbers are in the same set, and the other one is empty. |
题意:
给出n个数和a和b,要求x在集合A的话,a-x也要在集合A;x在集合B的话,b-x也要在集合B。求出可行的集合分配,或输出无解。
题解:
并查集。
x和a-x肯定同时在A集合或者同时在B集合(x在B集合的话a-x肯定不能在A集合),也就是x和x-a会在同一集合,b-x同理。
当没有x-b时,x肯定在A集合;没有x-a时同理;两个都没有是无解的一种情况。
并查集狂撸。
细节多,我怕了。
代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define mf1(array) memset(array, -1, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("huzhi.txt","w",stdout)
#define mp make_pair
#define pb push_back int d[];
int c[];
int f[];
int n,ab[]; map<int,int>S; int getfather(int x){
int t=f[x]!=x?getfather(f[x]):x;
f[x]=t;
return t;
}
void comb(int x,int y){
f[getfather(y)]=getfather(x);
} int gank(int x) {
int q=d[x];
int xa=S[ab[]-q];
int xb=S[ab[]-q];
if(xa!= && xb!=){
int fx=getfather(x);
int fxa=getfather(xa);
int fxb=getfather(xb);
if(fx>n && fxa>n && fx!=fxa)return -;
if(fx!=fxa) fx>fxa?comb(fx,fxa):comb(fxa,fx);
fx=getfather(x);
if(fx>n && fxb>n && fx!=fxb)return -;
if(fx!=fxb) fx>fxb?comb(fx,fxb):comb(fxb,fx);
}else if(xa!=){
int fx=getfather(x);
int fxa=getfather(xa);
if(fx==n+ || fxa==n+)return -;
if(fx!=n+)comb(n+,fx);
if(fxa!=n+)comb(n+,fxa);
}else if(xb!=){
int fx=getfather(x);
int fxb=getfather(xb);
if(fx==n+ || fxb==n+)return -;
if(fx!=n+)comb(n+,fx);
if(fxb!=n+)comb(n+,fxb);
}else return -;
return ;
} bool farm() {
if(ab[]==ab[])ab[]=1e9*+;
int i,j;
mf1(c);
FOR(i,,n+)f[i]=i;
///n+1是setA,n+2是setB
FOR(i,,n) {
if(gank(i)==-)return ;
//printf("%d f[1]=%d\n",i,getfather(1));
}
set<int>A;
int b[],r=;
A.clear();
int cnt=;
FOR(i,,n){
int fi=getfather(i);
if(fi!=n+ && fi!=n+){
int xa=S[ab[]-d[i]];
int xb=S[ab[]-d[i]];
if(xb!= && (getfather(xb)==n+ || i==xa))comb(n+,fi);
else if(xa!= && (getfather(xa)==n+ || i==xb)) comb(n+,fi);
else return ;
}
//printf("f[%d] = %d\n",i,f[i]);
}
return ;
} int main() {
int i;
RD3(n,ab[],ab[]);
FOR(i,,n) {
RD(d[i]);
S[d[i]]=i;
}
if(farm()) {
puts("YES");
if(n>)printf("%d",getfather()-n-);
FOR(i,,n)printf(" %d",getfather(i)-n-);
} else puts("NO");
return ;
}
CF469D Two Set (并查集)的更多相关文章
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- 【poj1017】 Packets
http://poj.org/problem?id=1017 (题目链接) 题意 一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为1*1, 2*2, 3* ...
- SQL Server发布订阅功能研究
前提: 发布订阅只能是同一个内网的机器上才能使用,其实这个可以用配置管理器的别名功能设置之后就可以了,外网的也能通过这样的方式来搞. 配置过程参考老D的文章:http://www.cnblogs.co ...
- npm下载包时代理配置
主要有两种方式,1通过命令行临时配置,2通过配置文件进行永久配置. 1.命令行: npm install --proxy http://localhost:1080 如果有账号密码等验证的,需要在后面 ...
- Android成长日记-数据存储之SQLite[2]
Part one: 首先看这样一段代码 SQLiteDatabase db=openOrCreateDatabase("SQLDemo.db", MODE_PRIVATE,null ...
- Windows下安装paramiko
windows下python IDE我用的是pycharm 在pycharm下安装paramiko import paramiko后提示没有pycrypto这个模块 在pycharm下又安装不上pyc ...
- linux系统安装配置
以中文拼音输入法为例:1.sudo apt install ibus-pinyin2.sudo apt install ibus-libpinyin3.注销重新登录 或则 重启计算机4.进入输入设置, ...
- bash的变量设置
1. myname=zhangsan //设置变量 2. echo $myname //显示变量 或者:echo ${myname} 3. myname="my name is $mynam ...
- vs 2012 + OPenCV 2.4.8 配置
一:安装Opencv 下载opencv-2.4.8.exe,然后安装在目录D:\opencv-2.4.8. 二:配置vs 2012 1.打开vs创建项目 2.下一步,选择dll ,空项目,然后完成.后 ...
- 软件工程个人作业-Week2
第一部分 调研, 评测 必应词典客户端版本:安卓版5.2.2 bug描述一:在学习页面点击“单词挑战”或“我爱说英语”会弹出“加载失败,请稍后重试”,无论点击多少次都加载不出来. bug描述二:在未 ...
- Linux rsync网站目录同步功能的实现
实现目标: 172.16.1.64服务器上的/var/www/sw_service目录,与172.16.1.60服务器上的/var/www/sw_service目录实现同步, 即1.60主动向1.64 ...