-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.cpp
More file actions
87 lines (74 loc) · 2.12 KB
/
reference.cpp
File metadata and controls
87 lines (74 loc) · 2.12 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
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
#include "gen.h"
#include <cstdio>
#include <algorithm>
#include <tuple>
#include <vector>
#include <numeric>
using namespace std;
struct BiEdge {
vid_t from, to;
weight_t w;
bool operator<(const BiEdge& o) const {
return tie(w, from, to) < tie(o.w, o.from, o.to);
}
};
struct {
vector<vid_t> cmp;
void init(vid_t n) {
cmp = vector<vid_t>(n, 0);
iota(cmp.begin(), cmp.end(), 0);
}
vid_t get(vid_t v) {
return cmp[v] == v ? v : cmp[v] = get(cmp[v]);
}
void merge(vid_t v, vid_t u) {
if (rand()&1) swap(v, u);
cmp[v] = u;
}
} snm;
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s input\n", argv[0]);
return 1;
}
readAll(argv[1]);
printf("Vertexes: %lld\n", (long long)(vertexCount));
printf("Edges: %lld\n", (long long)(edgesCount));
vid_t largeCnt = 0;
eid_t maxDegree = 0;
for (vid_t v = 0; v < vertexCount; ++v) {
eid_t degr = edgesIds[v + 1] - edgesIds[v];
if (degr > 1000) ++largeCnt;
if (degr > maxDegree) maxDegree = degr;
}
printf("Large vertexes: %d\n", (int)largeCnt);
printf("Max degree is %lld\n", maxDegree);
int64_t prepareTime = -currentNanoTime();
vector<BiEdge> es;
es.reserve(edgesCount / 2);
for (vid_t v = 0; v < vertexCount; ++v) {
for (eid_t i = edgesIds[v]; i < edgesIds[v + 1]; ++i) {
vid_t u = edges[i].dest;
if (u <= v) continue;
weight_t w = edges[i].weight;
es.push_back(BiEdge{v, u, w});
}
}
sort(es.begin(), es.end());
prepareTime += currentNanoTime();
int64_t calcTime = -currentNanoTime();
weight_t result = 0.0;
snm.init(vertexCount);
for (const BiEdge& e : es) {
int cv = snm.get(e.from);
int cu = snm.get(e.to);
if (cv != cu) {
result += e.w;
snm.merge(cv, cu);
}
}
calcTime += currentNanoTime();
printf("%.10lf\n", double(result));
fprintf(stderr, "%.3lf\n%.3lf\n", double(prepareTime) / 1e9, double(calcTime) / 1e9);
return 0;
}