51nod 1515 明辨是非 [并查集+set]
今天cb巨巨突然拿题来问,感觉惊讶又开心,希望他早日康复!!坚持学acm!加油!
1515 明辨是非
题目来源: 原创
基准时间限制: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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <limits.h>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
const int N = 1e5+;
int fa[N], r[N]; map <int, int> vis;
set <int> s[N];
set <int> :: iterator it;
struct node{
int x, y, p;
}q[N];
int fin(int x) {
if(x != fa[x]) fa[x] = fin(fa[x]);
return fa[x];
}
void uni(int a, int b) {
if(r[a] > r[b]) {
for(it = s[b].begin(); it != s[b].end(); it++) {
// s[a].insert(*it);
int x = fin(*it);
s[*it].erase(b);
s[a].insert(x);
s[x].insert(a);
}
fa[b] = a;
r[a]++;
}
else {
for(it = s[a].begin(); it != s[a].end(); it++) {
//s[b].insert(*it);
int x = fin(*it);
s[*it].erase(a);
s[b].insert(x);
s[x].insert(b);
}
fa[a] = b;
r[b]++;
}
}
int main() {
int n, x, y, t = ;
scanf("%d", &n); for (int i = ; i <= n; ++i) {
scanf("%d%d%d", &x, &y, &q[i].p);
if(!vis[x]) {vis[x] = t; q[i].x = t++;}
else q[i].x = vis[x]; if(!vis[y]) {vis[y] = t; q[i].y = t++;}
else q[i].y = vis[y];
}
for(int i = ; i <= n; ++i) {
fa[q[i].x] = q[i].x;
fa[q[i].y] = q[i].y;
}
for(int i = ; i <= n; ++i) {
t = ;
x = fin(q[i].x);
y = fin(q[i].y);
if(q[i].p) {
if(x == y) puts("YES");
else {
/*for(it = s[x].begin(); it != s[x].end(); it++) {
if(s[y].count(*it)) {
t = 1;
break;
}
}
for(it = s[y].begin(); it != s[y].end(); it++) {
if(s[x].count(*it)) {
t = 1;
break;
}
}
if(t) puts("NO");*/
if(s[x].count(y) || s[y].count(x)) puts("NO");
else {
puts("YES");
uni(x, y);
}
}
}
else {
if(x == y) puts("NO");
else {
puts("YES");
s[x].insert(y);
s[y].insert(x);
}
}
}
return ;
}
51nod 1515 明辨是非 [并查集+set]的更多相关文章
- 51Nod 1515 明辨是非 —— 并查集 + 启发式合并
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 1515 明辨是非 题目来源: 原创 基准时间限制:1 ...
- 51nod 1515 明辨是非 并查集 + set + 启发式合并
给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...
- 51nod 1515 明辨是非 并查集+set维护相等与不等关系
考试时先拿vector瞎搞不等信息,又没离散化,结果好像MLE:后来想起课上讲过用set维护,就开始瞎搞迭代器...QWQ我太菜了.. 用并查集维护相等信息,用set记录不相等的信息: 如果要求变量不 ...
- 51 nod 1515 明辨是非(并查集合并)
1515 明辨是非题目来源: 原创基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以 ...
- 51nod 1204 Parity(并查集应用)
1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...
- 51nod 1515:明辨是非 并查集合并
1515 明辨是非 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 给n组操作,每组操作形式为x y p. 当p为1时,如果第x ...
- 51nod 1515 明辨是非 启发式合并
1515 明辨是非 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 Description 给n组操 ...
- 51nod1515 明辨是非 并查集 + set
一开始想的时候,好像两个并查集就可以做......然后突然懂了什么.... 相同的并查集没有问题,不同的就不能并查集了,暴力的来个set就行了..... 合并的时候启发式合并即可做到$O(n \log ...
- 51nod-1515 明辨是非——并查集
给n组操作,每组操作形式为x y p. 当p为1时,如果第x变量和第y个变量可以相等,则输出YES,并限制他们相等:否则输出NO,并忽略此次操作. 当p为0时,如果第x变量和第y个变量可以不相等,则输 ...
随机推荐
- Python基础(6) - 基本语句
Python print(在Python 3.0中就变成了函数了) print语句是把对象用文本化的形式输出到标准的输出流上. Operation Interpretation print spam ...
- whatwg-fetch
fetch 是什么 XMLHttpRequest的最新替代技术 fetch优点 接口更简单.简洁,更加语义化 基于promise,更加好的流程化控制,可以不断then把参数传递,外加 async/aw ...
- ES6中的类继承和ES5中的继承模式详解
1.ES5中的继承模式 我们先看ES5中的继承. 既然要实现继承,首先我们得要有一个父类. Animal.prototype.eat = function(food) { console.log(th ...
- 【极客学院-idea教程】
极客学院idea教程: http://whudoc.qiniudn.com/2016/IntelliJ-IDEA-Tutorial/index.html
- postgres entityframework foreignkey
public class Model { [Key, Column("id"), DatabaseGenerated(DatabaseGeneratedOption.Identit ...
- HTTP Error 502.5 - Process Failure asp.net core error in IIS
在windows server 2012 上安装完dotnet-win-x64.1.1.1.exe 和 DotNetCore.1.0.4_1.1.1-WindowsHosting.exe后,没有重启服 ...
- 使用Myeclipse进行简单webservice开发的示例
(转) http://blog.csdn.net/changhenshui1990/article/details/70142371 前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要 ...
- (转载).NET的五层架构
我们刚开始学习架构的时候,首先会想到分层的概念,分层架构比较经典的是三层架构,那么,什么是三层架构呢?它包括表现层,业务层,数据访问层:而对于一个新手来说,从抽象意义上的三层架构,逻辑上就划分为三个层 ...
- 编写DBCP连接池
#配置数据库数据源package com.itang.utils; import java.io.InputStream; import java.sql.Connection; import jav ...
- 六、Spring之DI的Bean的作用域
Spring提供“singleton”和“prototype”两种基本作用域,另外提供“request”.“session”.“global session”三种web作用域:Spring还允许用户定 ...