高级农民
- 积分
- 1400
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-9-16
- 最后登录
- 1970-1-1
|
本帖最后由 Cabbage1123 于 2024-5-1 21:44 编辑
2024.04.29~04.30
下班之后直接睡大觉了。
2024.05.01
LC 2easy+1medium
复习了并查集
来列一下并查集的板子:
1. 朴素版-1- class UnionFind {
- public:
- UnionFind(int sz) : root(sz) {
- for (int i = 0; i < sz; i++) {
- root[i] = i;
- }
- }
- int find(int x) {
- return root[x];
- }
- void unionSet(int x, int y) {
- int rootX = find(x);
- int rootY = find(y);
- if (rootX != rootY) {
- for (int i = 0; i < root.size(); i++) {
- if (root[i] == rootY) {
- root[i] = rootX;
- }
- }
- }
- }
- bool connected(int x, int y) {
- return find(x) == find(y);
- }
- private:
- vector<int> root;
- };
复制代码 2. 朴素版-2- class UnionFind {
- public:
- UnionFind(int sz) : root(sz) {
- for (int i = 0; i < sz; i++) {
- root[i] = i;
- }
- }
- int find(int x) {
- while (x != root[x]) {
- x = root[x];
- }
- return x;
- }
- void unionSet(int x, int y) {
- int rootX = find(x);
- int rootY = find(y);
- if (rootX != rootY) {
- root[rootY] = rootX;
- }
- }
- bool connected(int x, int y) {
- return find(x) == find(y);
- }
- private:
- vector<int> root;
- };
复制代码 3. 进阶版-1: 带rank权重- class UnionFind {
- public:
- UnionFind(int sz) : root(sz), rank(sz) {
- for (int i = 0; i < sz; i++) {
- root[i] = i;
- rank[i] = 1;
- }
- }
- int find(int x) {
- while (x != root[x]) {
- x = root[x];
- }
- return x;
- }
- void unionSet(int x, int y) {
- int rootX = find(x);
- int rootY = find(y);
- if (rootX != rootY) {
- if (rank[rootX] > rank[rootY]) {
- root[rootY] = rootX;
- } else if (rank[rootX] < rank[rootY]) {
- root[rootX] = rootY;
- } else {
- root[rootY] = rootX;
- rank[rootX] += 1;
- }
- }
- }
- bool connected(int x, int y) {
- return find(x) == find(y);
- }
- private:
- vector<int> root;
- vector<int> rank;
- };
复制代码 4. 进阶版-2: 状态压缩(把deep tree压成flat的呸呸呸我在说些啥)- class UnionFind {
- public:
- UnionFind(int sz) : root(sz) {
- for (int i = 0; i < sz; i++) {
- root[i] = i;
- }
- }
- int find(int x) {
- if (x == root[x]) {
- return x;
- }
- return root[x] = find(root[x]);
- }
- void unionSet(int x, int y) {
- int rootX = find(x);
- int rootY = find(y);
- if (rootX != rootY) {
- root[rootY] = rootX;
- }
- }
- bool connected(int x, int y) {
- return find(x) == find(y);
- }
- private:
- vector<int> root;
- };
复制代码 5. 进阶版-3: 状态压缩+rank- class UnionFind {
- public:
- UnionFind(int sz) : root(sz), rank(sz) {
- for (int i = 0; i < sz; i++) {
- root[i] = i;
- rank[i] = 1;
- }
- }
- int find(int x) {
- if (x == root[x]) {
- return x;
- }
- // Some ranks may become obsolete so they are not updated
- return root[x] = find(root[x]);
- }
- void unionSet(int x, int y) {
- int rootX = find(x);
- int rootY = find(y);
- if (rootX != rootY) {
- if (rank[rootX] > rank[rootY]) {
- root[rootY] = rootX;
- } else if (rank[rootX] < rank[rootY]) {
- root[rootX] = rootY;
- } else {
- root[rootY] = rootX;
- rank[rootX] += 1;
- }
- }
- }
- bool connected(int x, int y) {
- return find(x) == find(y);
- }
- private:
- vector<int> root;
- vector<int> rank;
- };
复制代码 以上内容来自Leetcode美国区的Explore Graph~
贴在这里防止忘了2333但是并查集挺经典的...感觉也不怎么会忘记
话说一亩三分地的编辑器有朝一日可不可以支持Markdown。。。代码格式全都乱了。。。
洗洗睡了~ |
|