-
Notifications
You must be signed in to change notification settings - Fork 4
/
tarjan.cc
204 lines (158 loc) · 4.5 KB
/
tarjan.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#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"
#include "stack.h"
struct vertex_t;
struct edge_t {
edge_t(vertex_t* f, vertex_t* t) : from(f), to(t) { }
vertex_t* from;
vertex_t* to;
lite::stack_link<edge_t> from_link;
typedef lite::stack<edge_t, &edge_t::from_link> from_edges_t;
bool
bound() const {
return false
|| from_link.bound()
;;
}
void kill() { if (!bound()) delete this; }
};
struct vertex_t {
vertex_t(const char * c) : id(strdup(c)), index(0), root(NULL), edge(NULL) { }
~vertex_t() { free(const_cast<char*>(id)); }
const char* id;
unsigned index;
vertex_t* root;
edge_t* edge;
edge_t::from_edges_t from_edges;
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::stack_link<vertex_t> stack_link;
typedef lite::stack<vertex_t, &vertex_t::stack_link> stack_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()
|| stack_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;
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;
ss >> from >> to;
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()));
}
f->from_edges.push(new edge_t(f, t));
}
unsigned count = 0;
for (vertex_t* i = vertices.iterator() ; i ; i = vertices.next(i)) {
if (i->root)
continue;
vertex_t::stack_t path;
path.push(i);
while (!path.empty()) {
vertex_t* v = path.peek();
edge_t::from_edges_t & fe = v->from_edges;
if (!v->root) {
v->root = v;
v->index = ++count;
v->edge = fe.iterator();
} else if (v->edge) {
assert(v->edge->from == v);
if (!v->edge->to->index) {
assert(!v->edge->to->stack_link.bound());
path.push(v->edge->to);
}
v->edge = fe.next(v->edge);
} else {
path.pop();
assert(v->root == v);
for (edge_t* e = fe.iterator() ; e ; e = fe.next(e)) {
assert(e->from == v);
assert(e->to->index);
assert(e->to->root);
if (!e->to->root->stack_link.bound())
continue;
if (v->root->index <= e->to->root->index)
continue;
v->root = e->to->root;
e->to->root->unify(v);
}
if (v->root == v) {
if (v->typed()) {
vertex_t::set_t* s = v->archetype();
vertex_t* w = s->iterator();
std::cout << w->id;
while ((w = s->next(w)))
std::cout << ' ' << w->id;
std::cout << std::endl;
} else
std::cout << v->id << std::endl;
}
}
}
}
for (vertex_t* v = vertices.iterator() ; v ;
v = vertices.wipe(v, &vertex_t::kill)) {
while (!v->from_edges.empty())
v->from_edges.pop()->kill();
if (v->typed())
delete &v->archetype()->dissolve();
}
vertices.reseat();
return EXIT_SUCCESS;
}
//