题面

题目链接

大秦 为你打开 题目传送门

备用题面

给定若干未赋值的变量,并给出 $n$ 组操作,每组操作形如:x y p
当 $p$ 为 $1$ 时,如果第 $x$ 变量和第 $y$ 个变量可以相等,则输出 YES ,并限制他们必须相等;否则输出 NO ,并忽略此次操作。
当 $p$ 为 $0$ 时,如果第 $x$ 变量和第 $y$ 个变量可以不相等,则输出 YES ,并限制他们必须不相等;否则输出 NO ,并忽略此次操作。

思路

显而易见的相等关系肯定是用并查集存储,而不等关系我们只需要用 vector 存下来有以后大力合并即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// #pragma GCC optimize(2)
// #pragma GCC optimize(3, "Ofast", "inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef __int128 int128;
typedef pair<int , int > pii;
typedef unsigned long long ull;

namespace FastIO
{
// char buf[1 << 20], *p1, *p2;
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
template<typename T> inline T read(T& x) {
x = 0;
int f = 1;
char ch;
while (!isdigit(ch = getchar())) if (ch == '-') f = -1;
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x *= f;
return x;
}
template<typename T, typename... Args> inline void read(T& x, Args &...x_) {
read(x);
read(x_...);
return;
}
inline ll read() {
ll x;
read(x);
return x;
}
};
using namespace FastIO;

const int N = 1e5 + 10;

int n;
vector<int >vx;
int x[N], y[N], p[N];

int fa[N << 1];
inline int find(int x) { return fa[x] = (fa[x] == x ? x : find(fa[x])); }

vector<int >mN[N << 1];

inline void Input() {
read(n);
for(int i = 1; i <= n; i++) {
read(x[i], y[i], p[i]);
vx.push_back(x[i]);
vx.push_back(y[i]);
}
}

inline bool Check(int x, int y, int p) {
int fx = find(x), fy = find(y);
if(fx == fy) return p == 1; // must equal
if(mN[fx].size() > mN[fy].size()) swap(fx, fy);
for(auto &p : mN[fx]) {
if(find(p) == fy) {
return p == 0; // must not equal
}
}
// then we need define two variable is equal or not
if(p == 1) {
fa[fx] = fy;
// we are equal, so the variable not equal to you is also not equal to me
for(auto &p : mN[fx]) {
mN[fy].push_back(p);
}
mN[fx].clear();
}
else {
mN[fy].push_back(fx);
mN[fx].push_back(fy);
}
return 1;
}

inline void Work() {
sort(vx.begin(), vx.end());
vx.erase(unique(vx.begin(), vx.end()), vx.end());
for(int i = 1; i <= n; i++) {
x[i] = lower_bound(vx.begin(), vx.end(), x[i]) - vx.begin() + 1;
y[i] = lower_bound(vx.begin(), vx.end(), y[i]) - vx.begin() + 1;
}
for(int i = 0; i <= vx.size(); i++) fa[i] = i;
for(int i = 1; i <= n; i++) {
if(Check(x[i], y[i], p[i])) printf("YES\n");
else printf("NO\n");
}
}

int main() {
int T = 1;
while(T--) {
Input();
Work();
}
return 0;
}