考试时先拿vector瞎搞不等信息,又没离散化,结果好像MLE;后来想起课上讲过用set维护,就开始瞎搞迭代器。。。QWQ我太菜了。。

用并查集维护相等信息,用set记录不相等的信息:

如果要求变量不等,若不和并查集矛盾,就拿set互相记录一下,YES;矛盾就NO

如果要求变量相等,

  1.x记录的不等的变量中有y,说明矛盾,NO

  2.若祖先不等,那就合并两个变量所在的并查集(合并两个并查集的祖先),size大的并到小的上,set暴力转移集合不相等的信息,YES

否则YES

#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#define R register int
using namespace std;
int n,cnt;
set<int> s[];
map<int,int>mp;
int fa[];
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
}
int getf(int x) {return x==fa[x]?x:fa[x]=getf(fa[x]);}
signed main() {
n=g(); for(R i=;i<=n*+;++i) fa[i]=i;
for(R i=;i<=n;++i) {
R x=g(),y=g(),p=g();
if(mp[x]) x=mp[x]; else x=mp[x]=++cnt;
if(mp[y]) y=mp[y]; else y=mp[y]=++cnt;
x=getf(x),y=getf(y); if(s[x].size()<s[y].size()) swap(x,y);
if(p&) {
if(s[x].count(y)) putchar('N'),putchar('O'),putchar('\n');
else if(x!=y) { fa[y]=x;
for(set<int>::iterator it=s[y].begin();it!=s[y].end();it++)
s[*it].insert(x),s[x].insert(*it);
putchar('Y'),putchar('E'),putchar('S'),putchar('\n');
} else putchar('Y'),putchar('E'),putchar('S'),putchar('\n');
} else {
if(x==y) putchar('N'),putchar('O'),putchar('\n');
else s[x].insert(y),s[y].insert(x),
putchar('Y'),putchar('E'),putchar('S'),putchar('\n');
}
}
}

2019.04.11

51nod 1515 明辨是非 并查集+set维护相等与不等关系的更多相关文章

  1. 51nod 1515 明辨是非 [并查集+set]

    今天cb巨巨突然拿题来问,感觉惊讶又开心,希望他早日康复!!坚持学acm!加油! 题目链接:51nod 1515 明辨是非 [并查集] 1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间 ...

  2. 51nod 1515 明辨是非 并查集 + set + 启发式合并

    给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...

  3. 51Nod 1515 明辨是非 —— 并查集 + 启发式合并

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 1515 明辨是非  题目来源: 原创 基准时间限制:1 ...

  4. 51 nod 1515 明辨是非(并查集合并)

    1515 明辨是非题目来源: 原创基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以 ...

  5. 51nod 1515 明辨是非 启发式合并

    1515 明辨是非 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 Description 给n组操 ...

  6. 51nod 1204 Parity(并查集应用)

    1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...

  7. 51nod 1515:明辨是非 并查集合并

    1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给n组操作,每组操作形式为x y p. 当p为1时,如果第x ...

  8. Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】

    C. Rumor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  9. BZOJ 1202 [HNOI2005]狡猾的商人:并查集(维护距离)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1202 题意: 有一个账本,记录了n个月的盈亏. 每个月的数值:正为盈,负为亏. 你知道m个 ...

随机推荐

  1. python练习1(简单爬虫)

    做一个简单的练习 目标:爬取中文小说 目标网站:http://www.biqule.com/book_58/26986.html 只爬取正文部分. 使用requests库来获取网页信息,使用re库正则 ...

  2. eclipse的maven工程Dynamic Web Module 2.3 修改为3.0 解决办法

    1. 创建Maven Web工程 2. 项目只有src/main/resources >Java Build Path导入Tomcat运行环境 3. 删除以图片红框中的文件 4. Propert ...

  3. Gym-101673: A Abstract Art (模板,求多个多边形的面积并)

    手抄码板大法. #include<bits/stdc++.h> using namespace std; #define mp make_pair typedef long long ll ...

  4. Swift范性

    关于泛型 「泛型」(Generic Code)也许是Swift相对于OC的最大特性之一吧!基于Swift的「泛型」特性,你能够写出扩展性更强.复用性更强的方法.类型,它可以让你尽可能避免重复代码,用一 ...

  5. ACM学习历程——HDU4814 Golden Radio Base(数学递推) (12年成都区域赛)

    Description Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ...

  6. 【Lintcode】112.Remove Duplicates from Sorted List

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. Examp ...

  7. C++常见错误总结

    1.  error C2871: 'std' : does not exist or is not a namespace 原来 C++有两个不同版本的头文件.引入名字空间这个概念以前编译器用的是#i ...

  8. SQL 优化总结(二) 索引

     索引 1.索引的建立 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的:合理的索引设计要建立在对各种查询的分析和预测上. 一般来说: (1) 有大量重复值.且经常有范围查询(between, ...

  9. 四 akka学习 四种多线程的解决方案

    http://blog.csdn.net/chenleixing/article/details/44044243 四种多线程的解决方案

  10. css使图片变成黑白效果

    -webkit-filter: grayscale(%); -moz-filter: grayscale(%); -ms-filter: grayscale(%); -o-filter: graysc ...