BZOJ4195 luoguP1955 NOI2015D1T1 程序自动分析
题意:给定n个(xi = xj) 或 (xi != xj) 的条件,问是否可能成立
BZOJ链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4195
luogu链接:https://www.luogu.org/problemnew/show/P1955
离散化+并查集
离线,先把(xi = xj)用并查集解决,再一个一个判断不等的是否矛盾
一开始用map离散化结果TLE
emm
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<vector> using namespace std; const int MAXN = ;
int n, kx;
int fa[MAXN * ], size[MAXN * ], s[MAXN * ];
vector< pair<int, int> > tf, li; template <typename tn> void read (tn & a) {
tn x = , f = ;
char c = getchar();
while (c < '' || c > ''){ if (c == '-') f = -; c = getchar(); }
while (c >= '' && c <= ''){ x = x * + c - ''; c = getchar(); }
a = f == ? x : -x;
} int find (int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
} void link (int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (size[x] > size[y]) { link(y, x); return; }
fa[x] = y;
size[y] += size[x];
} int ge (int x) {
int left = , right = kx + ;
while (left < right - ) {
int mid = (left + right) / ;
if (s[mid] > x) right = mid; else left = mid;
}
if (s[left] == x) return left; else return left + ;
} int main() {
int T;
read(T);
while (T--) {
read(n);
tf.clear(); li.clear();
for (int i = ; i <= n; ++i) {
int x, y, k;
read(x);
read(y);
read(k);
if (k == ) tf.push_back(make_pair(x, y)); else li.push_back((make_pair(x, y)));
s[i + i - ] = x;
s[i + i] = y;
}
sort(s + , s + n + n);
int i = ;
kx = ;
while (i <= n * ) {
int j = i;
while (j + <= n * && s[j + ] == s[i]) ++j;
s[kx] = s[i];
i = j + ;
++kx;
}
--kx;
for (int i = ; i <= n + n; ++i) fa[i] = i, size[i] = ;
for (int i = ; i < li.size(); ++i) {
int x, y;
x = li[i].first;
y = li[i].second;
x = ge(x);
y = ge(y);
link(x, y);
}
bool f = ;
for (int i = ; i < tf.size(); ++i) {
int x, y;
x = tf[i].first;
y = tf[i].second;
x = ge(x);
y = ge(y);
x = find(x);
y = find(y);
if (x == y) { f = ; break; }
}
if (f) cout << "YES\n"; else cout << "NO\n";
}
return ;
}
BZOJ4195 luoguP1955 NOI2015D1T1 程序自动分析的更多相关文章
- 【BZOJ4195】[Noi2015]程序自动分析 并查集
[BZOJ4195][Noi2015]程序自动分析 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3 ...
- 【bzoj4195】[Noi2015]程序自动分析 离散化+并查集
题目描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量 ...
- 【BZOJ4195】 [Noi2015]程序自动分析
Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...
- BZOJ4195:[NOI2015]程序自动分析
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- 【BZOJ4195】【NOI2015】程序自动分析(并查集)
[BZOJ4195][NOI2015]程序自动分析(并查集) 题面 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设 ...
- 【bzoj4195】【NOI2015】程序自动分析
4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3470 Solved: 1626[Submit][Sta ...
- [UOJ#127][BZOJ4195][NOI2015]程序自动分析
[UOJ#127][BZOJ4195][NOI2015]程序自动分析 试题描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2, ...
- BZOJ-4195 NOI2015Day1T1 程序自动分析 并查集+离散化
总的来说,这道题水的有点莫名奇妙,不过还好一次轻松A 4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 836 ...
- BZOJ4195 程序自动分析
Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...
随机推荐
- 解决java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
启动eclipse 发现如下错误 Error configuring application listener of class org.springframework.web.util.Log4jC ...
- [转载] C# DllImport用法和路径问题
DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息. DllImport属性应用于方法,要求最少 ...
- 使用shell脚本批处理控制大数据环境服务启动停止
三台集群机器: master 192.168.168.200 slave1 192.168.168.201 slave2 192.168.168.202 1.start-maste ...
- ElasticSearch停止启动
1.查找ES进程 ps -ef | grep elastic 2.杀掉ES进程 kill -9 2382(进程号) 3.重启ES sh elasticsearch -d 注意:elasticsearc ...
- 基于mysql对mybatis中的foreach进行深入研究
鉴于上一篇博文一次修改mysql字段类型引发的技术探究提到的,要对foreach里面的collection相关的内容做一些介绍,今天就围绕foreach,做一些数据插入和查询相关的研究. 首先介绍一下 ...
- ArrayBlcokingQueue,LinkedBlockingQueue与Disruptor三种队列对比与分析
一.基本介绍 ArrayBlcokingQueue,LinkedBlockingQueue是jdk中内置的阻塞队列,网上对它们的分析已经很多,主要有以下几点: 1.底层实现机制不同,ArrayBlco ...
- xe5 android 调用照相机获取拍的照片[转]
本篇文章我们来看一下delphi xe5 在android程序里怎样启动照相机并获取所拍的照片,本代码取自xe自带打sample,路径为: C:\Users\Public\Documents\RAD ...
- php 二维数组按照指定字段进行排序
$allItem = [ ["id"=>10,"updated_at"=>"2018-11-01"], ["id&qu ...
- 在linux下使用am335x的DMA
[本文首发于cnblogs,作者:byeyear,Email:east3@163.com] 这几天在弄am3358的DMA,简化应用环境如下: am3358的FSMC接了一片FPGA: FPGA上接A ...
- STL基础--基本介绍
为什么要使用C++标准库 /* * 为什么使用C++标准库: * 1. 代码重用,不用重新造轮子 * 2. 效率(快速,且使用更少的资源). 现代C++编译器经常对C++标准库的代码有优化 * 3. ...