【BZOJ4195】 [Noi2015]程序自动分析
Description
在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足。
Input
输入文件的第1行包含1个正整数t,表示需要判定的问题个数。注意这些问题之间是相互独立的。
Output
输出文件包括t行。
Sample Input
2
1 2 1
1 2 0
2
1 2 1
2 1 1
Sample Output
YES
HINT
在第一个问题中,约束条件为:x1=x2,x1≠x2。这两个约束条件互相矛盾,因此不可被同时满足。
Solution
离散化完并查集。
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout)
#endif #define R register
#define getc() (_S == _T && (_T = (_S = _B) + fread(_B, 1, 1 << 15, stdin), _S == _T) ? EOF : *_S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
#define cabs(_x) ((_x) < 0 ? (- (_x)) : (_x))
char _B[ << ], *_S = _B, *_T = _B;
inline int F()
{
R char ch; R int cnt = ; R bool minus = ;
while (ch = getc(), (ch < '' || ch > '') && ch != '-') ;
ch == '-' ? minus = : cnt = ch - '';
while (ch = getc(), ch >= '' && ch <= '') cnt = cnt * + ch - '';
return minus ? -cnt : cnt;
}
#define maxn 1000010
struct Query
{
int a, b, type;
inline bool operator < (const Query &that) const {return type > that.type;}
}q[maxn];
int hash[maxn << ], Fa[maxn << ];
int Find(R int x) {return Fa[x] == x ? x : Fa[x] = Find(Fa[x]);}
int main()
{
// setfile();
for (R int cas = , t = F(); cas <= t; ++cas)
{
R int n = F(), hashcnt = ;
for (R int i = ; i <= n; ++i)
{
q[i] = (Query) {F(), F(), F()};
hash[++hashcnt] = q[i].a;
hash[++hashcnt] = q[i].b;
}
std::sort(hash + , hash + hashcnt + );
std::sort(q + , q + n + );
hashcnt = std::unique(hash + , hash + hashcnt + ) - hash - ;
for (R int i = ; i <= (hashcnt << ); ++i) Fa[i] = i;
R bool flag = ;
for (R int i = ; i <= n; ++i)
{
q[i].a = std::lower_bound(hash + , hash + hashcnt + , q[i].a) - hash;
q[i].b = std::lower_bound(hash + , hash + hashcnt + , q[i].b) - hash;
// printf("i = %d %d %d %d\n", i, q[i].a, q[i].b, q[i].type );
R int f1 = Find(q[i].a), f2 = Find(q[i].a + hashcnt);
R int f3 = Find(q[i].b), f4 = Find(q[i].b + hashcnt);
if (q[i].type == )
{
if (f1 == f3 || f2 == f4)
{
puts("NO");
flag = ;
break;
}
}
else
{
if (f1 == f3 || f2 == f4) continue;
Fa[f1] = f3;
Fa[f2] = f4;
}
}
flag ? puts("YES") : ;
}
return ;
}
/*
1
9
24 234 1
2837 1 1
242 78 0
23 1 1
223 977 0
254 76 1
235 877 0
235 987 0
877 987 0
*/
【BZOJ4195】 [Noi2015]程序自动分析的更多相关文章
- [UOJ#127][BZOJ4195][NOI2015]程序自动分析
[UOJ#127][BZOJ4195][NOI2015]程序自动分析 试题描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2, ...
- BZOJ4195 [Noi2015]程序自动分析(离散化+并查集)
4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 689 Solved: 296 [Submit][Sta ...
- BZOJ4195 NOI2015 程序自动分析
4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Description 在实现程序自动分析的过程中,常常需要判定一些约束条件 ...
- [BZOJ4195] [NOI2015] 程序自动分析 (并查集)
Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...
- bzoj4195 [Noi2015]程序自动分析——并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4195 突然在这道大水题上WA了半天... 思路很简单,离线处理询问,先把 = 的都加到并查集 ...
- [Bzoj4195] [NOI2015] 程序自动分析 [并查集,哈希,map] 题解
用并查集+离散化,注意:并查集数组大小不是n而是n*2 #include <iostream> #include <algorithm> #include <cstdio ...
- 【BZOJ4195】[Noi2015]程序自动分析 并查集
[BZOJ4195][Noi2015]程序自动分析 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3 ...
- BZOJ-4195 NOI2015Day1T1 程序自动分析 并查集+离散化
总的来说,这道题水的有点莫名奇妙,不过还好一次轻松A 4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 836 ...
- codevs4600 [NOI2015]程序自动分析==洛谷P1955 程序自动分析
4600 [NOI2015]程序自动分析 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 在实现 ...
- Codevs 4600 [NOI2015]程序自动分析
4600 [NOI2015]程序自动分析 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 在实现程序自动分析的过程中,常常需 ...
随机推荐
- css 命名 有的加# ,有的加点 ,有的没加。请问下都在什么情况下用的?
[nav{}]这样的样式是给特定的标签直接定义样式时使用的,这个样式名称是跟标签是相对应的,比如我要给<p></p>这个标签设置样式,那我就可以直接写:p{样式}就可以,但是这 ...
- AppDomain介绍
一.建立控制台项目 二.更改Program为ADSetp 三.去掉命名空间AppDomain.CurrentDomain 四.贴代码 using System; using System.IO; us ...
- HDU 1297 Children’s Queue (递推、大数相加)
Children’s Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- java 不可变对象 final Collections guava 简单样例
本地环境 jdk1.8 连接 Google Guava官方教程(中文版) journaldev 说明 java的final关键字大家都了解,但是final修饰的如果是引用类型,那么不可修改的其实只是重 ...
- gcc数据对齐之: howto 2.
原文链接:http://www.catb.org/esr/structure-packing/ 谁应阅读本文 本文探讨如何通过手工重新打包C结构体声明,来减小内存空间占用.你需要掌握基本的C语言知识, ...
- 3.学习Dispatcher
3.学习Dispatcher 不管是WinForm应用程序还是WPF应用程序,实际上都是一个进程,一个进程可以包含多个线程,其中有一个是主线程,其余的是子线程. 在WPF或WinForm应用程序中,主 ...
- IDEA一些有用的功能
使用 Type Info 如果你想要更多的关于符号的信息,例如从哪里或它的类型是什么, 快速文档可以很好的帮到您,您可以按下 Ctrl+Q 来调用它,然后你会看到一个包含这些细节的弹出窗口.如果您不需 ...
- qtdebug和release加载不同的文件配置
win32:CONFIG(release, debug|release): { LIBS +=$$PWD/../../../thirdparty\qwt\lib\qwt.lib LIBS +=$$PW ...
- 通过web.xml监听器启动main方法
web.xml中添加要启动的类 <listener> <listener-class>server.NettyServer</listener-class> < ...
- vue项目报错1 Vue is a constructor and should be called with the `new` keyword && jquery.js?eedf:3850 Uncaught TypeError: this._init is not a function...
Vue is a constructor and should be called with the `new` keyword Uncaught TypeError: this._init is n ...