题目来源: 原创
基准时间限制: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. HTML标签,CSS简介

    一  http://www.w3school.com.cn/tags/tag_span.asp

  2. AngularJS四大特征

    AngularJS四大特征 1.MVC模式 Angular遵循软件工程的MVC模式,并鼓励展现,数据,和逻辑组件之间的松耦合.通过依赖注入(dependency injection),Angular为 ...

  3. activiti 全局流程监听ActivitiEventListener,实现监听不同类型事件,不需要在acitivit中配置任务监听,非常方便

    如果我们像给任务配置监听,按照常规的做法是这样的 一个个配置,比较麻烦. 现在利用ActivitiEventListener,监听全局事件,并且可以判断不同的事件类型,进而执行不同的业务逻辑. 1.定 ...

  4. 139、Java内部类之使用this访问外部类属性

    01.代码如下: package TIANPAN; class Outer { // 外部类 private String msg = "Hello World !"; class ...

  5. 利用Python进行数据分析笔记-时间序列(时区、周期、频率)

    此文对Python中时期.时间戳.时区处理等阐述十分清楚,特别值得推荐学习. 原文链接:https://blog.csdn.net/wuzlun/article/details/80287517

  6. monkey常见API及实例

    一.API简介 LaunchActivity(pkg_name, cl_name):启动应用的Activity.参数:包名和启动的Activity. Tap(x, y, tapDuration): 模 ...

  7. cookie注入

    通常我们的开发人员在开发过程中会特别注意到防止恶意用户进行恶意的注入操作,因此会对传入的参数进行适当的过滤,但是很多时候,由于个人对安全技术了解的不同,有些开发人员只会对get,post这种方式提交的 ...

  8. Vue 中引入echarts

    安装依赖 npm install echarts -S 或者使用淘宝的镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org ...

  9. 解决Ubuntu(linux)系统中PHP的curl函数无法使用的问题

    我之前用的Windows的服务器,未出现问题,后来把服务器重装了系统,今天在学微信公众号获取信息的时候,发现curl函数出现了问题...... 解决方法 首先连接上服务器,找到/etc/php/7.0 ...

  10. 【剑指Offer面试编程题】题目1368:二叉树中和为某一值的路径--九度OJ

    题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 输入: 每个测试案例包括n+1行: 第一行为2 ...