-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.h
More file actions
61 lines (61 loc) · 1.08 KB
/
structs.h
File metadata and controls
61 lines (61 loc) · 1.08 KB
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
#pragma once
#include <cstdlib>
struct Edge
{
int node;
int ind;
};
class Graph
{
public:
int n; int m;
Edge* edgelists;
int* ptr;
Graph(int n, int m) :n(n), m(m) { }
Graph() { m = 0; }
void destroy() { free(edgelists); free(ptr); }
};
class DisjSet
{
public:
int* parent;
int* rank;
DisjSet(int max_size)
{
parent = (int*)malloc(sizeof(int) * max_size);
rank = (int*)malloc(sizeof(int) * max_size);
for (int i = 0; i < max_size; i++) parent[i] = i;
}
int find(int x)
{
int p = parent[x];
if (parent[p] != p)
{
p = find(p);
parent[x] = p;
}
return p;
}
void to_union(int x1, int x2)
{
int f1 = find(x1);
int f2 = find(x2);
if (f1 == f2) return;
if (rank[f1] < rank[f2]) parent[f1] = f2;
else
{
parent[f2] = f1;
if (rank[f1] == rank[f2]) ++rank[f1];
}
}
bool is_same(int e1, int e2) { return find(e1) == find(e2); }
void destroy() { free(parent); free(rank); }
};
struct dfsnode { int node, from; };
struct bfsnode { int node, layer; };
typedef struct edge
{
int u, v;
double w;
edge(int u, int v, double w) : u(u), v(v), w(w){}
}edge;