-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexclusive_or.cpp
More file actions
83 lines (70 loc) · 1.62 KB
/
Copy pathexclusive_or.cpp
File metadata and controls
83 lines (70 loc) · 1.62 KB
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
#pragma optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
struct DSU {
vi parent, size;
void init_dsu(int n) {
parent = vi(n);
size = vi(n, 1);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a == b)
return;
if (size[a] < size[b])
swap(a, b);
parent[b] = a;
size[a] += size[b];
}
};
struct edge {
int a, b, x;
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
DSU dsu;
vector<edge> zero_edges;
vector<edge> one_edges;
while (m--) {
char tchar;
string tstring;
int a, b, c;
cin >> tchar >> a >> tstring >> tchar >> b >> tstring >> c;
if (c == 0)
zero_edges.push_back({a, b, c});
if (c == 1)
one_edges.push_back({a, b, c});
}
dsu.init_dsu(n + 1);
for (edge e : zero_edges) {
dsu.union_sets(e.a, e.b);
}
for (edge e : one_edges) {
if (dsu.find_set(e.a) == dsu.find_set(e.b)) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}