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距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2745 Solved: 1694[Submit][Statu ...
- FAILED java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI:hdfs:192.*
运行的参数配置 hdfs:192.168.58.180/cf/userItem.txt 应该写成 hdfs://192.*
- CF 84D Doctor(二分)
题目链接: 传送门 Doctor time limit per test:1 second memory limit per test:256 megabytes Description Th ...
- Python编码格式的指定方式
参考自: http://python.jobbole.com/85852/, 原文探究的更深,有兴趣的可以去看看. 简介来讲就是使用一种特殊的注释来声明编码格式,如何判断这种格式也用了很简单粗暴有效的 ...
- JSP+JavaBean+Servlet实现各类列表分页功能
需求: 每页列表下都有一个分页的功能,显示总数量.当前页/总页数.首页.上一页.下一页.最后一页.GO到第几页 效果动态图: 实现思路: 因为每个列表页都需要,在每个出列表页数据的servlet中都要 ...
- UIViewController 的 presentedViewController 和 presentingViewController
#import "TestViewController.h" #import "OneViewController.h" 在TextViewController ...
- JavaWeb---总结(十七)JSP中的九个内置对象
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...
- HTML5预学习 长期更新
HTML5 仍处于完善之中. 为 HTML5 建立的一些规则: 新特性应该基于 HTML.CSS.DOM 以及 JavaScript. 减少对外部插件的需求(比如 ...
- MyEclipse快捷键大全(绝对全)
存盘 Ctrl+s(肯定知道) 注释代码 Ctrl+/ 取消注释 Ctrl+\(Eclipse3已经都合并到Ctrl+/了) 代码辅助 Alt+/ 快速修复 Ctrl+1 代码格式化 Ctrl+Shi ...
- centos设置编码
CentOS6.5解决中文乱码与设置字符集 Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的 ...