-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seidel-2.cpp
106 lines (82 loc) · 2.1 KB
/
Seidel-2.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
101
102
103
104
105
106
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> degree;
vector<vector<int>> adjMat;
vector<vector<int>> X;
inline void multiply(const vector<vector<int>>& A, const vector<vector<int>>& B, vector<vector<int>>& Z) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Z[i][j] = 0;
for (int k = 0; k < n; k++) {
Z[i][j] += A[i][k] * B[j][k];
}
}
}
}
void Seidel(vector<vector<int>>& 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;
}
vector<vector<int>> B(n, vector<int>(n, 0));
multiply(A, A, B);
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);
multiply(B, A, X);
for (int i = 0; i < n; i++) {
degree[i] = 0;
for (int j = 0; j < n; j++) {
degree[i] += A[i][j];
}
}
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;
}
}
}
}
int main() {
cin >> n >> m;
adjMat.assign(n, vector<int>(n, 0));
while (m--) {
int u, v;
cin >> u >> v;
adjMat[u][v] = adjMat[v][u] = 1;
}
clock_t start = clock();
X.assign(n, vector<int>(n, 0));
degree.assign(n, 0);
Seidel(adjMat);
double duration = (clock() - start) / (double) CLOCKS_PER_SEC;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", adjMat[i][j]);
} printf("\n");
}
printf("duration: %f\n", duration);
return 0;
}