-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1579.cpp
More file actions
71 lines (71 loc) · 1.85 KB
/
1579.cpp
File metadata and controls
71 lines (71 loc) · 1.85 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
class DisjointSet {
private:
vector<int> parent;
vector<int> rank;
int group;
public:
DisjointSet(int n) {
parent.resize(n, 0);
rank.resize(n, 0);
for (int i = 0; i < n; ++i) parent[i] = i;
group = n;
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
bool join(int x, int y) {
int pX = find(x);
int pY = find(y);
if (pX == pY) return false;
group--;
if (rank[pX] > rank[pY]) {
parent[pY] = pX;
}
else if (rank[pX] < rank[pY]) {
parent[pX] = pY;
}
else {
rank[pX]++;
parent[pY] = pX;
}
return true;
}
bool isFullConnected() {
return group == 1;
}
};
class Solution {
public:
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
DisjointSet* aliceSet = new DisjointSet(n);
DisjointSet* bobSet = new DisjointSet(n);
int res = 0;
int index = 0;
for (auto& edge : edges) {
if (edge[0] == 3) {
bool result = aliceSet->join(edge[1] - 1, edge[2] - 1);
bobSet->join(edge[1] - 1, edge[2] - 1);
if (result == false) res++;
}
}
for (auto& edge : edges) {
if (edge[0] == 2) {
bool result = bobSet->join(edge[1] - 1, edge[2] - 1);
if (result == false) res++;
}
}
for (auto& edge : edges) {
if (edge[0] == 1) {
bool result = aliceSet->join(edge[1] - 1, edge[2] - 1);
if (result == false) res++;
}
}
if (aliceSet->isFullConnected() && bobSet->isFullConnected()) {
return res;
}
return -1;
}
};