【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 在实现程序自动分析的过程中,常常需 ...
随机推荐
- 第一篇 数据库MySql
数据库的简介 数据库:存储数据的仓库 数据库管理系统软件 常见的数据库管理软件:甲骨文的oracle,IBM的db2,sql server, Access,Mysql(开源,免费,跨平台). 关系型数 ...
- VR中射线点击按钮的实现
VR中实现UI的Button点击,主要是需要实现IPointerClickHandler接口,因为在Unity将所有的按钮操作都封装成了相应的接口,需要相应的功能只需要去实现对应的接口就好了.在这里我 ...
- HttpServletResponse 之 sendError( )
直接返回http 401状态,提示重新登录 response.sendError(401, "当前账户未登录或会话失效,请重新登录!) HTTP状态码列表: 100Continue继续.客户 ...
- windows中使用mysql配置my.ini时的坑
windows中安装mysql的一般步骤: mysql版本:5.7.16 1.解压 2.把解压的文件夹bin目录地址添加到环境变量PATH里面 3.在文件加中添加配置文件my.ini——配置内容后面说 ...
- 2019CSUST集训队选拔赛题解(三)
PY学长的放毒题 Description 下面开始PY的香港之行,PY有n个要去的小吃店,这n个小吃店被m条路径联通起来. PY有1个传送石和n−1个传送石碎片. PY可以用传送石标记一个小吃店作为根 ...
- Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问题
Cross origin requests are only supported for protocol schemes: http, data, chrome,chrome-extension的问 ...
- php分页类学习
分页是目前在显示大量结果时所采用的最好的方式.有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据.在互联网上,分页是一般用于搜索结果或是浏览全部信息(比如:一个论坛主题).几乎在每一个W ...
- 《英文版c++语言程序设计》
compatibility [kəm,pætɪ'bɪlɪtɪ] n.兼容 compatible [kəm'pætɪb(ə)l] adj. 兼容的:能共处的:可并立的 interdependent [ɪ ...
- 作业2-MathExam V2.0
MathExam V2.0 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 20 50 • ...
- Alpha冲刺——第五天
Alpha第五天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...