【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 在实现程序自动分析的过程中,常常需 ...
随机推荐
- 第六周课程总结&试验报告(四)
一.实验目的 (1)掌握类的继承方法 (2)变量的继承和覆盖,方法的继承,重载和覆盖实现 二.实验内容 三.实验过程 1. 实验源码 package test; import java.util.Sc ...
- [转帖]Linux下批量替换文件内容方法
Linux下批量替换文件内容方法 https://www.cnblogs.com/fjping0606/p/4428850.html 刚才用到的命令 原作者写的挺好的记录一下 以后 用. 1:查找fi ...
- 为 JS 的字符串,添加一个 format 的功能。
<script> String.prototype.format = function (kwargs) { var ret = this.replace(/\{(\w+)\}/g, fu ...
- sqlserver迁移mysql语法修改
1.top 100 选取表中前100条改为 limit #{limit},limit 为变量2.获取当前日期getdate()改为now()3.id=#{id,jdbcType=BIGINT}改为i ...
- spring aop之父子容器
需求;项目对外提供接口,要求每个对外接口都要进行token认证. 解决办法:写一个token认证的工具类,在每个需要认证的接口方法开始的地方,调用工具类中的token认证方法. 问题:因为要满足指定条 ...
- python_操作MySQL 初解
单文件操作数据库 import random import threading, multiprocessing import time, datetime import pymysql import ...
- Mac 基于Anaconda的TensorFlow安装笔记
最近在中国大学MOOC平台学习北大的曹健老师上的“人工智能实践——Tensorflow”课程,开始我的人工智能之旅.第一天,讲解如何搭建实验室环境,我是mac系统,所以只写mac系统上的实验室环境安装 ...
- 如何利用`keep-alive`按需缓存页面数据
随着项目不断变大,页面变多,搜索条件也随之也越来越多,而每次跳转页面再返回时,之前的筛选的条件都会别清空.之前在elment-ui table组件 -- 远程筛选排序提到过缓存,但是有所取巧,这次重新 ...
- Vue.js实现分页
效果图 代码 <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/ ...
- 【Java】 ArrayList和LinkedList实现(简单手写)以及分析它们的区别
一.手写ArrayList public class ArrayList { private Object[] elementData; //底层数组 private int size; //数组大小 ...