【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 在实现程序自动分析的过程中,常常需 ...
随机推荐
- Keil出错解决方法
1.安装KEIL5后创建工程后出现这个报错 解决方法:打开下图目录的文件. Keil.STM32F1xx_DFP.pdsc文件是只读文件,必须将只读属性取消. 如下图所示,注释掉红色圆圈的哪一行,保存 ...
- Phaser3跟随自定义路径移动的赛车 -- iFIERO游戏教程
racingcar 在线预览:http://www.ifiero.com/uploads/phaser/pathrotate/代码: var config = { type: Phaser.AUT ...
- v-on 事件修饰符
事件修饰符: .stop 阻止冒泡 .prevent 阻止默认事件 .capture 添加事件侦听器时使用事件捕获模式 .self 只当该事件在该元素本身时(不是子元素)触发时才回调 .once ...
- K-means + PCA + T-SNE 实现高维数据的聚类与可视化
使用matlab完成高维数据的聚类与可视化 [idx,Centers]=kmeans(qy,) [COEFF,SCORE,latent] = pca(qy); SCORE = SCORE(:,:); ...
- Java线上应用故障排查之一:高CPU占用 (转)
一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. (友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hank ...
- 【算法设计与数据结构】为何程序员喜欢将INF设置为0x3f3f3f3f?(转)
摘自https://blog.csdn.net/jiange_zh/article/details/50198097 在算法竞赛中,我们常常需要用到一个“无穷大”的值,对于我来说,大多数时间我会根据具 ...
- USACO 3.3.1 Riding the Fences 骑马修栅栏(欧拉回路)
Description 农民John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个一个栅栏.你必须编一个程 ...
- Walking Between Houses(贪心+思维)
Walking Between Houses There are nn houses in a row. They are numbered from 11 to nn in order from l ...
- scrum立会报告+燃尽图(第三周第四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2286 项目地址:https://coding.net/u/wuyy694 ...
- [图算法] 1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...