?? unionfindwitharrays.cpp
字號(hào):
// simple unite/find using 1D array
#include <iostream>
#include "myExceptions.h"
using namespace std;
int *equivClass, // equivalence class array
n; // number of elements
void initialize(int numberOfElements)
{// Initialize numberOfElements classes with one element each.
n = numberOfElements;
equivClass = new int [n + 1];
for (int e = 1; e <= n; e++)
equivClass[e] = e;
}
void unite(int classA, int classB)
{// Unite the classes classA and classB.
// Assume classA != classB
for (int k = 1; k <= n; k++)
if (equivClass[k] == classB)
equivClass[k] = classA;
}
int find(int theElement)
{// Find the class that contains theElement.
return equivClass[theElement];
}
int main(void)
{
initialize(10);
unite(1,2);
unite(3,4);
unite(1,3);
for (int i = 1; i < 7; i++)
cout << "Element " << i << " is in equivalence class "
<< find(i) << endl;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -