-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjacency.h
149 lines (134 loc) · 2.92 KB
/
adjacency.h
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
#include <cmath>
#include <vector>
#include <unordered_set>
typedef std::vector<int> Vect;
typedef std::vector<Vect> Vect2;
typedef unordered_set<long long> HashSet;
struct GroupGraph {
int n,h;
Vect2 edges, splitoff, group_adj;
Vect group_id, group_size;
HashSet group_edges;
GroupGraph(int n_) {
n=n_;
}
long long encode(int a, int b) {
if (b<a) swap(a,b);
return a*(long long)n+b;
}
void connect(int a, int b) {
long long ab=encode(a,b);
if (!group_edges.count(ab)) {
group_edges.insert(ab);
group_adj[a].push_back(b);
group_adj[b].push_back(a);
}
}
int add_group() {
group_size.push_back(0);
splitoff.push_back(Vect());
group_adj.push_back(Vect());
return h++;
}
void process_edge(Vect &edge) {
// partition edge nodes by groups
Vect intersected_groups, new_group;
for (int x : edge) {
int g=group_id[x];
if (g==-1) new_group.push_back(x);
else {
if (splitoff[g].empty()) intersected_groups.push_back(g);
splitoff[g].push_back(x);
}
}
Vect groups;
// add new group
if (new_group.size()>0) {
int id=add_group();
for (int x : new_group) group_id[x]=id;
group_size[id]=new_group.size();
groups.push_back(id);
}
// split off the groups if necessary
for (int g : intersected_groups) {
if ((int)splitoff[g].size()==group_size[g]) {
groups.push_back(g);
} else {
int g_new=add_group();
// move nodes
for (int x : splitoff[g]) {
group_size[group_id[x]]--;
group_id[x]=g_new;
group_size[group_id[x]]++;
}
// copy connections
for (int g_adj : group_adj[g]) {
connect(g_new,g_adj);
}
connect(g_new,g);
groups.push_back(g_new);
}
}
// interconnect contained groups
for (int g1 : groups) {
for (int g2 : groups) if (g1<g2) connect(g1,g2);
}
// clean-up
for (int g : intersected_groups) {
splitoff[g].clear();
}
}
void process() {
h=0;
group_id = Vect(n, -1);
group_size = Vect(n, 0);
splitoff = Vect2(n, Vect());
group_edges = HashSet();
for (Vect &edge : edges) {
process_edge(edge);
}
}
int query(int i, int j) {
assert(h>0);
int gi=group_id[i], gj=group_id[j];
if (gi==-1 || gj==-1) return 0;
return (gi==gj || group_edges.count(encode(gi,gj)));
}
};
struct HyperGraph {
int n,m;
Vect2 edges;
vector<GroupGraph> sections;
void preprocess(double eps=1.0) {
int k=max(1.0,eps*log2(m));
GroupGraph g(n);
for (int i=0;i<m;i++) {
if (i%k==0) {
if (i!=0) sections.push_back(g);
g=GroupGraph(n);
}
assert(edges[i].size()>0);
g.edges.push_back(edges[i]);
}
sections.push_back(g);
for (GroupGraph &g : sections) {
g.process();
}
}
int query(int i, int j) {
for (GroupGraph &g : sections) {
if (g.query(i,j)) return 1;
}
return 0;
}
Vect2 adj;
void adjacency_matrix(double eps=1.0) {
preprocess(eps);
adj = Vect2(n, Vect(n));
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
adj[i][j]=query(i,j);
}
}
}
};