[UOJ#127][BZOJ4195][NOI2015]程序自动分析
[UOJ#127][BZOJ4195][NOI2015]程序自动分析
试题描述
在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足。
输入
输入文件的第1行包含1个正整数t,表示需要判定的问题个数。注意这些问题之间是相互独立的。
输出
输出文件包括t行。
输入示例
输出示例
NO
YES
数据规模及约定
1≤n≤1000000
1≤i,j≤1000000000
题解
离散化后,用个并查集,对于相等的两个变量把它们放在同一个连通块里,然后判断所有不等的条件中的两个元素是否在同一个连通块,在就输出 NO,否则输出 YES。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 100010 int fa[maxn<<1], cnt[2], num[maxn<<1], cntn;
struct Que {
int x, y;
Que() {}
Que(int _, int __): x(_), y(__) {}
} con[2][maxn]; int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); } int main() {
int T = read();
while(T--) {
int n = read();
cnt[0] = cnt[1] = cntn = 0;
for(int i = 1; i <= n; i++) {
int x = read(), y = read(), tp = read();
num[++cntn] = x; num[++cntn] = y;
con[tp][++cnt[tp]] = Que(x, y);
}
sort(num + 1, num + cntn + 1);
cntn = unique(num + 1, num + cntn + 1) - num - 1;
for(int i = 1; i <= cntn; i++) fa[i] = i;
for(int tp = 0; tp < 2; tp++)
for(int i = 1; i <= cnt[tp]; i++)
con[tp][i].x = lower_bound(num + 1, num + cntn + 1, con[tp][i].x) - num,
con[tp][i].y = lower_bound(num + 1, num + cntn + 1, con[tp][i].y) - num;
for(int i = 1; i <= cnt[1]; i++) {
int u = findset(con[1][i].x), v = findset(con[1][i].y);
if(u != v) fa[v] = u;
}
bool ok = 1;
for(int i = 1; i <= cnt[0]; i++) {
int u = findset(con[0][i].x), v = findset(con[0][i].y);
if(u == v){ ok = 0; break; }
}
puts(ok ? "YES" : "NO");
} return 0;
}
[UOJ#127][BZOJ4195][NOI2015]程序自动分析的更多相关文章
- 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 在实现程序自动分析的过程中,常常需 ...
随机推荐
- Android 滑动RecyclerView时隐藏部分控件
在使用RecyclerView控件时,上下拖动控件时的时候,需要实时的隐藏与显示部分控件,已到达很好的用户体验. 原理很简单,当RecyclerView拖动至最上层时显示控件,当RecyclerV ...
- Objective-C Protocols
Objective-C allows you to define protocols, which declare the methods expected to be used for a part ...
- Android程序初体验
第一个程序的实现的最终功能是: 点击"正确"或者"错误"会得到一个是否正确的提示. 直接上效果图. 此次涉及代码编写的文件有4个: package co ...
- 洛谷 P2922 [USACO08DEC]秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- 说说三四月的app审核中的几个坑
苹果的审核在3月异常严格,听说和换了部门领导有关(道听途说),恰逢三月公司新出了一个产品,我们的产品被苹果打回四五次,今天就在简书上把这些坑填下,也让遇到的朋友以后留意,也许是近期的最后一篇文章. 坑 ...
- 使用js将后台返回的数据转换成树形结构
将类似如下数据转换成树形的数据: [ { id: 1, name: '1', }, { id: 2, name: '1-1', parentId: 1 }, { id: 3, name: '1-1-1 ...
- 传统BP对比CNN
传统BP vs CNN 存在2个问题 传统BP网络存在的问题: 权值太多,计算量太大 权值太多,需要大量样本进行训练 传统的BP来处理图像问题的话因为计算权值太多太大. 网络的建立要根据数据的大小来建 ...
- DirectX9(翻译):介绍
一.简介 二.DirectX Software Development Kit 这本帮助文档总共分为五大部分:DirectX Software Development Kit DirectX Grap ...
- kubernetes概念
kubernetes blog cluster cluster是计算.存储.和网络资源的集合,kubernetes利用这些资源运行各种基于容器的应用. master master是cluster的大脑 ...
- python 实现计算器功能 输入字符串,输出相应结果
import re formul='1 - 2 *( (6 0- 30+(0-40/5) * (9-2* 5/3 +7 /3*99/4*2998 +10 *568/14)) - (-4*3) / (1 ...