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
|
#include <stdlib.h>
#include <string.h>
#define MAX_ELEM 40
int map_str(char *strs[MAX_ELEM], char *s, int num) {
for (int i = 0; i < num; i++)
if (!strcmp(strs[i], s))
return i + 1;
return 0;
}
int find_dj(int *rt, double wt[][MAX_ELEM], int x) {
int rx = rt[x];
if (rx == x)
return x;
rt[x] = find_dj(rt, wt, rx);
if (rt[x] != rx) {
wt[x][rt[x]] = wt[x][rx] * wt[rx][rt[x]];
wt[rt[x]][x] = 1 / wt[x][rt[x]];
}
return rt[x];
}
void union_dj(int *rt, int *rnk, double wt[][MAX_ELEM], double w, int x,
int y) {
int rx, ry;
rx = find_dj(rt, wt, x);
ry = find_dj(rt, wt, y);
wt[x][y] = w;
wt[y][x] = 1 / w;
if (rx == ry)
return;
wt[rx][ry] = wt[rx][x] * wt[y][ry] * wt[x][y];
wt[ry][rx] = 1 / wt[rx][ry];
if (rnk[rx] > rnk[ry])
rt[ry] = rx;
else if (rnk[ry] > rnk[rx])
rt[rx] = ry;
else {
rt[ry] = rx;
rnk[rx]++;
}
return;
}
double *calcEquation(char ***equations, int equations_size,
int *equations_col_size, double *values, int values_size,
char ***queries, int queries_size, int *queries_col_size,
int *return_size) {
int num_strs = 0, rt[MAX_ELEM], map_id1, map_id2;
char *strs[MAX_ELEM], *s;
double wt[MAX_ELEM][MAX_ELEM];
{
int rnk[MAX_ELEM];
for (int i = 0; i < MAX_ELEM; i++) {
for (int j = 0; j < MAX_ELEM; j++)
wt[i][j] = 1.0;
rt[i] = i;
rnk[i] = 1;
}
for (int i = 0; i < equations_size; i++) {
s = equations[i][0];
if (!(map_id1 = map_str(strs, s, num_strs))) {
strs[num_strs] = s;
map_id1 = num_strs;
num_strs++;
} else
map_id1--;
s = equations[i][1];
if (!(map_id2 = map_str(strs, s, num_strs))) {
strs[num_strs] = s;
map_id2 = num_strs;
num_strs++;
} else
map_id2--;
union_dj(rt, rnk, wt, values[i], map_id1, map_id2);
}
}
double *ans = (double *)malloc(sizeof(double) * queries_size);
for (int i = 0; i < queries_size; i++) {
s = queries[i][0];
if (!(map_id1 = map_str(strs, s, num_strs))) {
ans[i] = -1;
continue;
} else
map_id1--;
s = queries[i][1];
if (!(map_id2 = map_str(strs, s, num_strs))) {
ans[i] = -1;
continue;
} else
map_id2--;
if (map_id1 == map_id2)
ans[i] = 1;
else if (wt[map_id1][map_id2] != 1.0)
ans[i] = wt[map_id1][map_id2];
else {
int rx = find_dj(rt, wt, map_id1);
int ry = find_dj(rt, wt, map_id2);
if (rx == ry)
ans[i] = wt[map_id1][rx] * wt[rx][map_id2];
else
ans[i] = -1.0;
}
}
*return_size = queries_size;
return ans;
}
|