This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor_road.cpp
90 lines (81 loc) · 1.41 KB
/
xor_road.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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct edge
{
int a, b;
unsigned long long w;
bool operator<(const edge &other)
{
return w < other.w;
}
};
int n;
vector<int> par;
vector<int> rnk;
vector<unsigned long long> val;
vector<edge> edges;
void make_set(int i)
{
par[i] = i;
rnk[i] = 0;
}
int find_set(int i)
{
if (par[i] == i)
return i;
return par[i] = find_set(par[i]);
}
void union_set(int a, int b)
{
a = find_set(a);
b = find_set(b);
if (a != b)
{
if (rnk[a] < rnk[b])
swap(a, b);
par[b] = a;
if (rnk[a] == rnk[b])
rnk[a]++;
}
}
unsigned long long solve()
{
unsigned long long res = 0;
for (int i = 0; i < n; ++i)
{
make_set(i);
}
for (auto [a, b, w] : edges)
{
if (find_set(a) != find_set(b))
{
res -= w;
union_set(a, b);
}
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
par.assign(n, 0);
rnk.assign(n, 0);
val.resize(n);
for (int i = 0; i < n; ++i)
{
cin >> val[i];
}
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
edges.emplace_back(edge{i, j, -(val[i] ^ val[j])});
}
}
sort(edges.begin(), edges.end());
cout << solve();
}