-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeidel-3.cpp
100 lines (75 loc) · 1.68 KB
/
Seidel-3.cpp
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
#define ARMA_NO_DEBUG
#include <cstdio>
#include <armadillo>
using namespace std;
using namespace arma;
int n, m;
void Seidel(imat& A) {
bool done = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
if (A(i,j) == 0) {
done = false;
i = j = n; // break
}
}
}
if (done) {
return;
}
imat B = A * A;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j && (A(i,j) == 1 || B(i,j) > 0)) {
B(i,j) = 1;
} else {
B(i,j) = 0;
}
}
}
Seidel(B);
imat X = B * A;
icolvec degree = sum(A, 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (X(i,j) >= B(i,j) * degree[j]) {
A(i,j) = 2 * B(i,j);
} else {
A(i,j) = 2 * B(i,j) - 1;
}
}
}
}
inline void read_input() {
scanf("%d%d", &n, &m);
imat adjMat = imat(n, n, fill::zeros);
while (m--) {
int u, v;
scanf("%d%d", &u, &v);
adjMat(u,v) = adjMat(v,u) = 1;
}
}
inline double solve() {
clock_t start = clock();
Seidel(adjMat);
return (clock() - start) / (double) CLOCKS_PER_SEC;
}
inline void debug() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%lld ", adjMat(i,j));
} printf("\n");
}
}
int main() {
read_input();
double duration = solve();
#ifndef DEBUG
debug();
#endif
printf("duration: %f\n", duration);
return 0;
}