forked from upupming/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path947.most-stones-removed-with-same-row-or-column.cpp
73 lines (65 loc) · 2.49 KB
/
947.most-stones-removed-with-same-row-or-column.cpp
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
/*
* @lc app=leetcode id=947 lang=cpp
*
* [947] Most Stones Removed with Same Row or Column
*/
#include <bits/stdc++.h>
using namespace std;
// @lc code=start
class Solution {
public:
int removeStones(vector<vector<int>>& stones) {
map<int, map<int, bool>> m, m1;
map<int, map<int, int>> parent;
int rows = 0, cols = 0;
for (auto stone : stones) {
m[stone[0]][stone[1]] = true;
m1[stone[1]][stone[0]] = true;
parent[stone[0]][stone[1]] = -1;
}
int ans = 0;
for (auto mi : m) {
int i = mi.first;
for (auto mj : mi.second) {
int j = mj.first;
int curRoot = find(parent, i, j);
int xi = i, xj = j;
// cout << "curRoot: " << curRoot % 10000 << ", " << curRoot / 10000 << endl;
// Same row
for (auto point : mi.second) {
int xj = point.first;
if (!m[xi][xj] || !(xj > j)) continue;
int xRoot = find(parent, xi, xj);
// cout << "xRoot: " << xRoot % 10000 << ", " << xRoot / 10000 << endl;
if (curRoot != xRoot) {
parent[xRoot % 10000][xRoot / 10000] = curRoot;
// cout << "union for " << i << ", " << j << "; " << xi << ", " << xj << endl;
ans++;
// assert(find(parent, i, j) == find(parent, xi, xj));
}
}
// Same column
for (auto point : m1[j]) {
int xi = point.first;
if (!m[xi][xj] || !(xi > i)) continue;
int xRoot = find(parent, xi, xj);
// cout << "xRoot: " << xRoot % 10000 << ", " << xRoot / 10000 << endl;
if (curRoot != xRoot) {
parent[xRoot % 10000][xRoot / 10000] = curRoot;
// cout << "union for " << i << ", " << j << "; " << xi << ", " << xj << endl;
ans++;
// assert(find(parent, i, j) == find(parent, xi, xj));
}
}
}
}
return ans;
}
int find(map<int, map<int, int>>& parent, int i, int j) {
if (parent[i][j] == -1)
return i + j * 10000;
else
return find(parent, parent[i][j] % 10000, parent[i][j] / 10000);
}
};
// @lc code=end