-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
68 lines (63 loc) · 1.57 KB
/
test.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
#include <fstream>
#include <iostream>
#include <vector>
#include "node.h"
node* findNode(char label,node* nodes[],int N) {
for(int n = 0;n < N;n++) {
if(nodes[n]->getName() == label)
return nodes[n];
}
node* result = nullptr;
return result;
}
int main() {
// Read the graph data
std::ifstream in;
in.open("nodes.txt");
int N;
in >> N;
// Make one node for each vertex in the graph
node** nodes = new node*[N];
for(int n = 0;n < N;n++) {
char label;
in >> label;
nodes[n] = new node(label);
}
int E;
in >> E;
// For each edge, find the nodes that the edge connects. If those nodes
// have different representatives, form the union of their sets.
for(int n = 0;n < E;n++) {
char label1,label2;
in >> label1 >> label2;
node* first = findNode(label1,nodes,N);
node* second = findNode(label2,nodes,N);
if(first->getRepresentative() != second->getRepresentative()) {
first->makeUnion(second);
}
}
in.close();
// Construct a list of the distinct sets that are left
std::vector<node*> sets;
for(int n = 0;n < N;n++) {
node* rep = nodes[n]->getRepresentative();
bool seen = false;
for(int m = 0;m < sets.size();m++) {
if(sets[m] == rep)
seen = true;
}
if(!seen) {
sets.push_back(rep);
}
}
// Print the members of each distinct set
for(int m = 0;m < sets.size();m++) {
for(int n = 0;n < N;n++) {
if(nodes[n]->getRepresentative() == sets[m]) {
std::cout << nodes[n]->getName() << ' ';
}
}
std::cout << std::endl;
}
return 0;
}