-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialCode.c
153 lines (133 loc) · 3.05 KB
/
serialCode.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
struct node {
int val;
struct node *next;
};
struct queue {
struct node *front, *rear;
};
struct node* newNode(int n) {
struct node* temp = (struct node*) malloc(sizeof(struct node));
temp->val = n;
temp->next = NULL;
return temp;
}
struct queue* createQueue() {
struct queue* q = (struct queue*) malloc(sizeof(struct queue));
q->front = NULL;
q->rear = NULL;
return q;
}
void enQueue(struct queue *q, int n) {
struct node* temp = newNode(n);
if(q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
struct node* deQueue(struct queue *q) {
if(q->front == NULL) {
return NULL;
}
struct node* temp = q->front;
q->front = q->front->next;
if(q->front == NULL) {
q->rear = NULL;
}
return temp;
}
bool isEmpty(struct queue *q) {
return (q->front == NULL);
}
void initialize(int *menacc, int *womenacc, int *menpre, int n)
{
int i;
for(i=0; i<=n; i++)
{
menacc[i]=-1;
womenacc[i]=-1;
menpre[i]=1;
}
}
int main()
{
int n,i,j,k,idx;
int **men, **women;
int *menacc, *womenacc, *menpre;
clock_t start, end;
double time_taken;
start = clock();
scanf("%d",&n);
men = (int **) malloc((n+1)*sizeof(int*));
women = (int **) malloc((n+1)*sizeof(int*));
menacc = (int *) malloc((n+1)*sizeof(int));
womenacc = (int *) malloc((n+1)*sizeof(int));
menpre = (int *) malloc((n+1)*sizeof(int));
for(i=0; i<=n; i++) {
men[i] = (int *) malloc((n+1)*sizeof(int));
women[i] = (int *) malloc((n+1)*sizeof(int));
}
initialize(menacc, womenacc, menpre, n);
for(i=1;i<=n;i++) {
for(j=0;j<=n;j++) {
scanf("%d",&men[i][j]);
}
}
for(i=1;i<=n;i++) {
for(j=0;j<=n;j++) {
scanf("%d",&k);
women[i][k]=j;
}
}
end = clock();
time_taken = ((double)(end-start) * 1000000)/CLOCKS_PER_SEC;
printf("read time : %f us, ", time_taken);
struct queue* q = createQueue();
for(i=1; i<=n; i++) {
enQueue(q, i);
}
start = clock();
int ct=0;
while(!isEmpty(q)) {
ct++;
struct node* cur = deQueue(q);
j = cur->val;
free(cur);
// j is the index of man, menpre contains the next preference to be checked
// idx is the index of woman he proposes
idx = men[j][menpre[j]];
if(womenacc[idx] == -1) {
womenacc[idx] = j;
menacc[j] = idx;
}
else if(women[idx][womenacc[idx]] > women[idx][j]) {
menacc[womenacc[idx]] = -1;
menacc[j] = idx;
if(menpre[womenacc[idx]] <= n)
enQueue(q, womenacc[idx]);
womenacc[idx] = j;
}
else if(menpre[j] <= n) {
enQueue(q, j);
}
menpre[j]++;
}
end = clock();
time_taken = ((double)(end-start) * 1000000)/CLOCKS_PER_SEC;
printf("compute time : %f us\n", time_taken);
printf("count : %d\n", ct);
for(j=1;j<=n;j++)
printf("%d %d\n", j, menacc[j]);
free(q);
for(i=0; i<=n; i++) {
free(men[i]); free(women[i]);
}
free(men); free(women);
free(menacc); free(womenacc); free(menpre);
return 0;
}