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
|
#include <stdio.h>
#include <stdlib.h>
typedef struct set set;
typedef struct element element;
typedef struct bucket bucket;
typedef struct parent parent;
struct element {
int id;
element* next;
element* prev;
set* set;
};
struct bucket {
int size;
bucket* prev;
bucket* next;
set* head;
};
struct set {
int id;
set* next;
set* prev;
element* head;
bucket* bucket;
};
struct parent {
element* pos;
parent* next;
};
element* new_element(int id, set* s) {
element* el = malloc(sizeof(element));
el->id = id;
el->set = s;
el->prev = el->next = NULL;
return el;
}
element* insert_element(set* s, int id) {
element* el = new_element(id, s);
if (s->head) {
el->next = s->head;
s->head->prev = el;
}
s->head = el;
return el;
}
set* new_set(int id) {
set* s = malloc(sizeof(set));
s->id = id;
s->prev = s->next = NULL;
s->head = NULL;
s->bucket = NULL;
return s;
}
parent* new_parent(element* el) {
parent* p = malloc(sizeof(parent));
p->pos = el;
p->next = NULL;
return p;
}
void insert_parent(parent** parents, element* el) {
int id = el->id;
parent* p = new_parent(el);
if (parents[id]) {
p->next = parents[id];
}
parents[id] = p;
}
bucket* new_bucket(set* s, int size) {
bucket* b = malloc(sizeof(bucket));
b->size = size;
b->prev = b->next = NULL;
b->head = s;
return b;
}
void insert_set(set* s, bucket* b) {
s->next = b->head;
b->head->prev = s;
b->head = s;
}
void remove_element(element* el) {
if (el->next) {
el->next->prev = el->prev;
}
if (el->prev) {
el->prev->next = el->next;
} else {
el->set->head = el->next;
}
//demote(el->set);
free(el);
}
void remove_parents(int id, parent** parents) {
for(parent* p=parents[id]; p != NULL; p = p->next) {
remove_element(p->pos);
free(p);
}
}
void greedy(bucket* head, parent** parents) {
bucket* b = head;
for(set* s=b->head; s != NULL; s = s->next) {
for (element* el = s->head; el != NULL; el = el->next) {
remove_parents(el->id, parents);
}
}
}
int main(int argc, char* argv[]) {
FILE* file = fopen(argv[1], "r");
int n_sets, n_elements = 0;
int current_element = 0;
bucket *tail, *head = NULL;
fscanf(file, "%d %d", &n_sets, &n_elements);
parent** parents = malloc(n_elements*sizeof(parent*));
for (int element_id = 0; element_id < n_elements; element_id++) {
parents[element_id] = NULL;
}
for(int current_set = 0; current_set < n_sets; current_set++) {
fscanf(file, "%d", &n_elements);
set* s = new_set(current_set);
bucket* current_bucket = tail;
int j;
for(j = 0; j < n_elements; j++) {
fscanf(file, "%d", ¤t_element);
element* el = insert_element(s, current_element);
insert_parent(parents, el);
if (current_bucket && current_bucket->size < j) {
current_bucket = current_bucket->next;
}
}
if (!current_bucket) {
bucket* b = new_bucket(s, j);
b->prev = head;
if (head) {
head->next = b;
} else {
head = tail = b;
}
} else if (current_bucket->size == j) {
insert_set(s, current_bucket);
} else if (current_bucket->size > j) {
bucket* b = new_bucket(s, j);
b->next = current_bucket;
if (current_bucket->prev) {
b->prev = current_bucket->prev;
} else {
tail = b;
}
current_bucket->prev = b;
}
}
return 0;
}
|