⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uf.cpp

📁 经典c++程序的实现
💻 CPP
字号:
#include <iostream.h>
#include "book.h"

// Collect together the UNION/FIND functions
#include "uf.h"

Gentree::Gentree(int sz) {     // Constructor
  size = sz;
  array = new GTNode[sz];      // Create node array
}

Gentree::~Gentree() {          // Destructor
  delete [] array;             // Free node array
}

bool Gentree::differ(int a, int b) { // Nodes in different trees?
  GTNode* root1 = FIND(&array[a]);   // Find root of node a
  GTNode* root2 = FIND(&array[b]);   // Find root of node b
  return root1 != root2;             // Compare roots
}

void Gentree::UNION(int a, int b) {  // Merge two subtrees
  GTNode* root1 = FIND(&array[a]);   // Find root of node a
  GTNode* root2 = FIND(&array[b]);   // Find root of node b
  if (root1 != root2) root2->par = root1; // Merge subtrees
}

GTNode* Gentree::FIND(GTNode* curr) const {
  if (curr->parent() == NULL) return curr;  // At root
    return curr->par = FIND(curr->parent());
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -