题目来源: 原创
基准时间限制: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. awk及sum求和!

    awk 也是一个强大的编辑工具,它比 sed 的功能更加强大,可以在无交互的情况下实现相当复杂的文本操作. 1.awk 的语法 awk [选项] ' print $1' 文件名 选项 -F指定分隔符 ...

  2. 事件类型-UI事件、焦点事件

    DOM3级事件包括以下几类事件: UI事件:当用户与页面上的元素交互时触发 焦点事件:当元素获得或失去焦点时触发 鼠标事件:当用户通过鼠标在页面上执行操作时触发 滚轮事件:当使用鼠标滚轮时触发 文本事 ...

  3. A way to find out how activity for mssql and oracle

    Dear buddy, Have you confuse that how activity about my databases, if we conside it by using backup ...

  4. excel2003 颜色筛选问题

    "excel2003中,添加辅助列,用定义名称的方法得到对应的颜色号,然后对辅助列进行排序: 颜色单元格在A列,选中B1,插入->名称->定义,输入a,下面输入公式 =get.c ...

  5. PHP 获取一周的时间

    $date = array( 'Monday' => array(date('Y-m-d H:i:s',strtotime("Monday")),date('Y-m-d H: ...

  6. SpringMVC的@ControllerAdvice注解

    @ControllerAdvice顾名思义,他是一个Controller的增强,是一个异常处理类.常用于实现下面三个方面的功能: 1.处理全局异常,结合方法型注解@ExceptionHandler,用 ...

  7. 实验一 git代码版本管理

    实验目的 1. 了解分布式系统版本管理的核心机理. 2. 熟练掌握 git 的基本指令和分支管理指令. 实验内容 1. 安装 git.2. 初始化配置 git,git init ,git status ...

  8. Prometheus简介【转】

    Prometheus简介 Prometheus受启发于Google的Brogmon监控系统(相似的Kubernetes是从Google的Brog系统演变而来),从2012年开始由前Google工程师在 ...

  9. _CrtIsValidHeapPointer(pUserData)

    程序遇到如题的运行时报错,参考下面这段文字,采取将自定义类的对象定义改为new方式生成后问题解决. !!Expression: _CrtIsValidHeapPointer(pUserData) vo ...

  10. 如何反编译MIPS64伪代码?用Ghidra

    在分析固件时,碰到MIPS64架构的程序会很头疼,虽然用IDA能够反编译出汇编代码,但是没办法F5一键反编译成伪代码,如果单看汇编,看久了脑壳痛. 后来Google到了一个好工具,Ghidra,发音和 ...