题目来源: 原创
基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
 收藏
 关注

给n组操作,每组操作形式为x y p。

当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等;否则输出NO,并忽略此次操作。

当p为0时,如果第x变量和第y个变量可以不相等,则输出YES,并限制他们不相等 ;否则输出NO,并忽略此次操作。

Input
输入一个数n表示操作的次数(n<=1*10^5)
接下来n行每行三个数x,y,p(x,y<=1*10^8,p=0 or 1)
Output
对于n行操作,分别输出n行YES或者NO
Input示例
3
1 2 1
1 3 1
2 3 0
Output示例
YES
YES
NO

考虑用并查集维护相等关系,用set维护不等关系。

就是说相等的放在一个并查集里面,这个集合开个set,其中存放和它不等的并查集序号。

如果限制不相等,显然就是在set中添加元素

如果限制相等,那么我们可以通过启发式合并将两个并查集以及他们对应的set合并

复杂度为n*(logn)^2

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <map>
#pragma warning(disable:4996)
using namespace std; int pre[200005];
int x[100005];
int y[100005];
int oper[100005];
int num[200005];
set<int> notequal[50005];
int n,m,shizu1,shizu2;
set<int>::iterator it1,it2;
map<int,int>hash_m; int findpre(int x)
{
while(x!=pre[x])
{
x=pre[x];
}
return x;
} void union_set(int x,int y)
{
int pre_x = x;
int pre_y = y; if(pre_x == pre_y)
return;
else if(notequal[pre_y].size()>notequal[pre_x].size())
{
int temp = pre_x;
pre_x = pre_y;
pre_y = temp;
} pre[pre_y]=pre_x; for(it2=notequal[pre_y].begin();it2!=notequal[pre_y].end();it2++)
{
notequal[pre_x].insert(*it2);
}
} template <class F,class T>
F bin_search(F s,F e,T val)
{
F L = s;
F R = e-1; while(L<=R)
{
F mid = L + (R-L)/2;
if(!(*mid<val || val < *mid))
{
return mid;
}
else if(val < *mid)
{
R = mid -1;
}
else
{
L= mid + 1;
}
}
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,j,test,num_m,le,re;
num_m=0;
scanf("%d",&test);
for(j=1;j<=test;j++)
{
scanf("%d%d%d",&x[j],&y[j],&oper[j]);
num[num_m]=x[j];
pre[num_m]=num_m;
num_m++; num[num_m]=y[j];
pre[num_m]=num_m;
num_m++;
}
sort(num,num+num_m);
num_m=unique(num,num+num_m)-num;
for(j=0;j<num_m;j++)
{
hash_m[num[j]]=j;
}
for(j=1;j<=test;j++)
{
le=hash_m[x[j]];
re=hash_m[y[j]];
shizu1=findpre(re);
shizu2=findpre(le);
if(oper[j]==1)
{
int flag=0; if(notequal[shizu2].size()<notequal[shizu1].size())
{
it1=notequal[shizu2].find(shizu1);
if(it1!=notequal[shizu2].end()) //找到
{
flag=1;
}
if(flag==0)
{
for(it1=notequal[shizu2].begin();it1!=notequal[shizu2].end();it1++)
{
if(shizu1 == findpre(*it1))
{
flag=1;
break;
}
}
}
}
else
{
it2=notequal[shizu1].find(shizu2);
if(it2!=notequal[shizu1].end()) //找到
{
flag=1;
}
for(it2=notequal[shizu1].begin();it2!=notequal[shizu1].end();it2++)
{
if(shizu2 == findpre(*it2))
{
flag=1;
break;
}
}
} if(flag==0)
{
union_set(shizu1,shizu2);
printf("YES\n");
}
else
{
printf("NO\n");
}
}
else
{
if(shizu1==shizu2)
{
printf("NO\n");
}
else
{
notequal[shizu2].insert(shizu1);
notequal[shizu1].insert(shizu2);
printf("YES\n");
}
}
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

51nod 1515:明辨是非 并查集合并的更多相关文章

  1. 51nod 1515 明辨是非 [并查集+set]

    今天cb巨巨突然拿题来问,感觉惊讶又开心,希望他早日康复!!坚持学acm!加油! 题目链接:51nod 1515 明辨是非 [并查集] 1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间 ...

  2. 51 nod 1515 明辨是非(并查集合并)

    1515 明辨是非题目来源: 原创基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以 ...

  3. 51Nod 1515 明辨是非 —— 并查集 + 启发式合并

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 1515 明辨是非  题目来源: 原创 基准时间限制:1 ...

  4. 51nod 1515 明辨是非 并查集 + set + 启发式合并

    给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...

  5. 51nod 1515 明辨是非 并查集+set维护相等与不等关系

    考试时先拿vector瞎搞不等信息,又没离散化,结果好像MLE:后来想起课上讲过用set维护,就开始瞎搞迭代器...QWQ我太菜了.. 用并查集维护相等信息,用set记录不相等的信息: 如果要求变量不 ...

  6. 51nod 1204 Parity(并查集应用)

    1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题   你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...

  7. 51nod 1515 明辨是非 启发式合并

    1515 明辨是非 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 Description 给n组操 ...

  8. 51nod1515 明辨是非 并查集 + set

    一开始想的时候,好像两个并查集就可以做......然后突然懂了什么.... 相同的并查集没有问题,不同的就不能并查集了,暴力的来个set就行了..... 合并的时候启发式合并即可做到$O(n \log ...

  9. 51nod-1515 明辨是非——并查集

    给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...

随机推荐

  1. 【PAT甲级】1007 Maximum Subsequence Sum (25 分)

    题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...

  2. ‘A’ = N’A’ , will not kill index seek in sqlserver version above 2014 anymore

    Thanks to The advisor show us that we can get different behavior when we use condition , ='20000' or ...

  3. SSM(Spring-SpringMvc-Mybatis)练习

    1.总结 https://pan.baidu.com/s/1kXlCf4r  密码:hv6v 2.代码 https://pan.baidu.com/s/1pNgKph5  密码:6rcm 3.资料 h ...

  4. sklearn中调用集成学习算法

    1.集成学习是指对于同一个基础数据集使用不同的机器学习算法进行训练,最后结合不同的算法给出的意见进行决策,这个方法兼顾了许多算法的"意见",比较全面,因此在机器学习领域也使用地非常 ...

  5. PaperReading20200221

    CanChen ggchen@mail.ustc.edu.cn Busy... Human-level concept learning through probabilistic program i ...

  6. ZCGL大数据项目优化组件布置

    1.经JMeter并发性能测试,每个HBaseService服务的并发请求上限大概是1K,为了支持5W个并发请求量,需要增加部署节点,相应需要增加部署路由网管Zuul,为了隐藏多个路由网管Zuul的I ...

  7. AJAX封装数据处理简单操作

    数据的封装处理主要展现在JS中,在页面里面引入封装的JS, "js/ajax.js" 简单封装将get和post方法都写入,get的方法和post的方法依然需要严格区分,包括typ ...

  8. DICOM设备Raw Data与重建

    DICOM设备Raw Data与重建      现在的医疗影像设备基本都已DICOM为标准.但现在许多医院的技术人员都以为只要支持DICOM就一切OK,其实不然.DICOM中有Storage.Prin ...

  9. Day1-D-CF-1144C

    简述:给你一个数组,判断是否能拆分成2个数组,一个递增一个递减,若不行输出No,可以就Yes并分别输出 思路:统计每个数出现的次数,若有大于2的肯定无法组成严格单调,这样就只需要将出现两次的组成递,剩 ...

  10. String类与StringBuffer类

    String类与StringBuffer类   一.String类和StringBuffer类的区别 String类是不可变类,新建的对象为不可变对象(String类的内容和长度是固定的),一旦被创建 ...