浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4195

由于等于具有传递性,所以此题可以用并查集完美的解决。

把相等的元素放在一个集合里,不等的判断是不是不在一个集合里即可。

时间复杂度:\(O(\alpha{n})\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=1e5+5; int n,cnt;
int fa[maxn<<1],tmp[maxn<<1];
int a[maxn],b[maxn],opt[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} int find(int x) {
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
} void merge(int a,int b) {
a=find(a),b=find(b);
if(a!=b)fa[a]=b;
} int main() {
int T=read();
while(T--) {
n=read();
for(int i=1;i<=n;i++)
a[i]=tmp[i]=read(),b[i]=tmp[i+n]=read(),opt[i]=read();
sort(tmp+1,tmp+(n<<1)+1);
cnt=unique(tmp+1,tmp+(n<<1)+1)-tmp-1;
for(int i=1;i<=cnt;i++)fa[i]=i;
for(int i=1;i<=n;i++) {
a[i]=lower_bound(tmp+1,tmp+cnt+1,a[i])-tmp;
b[i]=lower_bound(tmp+1,tmp+cnt+1,b[i])-tmp;
}
bool ans=1;
for(int i=1;i<=n;i++)
if(opt[i])merge(a[i],b[i]);
for(int i=1;i<=n;i++)
if(!opt[i]&&find(a[i])==find(b[i]))ans=0;
if(ans)puts("YES");
else puts("NO");
}
return 0;
}

BZOJ4195:[NOI2015]程序自动分析的更多相关文章

  1. [UOJ#127][BZOJ4195][NOI2015]程序自动分析

    [UOJ#127][BZOJ4195][NOI2015]程序自动分析 试题描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2, ...

  2. BZOJ4195 [Noi2015]程序自动分析(离散化+并查集)

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 689  Solved: 296 [Submit][Sta ...

  3. BZOJ4195 NOI2015 程序自动分析

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Description 在实现程序自动分析的过程中,常常需要判定一些约束条件 ...

  4. [BZOJ4195] [NOI2015] 程序自动分析 (并查集)

    Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...

  5. bzoj4195 [Noi2015]程序自动分析——并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4195 突然在这道大水题上WA了半天... 思路很简单,离线处理询问,先把 = 的都加到并查集 ...

  6. [Bzoj4195] [NOI2015] 程序自动分析 [并查集,哈希,map] 题解

    用并查集+离散化,注意:并查集数组大小不是n而是n*2 #include <iostream> #include <algorithm> #include <cstdio ...

  7. 【BZOJ4195】[Noi2015]程序自动分析 并查集

    [BZOJ4195][Noi2015]程序自动分析 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3 ...

  8. BZOJ-4195 NOI2015Day1T1 程序自动分析 并查集+离散化

    总的来说,这道题水的有点莫名奇妙,不过还好一次轻松A 4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 836 ...

  9. codevs4600 [NOI2015]程序自动分析==洛谷P1955 程序自动分析

    4600 [NOI2015]程序自动分析  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 在实现 ...

  10. Codevs 4600 [NOI2015]程序自动分析

    4600 [NOI2015]程序自动分析 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 在实现程序自动分析的过程中,常常需 ...

随机推荐

  1. verilog FAQ(zz)

    1. What is the race condition in verilog? Ans :The situation when two expressions are allowed to exe ...

  2. linux上安装程序出现的问题汇总

    1.程序在编译过程中出现:variable set but not used [-Werror=unused-but-set-variable] 解决方法:将configure文件和Makefile文 ...

  3. Ubuntu: 无法使用su命令

    Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...

  4. Android编译系统简要介绍【转】

    本文转载自:http://blog.csdn.net/luoshengyang/article/details/18466779 在Android源码环境中,我们开发好一个模块后,再写一个Androi ...

  5. zookeeper分布式锁的问题

    分布式锁的流程: 在zookeeper指定节点(locks)下创建临时顺序节点node_n 获取locks下所有子节点children 对子节点按节点自增序号从小到大排序 判断本节点是不是第一个子节点 ...

  6. 新建maven web后controller不能被扫描到

    1.新建maven web 子工程 2.修改web.xml. 3.resources下建立spring.xml与springmvc.xml. 4.spring.xml删除对controller的扫描. ...

  7. springcloud-Api网关服务Zuul

    springcloud项目例子:链接:https://pan.baidu.com/s/1O1PKrdvrq5c8sQUb7dQ5Pg 密码:ynir 1.由来: 如果我的微服务中有很多个独立服务都要对 ...

  8. elasticsearch中如何手动控制全文检索结果的精准度

    1.为帖子数据增加标题字段 POST /forum/article/_bulk{ "update": { "_id": "1"} }{ &q ...

  9. HTTP 指纹识别v0.1

    // Winhttp.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include ...

  10. SoapUI使用方法-01发送http请求

    soap ui http://blog.csdn.net/russ44/article/details/51680083 一.发送HTTP请求消息 1.打开soapUI. 2.新建一个项目,实例如下: ...