题目来源: 原创
基准时间限制: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. redhat 7.6 配置repo源

    vi /etc/yum.repos.d/base.repo          #编辑配置repo配置文件,如果没有则自动创建,没有影响 name=base     //源名字,起什么名都没影响 bas ...

  3. alert \ confirm \ prompt

    alert() : 会将()中的内容弹出,返回的是()中的内容值,也就是字符串值 confirm :需要用户点击 "确定" 或 "取消" ,若用户点击 ”确定“ ...

  4. MySql的数据导入到Sql Server数据库中

    步骤一:安装MySql驱动 驱动下载链接:https://dev.mysql.com/downloads/connector/odbc/ 下载完成后安装, 一路Next即可 步骤二:创建DSN DSN ...

  5. unity渲染优化

    https://blog.csdn.net/yudianxia/article/details/79339103 https://blog.csdn.net/e295166319/article/de ...

  6. java并发:初探sleep方法

    sleep与wait sleep是Thread方法,使得当前线程从运行态变为阻塞态.但它不会释放对象的锁. wait方法是Object方法,它的作用是使得当前拥有对象锁的线程从运行态变为阻塞态, 它会 ...

  7. Day9 - K - Yue Fei's Battle HDU - 5136

    Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in t ...

  8. 「HNOI/AHOI2018」道路

    传送门 Luogu 解题思路 考虑树形 \(\text{DP}\) 设状态 \(dp[u][i][j]\) 表示从首都走到点 \(u\) ,经过 \(i\) 条公路,\(j\) 条铁路的最小不方便值. ...

  9. spring事务代码实践

    事务一般是指数据库事务,是指作为一个程序执行单元执行的一系列操作,要么完全执行,要么完全不执行.事务就是判断以结果为导向的标准. 一.spring的特性(ACID) (1).原子性(atomicity ...

  10. Linux 下JDK安装

     ubuntu下安装jdk  sudo apt-get install openjdk-8-jdk =============================================== Ce ...