-
Notifications
You must be signed in to change notification settings - Fork 4
/
kruskal.cc
142 lines (112 loc) · 3.04 KB
/
kruskal.cc
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <unistd.h>
#include "set.h"
#include "heap.h"
#include "table.h"
struct vertex_t;
struct edge_t {
edge_t(vertex_t* f, vertex_t* t, unsigned w) : from(f), to(t), cost(w) { }
vertex_t* from;
vertex_t* to;
unsigned cost;
lite::heap_link<edge_t> link;
typedef lite::heap<edge_t, &edge_t::link, typeof(edge_t::cost), &edge_t::cost> heap_t;
bool
bound() const {
return false
|| link.bound()
;;
}
void kill() { if (!bound()) delete this; }
};
struct vertex_t {
vertex_t(const char * c) : id(strdup(c)) { }
~vertex_t() { free(const_cast<char*>(id)); }
const char* id;
lite::table_link<vertex_t> table_link;
typedef lite::table<vertex_t, &vertex_t::table_link, typeof(vertex_t::id), &vertex_t::id> table_t;
lite::set_link<vertex_t> set_link;
typedef lite::set<vertex_t, &vertex_t::set_link> set_t;
bool typed() const { return set_t::typed(this); }
set_t* archetype() const { return set_t::archetype(this); }
bool
bound() const {
return false
|| table_link.bound()
|| set_link.bound()
;;
}
void kill() { if (!bound()) delete this; }
bool unify(vertex_t* that) {
if (this == that)
return false;
else if (!typed() && !that->typed())
(new set_t)->join(this).join(that);
else if (!typed())
set_t::archetype(that)->join(this);
else if (!that->typed())
set_t::archetype(this)->join(that);
else {
set_t* this_a = set_t::archetype(this);
set_t* that_a = set_t::archetype(that);
if (this_a == that_a)
return false;
delete this_a->conjoin(that_a);
}
return true;
}
};
int
main(int, char* argv[]) {
const unsigned load = 2;
unsigned n_vertices = 0;
edge_t::heap_t edges;
vertex_t::table_t vertices;
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty() || '#' == line[0])
continue;
std::stringstream ss(line);
std::string from, to;
unsigned cost = 1;
ss >> from >> to >> cost;
vertex_t* f = vertices.get(from.c_str());
if (!f) {
++n_vertices;
if (vertices.buckets() < load * n_vertices)
vertices.reseat((load+1) * (n_vertices+1));
vertices.set(f = new vertex_t(from.c_str()));
}
vertex_t* t = vertices.get(to.c_str());
if (!t) {
++n_vertices;
if (vertices.buckets() < load * n_vertices)
vertices.reseat((load+1) * (n_vertices+1));
vertices.set(t = new vertex_t(to.c_str()));
}
edges.inhume(new edge_t(f, t, cost));
}
while (!edges.empty()) {
edge_t* e = edges.exhume();
if (e->from->unify(e->to))
std::cout
<< e->from->id << " "
<< e->to->id << " "
<< e->cost << std::endl;
delete e;
}
for (vertex_t* v = vertices.iterator() ; v ;
v = vertices.wipe(v, &vertex_t::kill)) {
if (v->typed())
delete &v->archetype()->dissolve();
}
vertices.reseat();
return EXIT_SUCCESS;
}
//