【bzoj4195】【NOI2015】程序自动分析
4195: [Noi2015]程序自动分析
Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 3470 Solved: 1626
[Submit][Status][Discuss]
Description
在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足。
Input
输入文件的第1行包含1个正整数t,表示需要判定的问题个数。注意这些问题之间是相互独立的。
题解:
这题可以离线,并且是对整体进行询问,直接离线所有判断,然后并查集合并所有等号,对不等号一一判断即可;
想说的是这个题的在线版本(ORZ wwwwodddd)


大致就是在线了然后回答每一个询问;
依旧用并查集,但是用set对每个并查集维护一个敌人集合,然后合并两个集合的时候启发式合并set即可,需要离散化;(两份代码都放了OVO)
复杂度O(nlogn)
#include<bits/stdc++.h>
using namespace std;
const int N=;
int T,n,tot,tx[N],ty[N],cnt,fa[N];
map<int,int>mp;
int get(int x){
if(!mp[x])mp[x]=++tot,fa[tot]=tot;
return mp[x];
}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main(){
// freopen("bzoj4195.in","r",stdin);
// freopen("bzoj4195.out","w",stdout);
scanf("%d",&T);
while(T--){
scanf("%d",&n);
tot=cnt=;mp.clear();
for(int i=,x,y,e;i<=n;i++){
scanf("%d%d%d",&x,&y,&e);
x=get(x);y=get(y);
if(e){
int fx=find(x),fy=find(y);
if(fx!=fy)fa[fx]=fy;
}else tx[++cnt]=x,ty[cnt]=y;
}
int fg=;
for(int i=;i<=cnt;i++)if(find(tx[i])==find(ty[i])){fg=;break;}
puts(fg?"NO":"YES");
}
return ;
}
离线版本
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
#include<set>
#define Run(i,l,r) for(int i=l;i<=r;i++)
#define Don(i,l,r) for(int i=l;i>=r;i--)
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=;
int n,m,sub[N],tot,fa[N];
struct data{
int x,y,p;
data(int _x=,int _y=,int _p=):x(_x),y(_y),p(_p){};
}A[N];
set<int>s[N];
set<int>::iterator it;
char gc(){
static char*p1,*p2,S[];
if(p1==p2)p2=(p1=S)+fread(S,,,stdin);
return(p1==p2)?EOF:*p1++;
}
int rd(){
int x=,f=; char c=gc();
while(c<''||c>''){if(c=='-')f=-;c=gc();}
while(c>=''&&c<='')x=(x<<)+(x<<)+c-'',c=gc();
return x*f;
}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
bool check(int x,int y){
if(x==y)return false;
if(s[x].size()>s[y].size())swap(x,y);
for(it=s[x].begin();it!=s[x].end();it++){
if(find(*it)==y)return true;
}
fa[x]=y;
for(it=s[x].begin();it!=s[x].end();it++){
s[y].insert(find(*it));
}
s[x].clear();
return false;
}
int main(){
// freopen("mzoj1292.in","r",stdin);
// freopen("mzoj1292.out","w",stdout);
//scanf("%d",&n);
n=rd();
Run(i,,n){
//A[i]=(data){rd(),rd(),rd()};
//scanf("%d%d%d",&A[i].x,&A[i].y,&A[i].p);
A[i].x=rd(); A[i].y=rd(); A[i].p=rd();
sub[++tot]=A[i].x , sub[++tot]=A[i].y;
}
sort(sub+,sub+tot+);
tot=unique(sub+,sub+tot+)-sub-;
Run(i,,tot)fa[i]=i;
for(int i=,x,y;i<=n;i++){
x=A[i].x=lower_bound(sub+,sub+tot+,A[i].x)-sub;
y=A[i].y=lower_bound(sub+,sub+tot+,A[i].y)-sub;
if(A[i].p==){
if(find(x)==find(y)){puts("No");continue;}
puts("Yes");
s[fa[x]].insert(fa[y]);
s[fa[y]].insert(fa[x]);
}else {
int fx=find(x) , fy=find(y);
if(check(fx,fy)){puts("No");continue;}
else puts("Yes");
}
}
return ;
}//by tkys_Austin;
在线版本
【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 在实现程序自动分析的过程中,常常需 ...
随机推荐
- ubuntu/linux系统中安装jdk以及eclipse(附图解详细步骤)
1.首先得先下载JDK和eclipsejdk下载网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21 ...
- Phaser3 屏幕适配iPhoneX、iPhoneXs的坑 -- JavaScript Html5 游戏开发
PhaserJS 坑:在config内不要把 width 设为 window.innnerWidth在config内不要把 width 设为 window.innnerWidth在config内不 ...
- Lua学习笔记(3):运算符
算术运算符 运算符 描述 + 加法运算符 - 减法运算符 * 乘法运算符 / 除法运算符 % 取模运算符 ^ 乘幂 A=3 print(A^2)输出9 关系运算符 ~= 不等于 == 等于 > ...
- 《算法图解》——第十章 K最近邻算法
第十章 K最近邻算法 1 K最近邻(k-nearest neighbours,KNN)——水果分类 2 创建推荐系统 利用相似的用户相距较近,但如何确定两位用户的相似程度呢? ①特征抽取 对水果 ...
- lamp一键配置 --转自秋水
https://teddysun.com/lamp LAMP一键安装脚本 最后修改于:2015年11月08日 / 秋水逸冰 / 54,300 次围观 973 本脚本适用环境: 系统支持:CentOS/ ...
- Scrum立会报告+燃尽图(十月二十日总第十一次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- Xftp安装和使用的视频录制方法
内容: 1.使用工具 2.操作步骤及方法 视频地址: http://v.youku.com/v_show/id_XMzEwNjg2MTg2NA==.html?spm=a2h3j.8428770.341 ...
- CodeForces 483B 二分答案
题目: B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input s ...
- Web站点性能-微观手段
文章:网站性能优化 百度百科:高性能Web站点 文章:构建高性能WEB站点之 吞吐率.吞吐量.TPS.性能测试
- lintcode-423-有效的括号序列
423-有效的括号序列 给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and ']', 判定是否是有效的括号序列. 样例 括号必须依照 "( ...